This package provides serverless functions with middleware functionality similar to AspNetCore
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
}
}
[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>();
}
}
}
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);
}
}
builder.UseMiddleware<SampleMiddleware>();
UseFunction(Func<Task> 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");
});
await pipeline.RunAsync(req.HttpContext);
builder.Clear();
