v2.1.0 #6
Divakar-kumar
started this conversation in
Ideas
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Features
Supported Frameworks
Installation
Install with Package Manager Console
PM> Install-Package AzureFunctions.Extensions.MiddlewareUsage
Getting Started
1. Add HttpContextAccessor and ExecutionContext to service collection
Inorder to access/modify HttpContext within custom middleware we need to add HttpContextAccessor in Startup.cs file and for accessing ExecutionContext we need to add FunctionExecutionContext concrete class in Startup.cs
2. Add custom middlewares to the pipeline
One or more custom middlewares can be added to the execution pipeline using MiddlewareBuilder.
2.1 Use()
2.2 UseWhen()
of exectuion.
3. IMiddlewareBuilder dependency
We can now add IMiddlewareBuilder as a dependency to our HTTP trigger function class.
4. Define Custom middlewares
If HTTP trigger is used try to implement the InvokeAsync(HttpContext) and for non-http triggers implement InvokeAsync(ExecutionContext), (If both http and non-http triggers are deployed in same
azure function try to implement both methods)
5. Execute pipeline
Now we need to bind last middleware for our HttpTrigger method , to do that wrap our existing code inside Functionsmiddleware block "_middlewareBuilder.ExecuteAsync(new FunctionsMiddleware(async (httpContext) =>{HTTP trigger code})"
For returning IActionResult use FunctionsMiddleware
For non-http triggers use TaskMiddleware
Based on the type of middleware(TaskMiddleware or FunctionsMiddleware) , respective InvokeAsync method will called with ExecutionContext or HttpContext
Sample
You can find .NET 6 sample application here . In this example we have registered Exception handling custom middleware to the exectuion order that
will handle any unhandled exceptions in the Http Trigger execution.
Special Thanks
Thank you to the following people for their support and contributions!
Sponsor
Leave a ⭐ if this library helped you at handling cross-cutting concerns in serverless architecture.
Website | LinkedIn | Forum | Contribution Guide | Donate | License
© Divakar Kumar
For detailed documentation, please visit the docs.
Contact
Divakar Kumar - @Divakar-Kumar - https://iamdivakarkumar.com
Project Link: https://github.com/Cloud-Jas/AzureFunctions.Extensions.Middleware
This discussion was created from the release v2.1.0.
Beta Was this translation helpful? Give feedback.
All reactions