Azure / Azure/azure-functions-dotnet-worker

.NET isolated ASP.NET Core integration: provide a way to define middleware before .UseAspNetCoreIntegration()

Open
#2,280 2 comments 5 reactions 0 assignees View on GitHub
area: http enhancement
Dominant language
C#
Stars
466
Forks
215
Avg merge
3d 10h
Merged PRs (30d)
7

Description

### Description

I am using isolated Azure Functions with ASP.NET Core integration (`Microsoft.Azure.Functions.Worker.Extensions.Http.AspNetCore`). I want to be able to define my own middleware which runs after the default `FunctionsHttpProxyingMiddleware` (after `await next(context);`). However the extension method `ConfigureFunctionsWebApplication` on `IHostBuilder` does not provide a way to register middleware before the call to `UseAspNetCoreIntegration()` (which registers the `FunctionsHttpProxyingMiddleware`):

```csharp
public static IHostBuilder ConfigureFunctionsWebApplication(this IHostBuilder builder, Action configureWorker)
{
builder.ConfigureFunctionsWorkerDefaults((hostBuilderContext, workerAppBuilder) =>
{
workerAppBuilder.UseAspNetCoreIntegration();
configureWorker?.Invoke(hostBuilderContext, workerAppBuilder);
});

builder.ConfigureAspNetCoreIntegration();

return builder;
}
```

If I would register my own custom middleware in `Program.cs` using the provided extension method on `IHostBuilder`, `MyCustomMiddleware` would run **before** `FunctionsHttpProxyingMiddleware`. This is not what I want, because the `HttpContext` attached to `FunctionContext` is not filled with the response, response code and response headers after the `IActionResult` was executed:
```csharp
var host = new HostBuilder()
.ConfigureFunctionsWebApplication(worker =>
{
worker.UseMiddleware();
})
```

Unfortunately I cannot implement my own implementation of the `ConfigureFunctionsWebApplication` extension method to solve this issue, because `ConfigureAspNetCoreIntegration` and some used classes are `internal`. Is there any way to register the middleware so that it would run **before** `FunctionsHttpProxyingMiddleware`?

- `Microsoft.Azure.Functions.Worker.Extensions.Http.AspNetCore 1.2.1`
- `.NET 6.0`
- Azure Functions isolated v4

Contributor guide

No contributing guide indexed for this repository

Research direction

Start by tracing ConfigureFunctionsWebApplication, UseAspNetCoreIntegration, ConfigureAspNetCoreIntegration, and FunctionsHttpProxyingMiddleware in the Microsoft.Azure.Functions.Worker.Extensions.Http.AspNetCore integration. Compare the Program.cs UseMiddleware example with the current registration order. Done means the public configuration path can place custom middleware after the proxy middleware, with coverage for the resulting HttpContext response state.

Written by the indexing model from the issue text.

Assessment

Tech stack
azure, csharp
Domain
backend, cloud
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.