Open gastonmuijtjens opened 5 months ago
@gastonmuijtjens Can you share more about the scenario you are trying to accomplish?
@gastonmuijtjens Can you share more about the scenario you are trying to accomplish?
Hi @satvu, thank you for your reply. In my specific scenario I want to be able to set the Content-Length
header based on the length of the body. When not using the ASP.NET Core-integration, I am able to accomplish this by using the following middleware:
public class ContentLengthMiddleware : IFunctionsWorkerMiddleware
{
public async Task Invoke(FunctionContext context, FunctionExecutionDelegate next)
{
await next(context);
var httpResponseData = context.GetHttpResponseData();
httpResponseData?.Headers.Add(HeaderNames.ContentLength, httpResponseData.Body.Length.ToString());
}
}
However since I cannot run my own middleware after the middleware that handles the ASP.NET Core-integration, there is no way for me to retrieve the length of the response body.
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 defaultFunctionsHttpProxyingMiddleware
(afterawait next(context);
). However the extension methodConfigureFunctionsWebApplication
onIHostBuilder
does not provide a way to register middleware before the call toUseAspNetCoreIntegration()
(which registers theFunctionsHttpProxyingMiddleware
):If I would register my own custom middleware in
Program.cs
using the provided extension method onIHostBuilder
,MyCustomMiddleware
would run beforeFunctionsHttpProxyingMiddleware
. This is not what I want, because theHttpContext
attached toFunctionContext
is not filled with the response, response code and response headers after theIActionResult
was executed:Unfortunately I cannot implement my own implementation of the
ConfigureFunctionsWebApplication
extension method to solve this issue, becauseConfigureAspNetCoreIntegration
and some used classes areinternal
. Is there any way to register the middleware so that it would run beforeFunctionsHttpProxyingMiddleware
?Microsoft.Azure.Functions.Worker.Extensions.Http.AspNetCore 1.2.1
.NET 6.0