While reviewing some code I noticed the following code snippet;
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void Configuration(IAppBuilder app) | |
{ | |
//Removed other code | |
app.UseStageMarker(PipelineStage.ResolveCache); | |
} |
I had no clue what Stage Markers where so time to dig in…
What are stage markers?
Stage markers play a rol when you are using OWIN in IIS. IIS has a specific execution pipeline containing a predefined set of pipeline events. If you want to run a specific set of OWIN middleware during a particular stage in the IIS pipeline, you can use the UseStageMarker
method as seen in the sample above.
Stage marker rules
There are rules on which stage of the pipeline you can execute middleware and the order components must run. Following stage events are supported:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public enum PipelineStage | |
{ | |
Authenticate = 0, | |
PostAuthenticate = 1, | |
Authorize = 2, | |
PostAuthorize = 3, | |
ResolveCache = 4, | |
PostResolveCache = 5, | |
MapHandler = 6, | |
PostMapHandler = 7, | |
AcquireState = 8, | |
PostAcquireState = 9, | |
PreHandlerExecute = 10, | |
} |
By default, OWIN middleware runs at the last event (PreHandlerExecute
). To run a set of middleware components during an earlier stage, insert a stage marker right after the last component in the set during registration.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void Configuration(IAppBuilder app) | |
{ | |
//This middleware will run at the ResolveCache event in the IIS pipeline | |
app.Use((context, next) => | |
{ | |
return next.Invoke(); | |
}); | |
app.UseStageMarker(PipelineStage.ResolveCache); | |
//This middleware will run at the PreRequestExecute event in the IIS pipeline | |
app.Use((context, next) => | |
{ | |
return next.Invoke(); | |
}); | |
} |
More information: https://docs.microsoft.com/en-us/aspnet/aspnet/overview/owin-and-katana/owin-middleware-in-the-iis-integrated-pipeline