Azure Function recently supports middleware feature with .NET 5. In this post, I show you how to implement the new feature in a demo project.
Assume we want to add a record to the DB. The naive approach: a function that writing logs after starting, doing some business logic, and adding data to ORM, then committing changes it into the database, and ultimately writing a log after all of the previous tasks being performed. Earlier without .NET 5, we had to do that like this code:
We wrote logs, do business logic, commit in a function.
A cleaner code could be achieved by obeying SRP using the Chain of Responsibility pattern. Let’s implement it with the new feature of Azure Function, Middleware.
Note: Visual Studio and Visual Studio for Mac have not yet been updated to run .NET 5 Azure Functions. Currently, the only way to run our .NET 5 Azure Functions locally is to use the command line.
Setup HostBuilder; Note: UseFunctionExecutionMiddleware is defined in Microsoft.Azure.Functions.Worker
Source Code is available on Github. We can benefit a lot using the new useful feature. hope you found this article useful.