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.
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:
var host = new HostBuilder()
.ConfigureFunctionsWorkerDefaults( builder =>
{
builder.AddApplicationInsights()
.AddApplicationInsightsLogger()
.UseMiddleware<YourMiddleweare>()
} )
.ConfigureServices( services =>
{
... add services to DI container here ...
}
.Build();
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments