Skip to content
This repository was archived by the owner on Nov 20, 2018. It is now read-only.
This repository was archived by the owner on Nov 20, 2018. It is now read-only.

Need for terminal middleware #686

@jerriep

Description

@jerriep

I had the following code which used to work fine in RC2

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
    }

    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
    {
        loggerFactory.AddConsole(LogLevel.Debug);

        app.Use(async (context, next) =>
        {
            await context.Response.WriteAsync("<p>Hello from Middleware 1</p>");

            await next.Invoke();

            await context.Response.WriteAsync("<p>Goodbye from Middleware 1</p>");
        });

        app.Use(async (context, next) =>
        {
            await context.Response.WriteAsync("<p>Hello from Middleware 2</p>");

            await next.Invoke();

            await context.Response.WriteAsync("<p>Goodbye from Middleware 2</p>");
        });
    }
}

But now with 1.0 this does not seem to work anymore. I get the following error:

 Connection id "0HKU1UAM61D6F": An unhandled exception was thrown by the application.
System.InvalidOperationException: Status code cannot be set, response has already started.
   at Microsoft.AspNetCore.Server.Kestrel.Internal.Http.Frame.set_StatusCode(Int32 value)
   at Microsoft.AspNetCore.Builder.Internal.ApplicationBuilder.<>c.<Build>b__16_0(HttpContext context)
   at MiddlewareDeepDive.Startup.<>c.<<Configure>b__1_1>d.MoveNext() in C:\Development\AspnetCasts\MiddlewareDeepDive\02 - Your first middleware pipeline\After\MiddlewareDeepDive\Startup.cs:line 39
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
   at MiddlewareDeepDive.Startup.<>c.<<Configure>b__1_0>d.MoveNext() in C:\Development\AspnetCasts\MiddlewareDeepDive\02 - Your first middleware pipeline\After\MiddlewareDeepDive\Startup.cs:line 30
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.Hosting.Internal.RequestServicesContainerMiddleware.<Invoke>d__3.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.Server.Kestrel.Internal.Http.Frame`1.<RequestProcessingAsync>d__2.MoveNext()

And also I only get the following output displayed:

Hello from Middleware 1

Hello from Middleware 2

Is this something that has changed in 1.0 RTM? Should the middleware pipeline now always contain terminal middleware at the end on the pipeline?

Edit:
FWIW, here is a video proving that this used to work fine in RC2:
https://www.youtube.com/watch?v=B2WftX-etVo

See from around the 8:20 mark...

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions