Serverless.Function.Middleware 0.1.2

dotnet add package Serverless.Function.Middleware --version 0.1.2                
NuGet\Install-Package Serverless.Function.Middleware -Version 0.1.2                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Serverless.Function.Middleware" Version="0.1.2" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Serverless.Function.Middleware --version 0.1.2                
#r "nuget: Serverless.Function.Middleware, 0.1.2"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Serverless.Function.Middleware as a Cake Addin
#addin nuget:?package=Serverless.Function.Middleware&version=0.1.2

// Install Serverless.Function.Middleware as a Cake Tool
#tool nuget:?package=Serverless.Function.Middleware&version=0.1.2                

Serverless.Function.Middleware

This package provides serverless functions with middleware functionality similar to AspNetCore

Create Middleware that implements IFunctionMiddleware instead of IMiddleware

public class SampleMiddleware : IFunctionMiddleware
{
    public SampleMiddleware()
    {
    }

    public async Task InvokeAsync(HttpContext httpContext, FunctionRequestDelegate next)
    {
        // Do something before function is executed
        
        await next(httpContext);
        
        // Do something after function is executed
    }
}

Startup File using Microsoft.Azure.Functions.Extensions Package

[assembly: FunctionsStartup(typeof(Startup))]
namespace SampleAzureFunction
{
    public class Startup : FunctionsStartup
    {
        public override void Configure(IFunctionsHostBuilder builder)
        {
            var services = builder.Services;

            services.UseServerlessMiddleware();
            services.AddTransient<SampleMiddleware>();
        }
    }
}

Implement Function, load middleware into the functionBuilder and execute pipeline

public class SampleFunction
{
    private readonly IFunctionApplicationBuilder _builder;

    public SampleFunction(IFunctionApplicationBuilder builder)
    {
        _builder = builder;

        // Loading the middleware into the builder. You can load multiple middleware.
        _builder.UseMiddleware<SampleMiddleware>();
    }

    [FunctionName("Function1")]
    public async Task<IActionResult> Run(
        [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
        ILogger log)
    {
        // Loading the endpoint to be called. You can only load a single endpoint
        // This will return a pipeline complete with middleware and endpoint delegates.
        var pipeline = _builder.UseEndpoint(async () =>
        {
            log.LogInformation("FUNCTION EXECUTING!!!!");

            return new OkObjectResult("Everything went well");
        });

        // Execute the pipeline.
        return await pipeline.RunAsync(req.HttpContext);
    }
}

IFunctionApplicationBuilder Methods

UseMiddleware<TMiddleware>() - Add Middleware to the pipeline (Can add multiple)

builder.UseMiddleware<SampleMiddleware>();

UseFunction(Func<Task<IActionResult>> function) - Add or Replace function that returns IActionResult to the pipeline. (Can only have once)

var pipeline = _builder.UseEndpoint(async () =>
{
    return new OkObjectResult("Everything went well");
});

RunAsync(HttpContext httpContext) - Executes the request pipeline

await pipeline.RunAsync(req.HttpContext);

Clear() - Clears middleware and endpoint from the request pipeline

builder.Clear();

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp3.1 is compatible. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
0.1.2 8,379 4/24/2020
0.1.1 375 4/24/2020
0.1.0 378 4/24/2020

Launch Party!