I found one sample on Stackoverflow (How to insert a middleware in Azure Durable Functions), but it also just talks about Isolated and Duarable Trigger functions. I want to add middlelayer in Azure HttpTrigger (Microsoft.NET.Sdk.Functions) Functions in .net
I am creating a project in .Net 6. This project will have HttpTrigger type of Azure functions.
When I created the default project from Visual Studio Azure function template, it has just created a simple function file and project without startup and Program.cs.
I can define other layers like data layer, business layer, etc. but the problem I am facing is:
How to add Middleware layer in Azure Functions (Microsoft.NET.Sdk.Functions) application?
One solution I feel might be we can add a program.cs file to the project which will have a main method? My doubt is can we add Program.cs file to this project, what could be the implications?
Or is their a way to use Startup.cs to configure middlewares?
Why this doubt of adding Program.cs file came to my mind is because, all the online examples I am seeing is for Isolated Azure Function. and their is no reference of HttpTrigger function, so is it correct to add a program.cs to HttpTrigger function project.
I have already added Startup.cs for dependency Injection.
2
Answers
Adding important discussions and my findings and some links around in-process function app.
If you have this:
Microsoft.NET.Sdk.Functions
as dependency in your solution you have in-process function app. If you are in .Net6, and you create a Function App solution in Visual Studio, it will create in-process function app, with no Startup or Program file. These functions you can directly copy paste to Azure Portal and run.but in-process function apps is legacy now. and it has support till .net6. After .net6, in-process function apps will be deprecated.
.net6 is LTS release and in November 2023, we will have .Net8 release after which in-process function apps will be deprecated and we will just have isolated function apps. (https://learn.microsoft.com/en-us/azure/azure-functions/dotnet-isolated-in-process-differences)
Conclusion:
The Azure Functions application must use the isolated process, in which case you use the
HostBulder.AddMiddleware<T>
method to add middleware when configuring the function worker: