Skip to content

Unable to disable buffering once enabled #508

@TaoziZ03

Description

@TaoziZ03

Summary

Hi, I am migrating a web to net core. The line 68 enable response buffering if HttpApplication events are needed. This disables our cache policy in later code.

internal static void UseSystemWebAdapterFeatures(this IApplicationBuilder app)
{
if (app.HasBeenAdded())
{
return;
}
if (app.AreHttpApplicationEventsRequired())
{
app.UseMiddleware<HttpApplicationMiddleware>();
}
app.UseMiddleware<PreBufferRequestStreamMiddleware>();
app.UseMiddleware<BufferResponseStreamMiddleware>();
app.UseMiddleware<SetDefaultResponseHeadersMiddleware>();
app.UseMiddleware<SingleThreadedRequestMiddleware>();
app.UseMiddleware<CurrentPrincipalMiddleware>();
if (app.AreHttpApplicationEventsRequired())
{
app.UseHttpApplicationEvent(ApplicationEvent.BeginRequest);
}
}

Motivation and goals

Allow user code to manage response buffering feature.

Risks / unknowns

Can anyone provide some insights for this design?

Repro

builder.Services.AddSystemWebAdapters()
    .AddWrappedAspNetCoreSession()
    .AddHttpApplication<MyApp>(options =>
    {
        options.RegisterModule<MyModule>("Module1");
        options.RegisterModule<MyModule>("Module2");
        options.RegisterModule<MyModule>("Module3");
.......
    });

var app = builder.Build();

// Since HttpApplication events are needed, here will add `HttpApplicationMiddleware`.
app.UseSystemWebAdapters();

app.Use((context, next) =>
{
    // This will not success because `HttpApplicationMiddleware` will enable buffering.
    if(!ShouldBufferReponse(context.Request.Path))
        context.Features.Get<IHttpResponseBodyFeature>().DisableBuffering();
    return next();
});

Here is where updated the response buffering.

context.Features.GetRequired<IHttpResponseBufferingFeature>().EnableBuffering(BufferResponseStreamAttribute.DefaultMemoryThreshold, default);

Metadata

Metadata

Assignees

No one assigned

    Labels

    Needs: Triage 🔍Label added to new issues which need Triage

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions