Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using Middleware with Azure Functions #2545

Open
vijayrkn opened this issue Sep 19, 2024 · 1 comment
Open

Using Middleware with Azure Functions #2545

vijayrkn opened this issue Sep 19, 2024 · 1 comment
Labels

Comments

@vijayrkn
Copy link

Creating this issue on behalf of the Visual Studio customer. Please find the details of the issue here: https://developercommunity.visualstudio.com/t/Using-Middleware-with-Azure-Functions/10748527

@vijayrkn vijayrkn added the bug label Sep 19, 2024
@vijayrkn
Copy link
Author

Dale Godfredson

Reported Sep 18, 2024 3:58 PM

Hi

I’d like to include middleware for my Azure function using the Isolated Worker Model. Currently using the latest version of .Net 8 & visual studio 2022.

As per the documentation (https://learn.microsoft.com/en-us/azure/azure-functions/dotnet-isolated-process-guide?tabs=windows) it indicates to use ConfigureFunctionsWorkerDefaults however when I try to use this visual studio gives me the following error:

AZFW0014: The registration method for ‘ConfigureFunctionsWebApplication’ is expected for ASP.Net Core integration.

The example code is:

var host = new HostBuilder()
    . ConfigureFunctionsWorkerDefaults(workerApplication =>
    {
        // Register our custom middlewares with the worker

workerApplication.UseMiddleware<ExceptionHandlingMiddleware>();

workerApplication.UseMiddleware<MyCustomMiddleware>();

workerApplication.UseWhen<StampHttpHeaderMiddleware>((context) =>
        {
            // We want to use this middleware only for http trigger invocations.
            return context. FunctionDefinition.InputBindings.Values
                          . First(a => a.Type.EndsWith("Trigger")). Type == "httpTrigger";
        });
    })
    . Build();

However I can get it to work adding a reference to ConfigureFunctionsWorkerDefaults after ConfigureFunctionsWebApplication as follows:

var host = new HostBuilder()
    . ConfigureFunctionsWebApplication()
    . ConfigureFunctionsWorkerDefaults((builder) =>
    {
        builder. UseMiddleware<FooMiddleware>();
    })
    . Build();

host. Run();

Can you please advise if this is the correct way to implement.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant