skip to Main Content

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.

enter image description here

enter image description here

2

Answers


  1. Chosen as BEST ANSWER

    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) enter image description here

    Conclusion:

    • Create Function Apps which are isolated function apps. You will not be able to edit directly in the Azure portal, but that is also just for learning and training. Real applications don't work from portals, you need a proper solution, proper dependency injection, middlewares, etc. Isolated function apps are the future.

  2. 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();
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search