StageMarkers inside of MapWhen

Sep 11, 2014 at 5:04 AM
I'm trying to use stage markers inside of a MapWhen mapping, but it isn't working as I'd expected. The first call to UseStageMarker inside of the MapWhen configuration seems to apply to all subsequent middleware.

Does anyone know whether this is expected behavior and whether there is a workaround available?

Here as a simple snippet demonstrating the issue:
public class Startup
{
    public void Configuration(IAppBuilder app)
    {
        app.MapWhen(context => true, MappedConfiguration);
    }

    private static void MappedConfiguration(IAppBuilder app)
    {
        app.Use((context, next) =>
        {
            Trace.WriteLine("1: " + HttpContext.Current.CurrentNotification.ToString());
            return next.Invoke();
        });

        app.UseStageMarker(PipelineStage.Authenticate);

        app.Use((context, next) =>
        {
            Trace.WriteLine("2: " + HttpContext.Current.CurrentNotification.ToString());
            return next.Invoke();
        });

        // Expect that this will make preceeding middleware to run in the PreHandlerExecute stage.
        app.UseStageMarker(PipelineStage.PreHandlerExecute);
    }
}
Expected Output (This is what I see if I don't call MapWhen and just directly use the configuration inside of MappedConfiguration)
1: AuthenticateRequest
2: PreExecuteRequestHandler
Actual Output:
1: AuthenticateRequest
2: AuthenticateRequest
I'm using IIS Express 8.0 and Microsoft.Owin.Host.SystemWeb 3.0.0.0.
Coordinator
Sep 11, 2014 at 2:45 PM
This is by design. The stage markers are limited to the primary pipeline. Trying to add them inside of Map/MapWhen results in them being added to the primary pipeline immediately after Map, forcing the whole contents of the Map to run earlier.