Wednesday, 16 July 2014

OWIN Katana and Stage Marker in Asp.net Integrated pipeline

This is third post in the series of blog posts on OWIN and Katana.
i) Basics of OWIN and Katana
ii) OWIN middleware-Asp.net integrated Pipeline extensions in Katana

In previous post of this post we discussed about the Asp.net integrated pipeline and HttpModule. How to add your custom OWIN middleware component? In this post we’ll be learning about the various stages of Asp.net Integrated pipeline and how to register and run your OMC at a specific stage.

Below is the list of various stages available in the Katana which acts as a OWIN middleware:

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,
}
Now if you want to run your code at specific stage you just need to register your OMC with the stage. This can be performed with the help of StageMarkers in OWIN. So let’s create or use our previous OMC and run it at a specific event. In this demo let’s use the stage of Authentication and run our OMC during the Authroization stage in the Asp.net Pipeline.


using AppFunc = Func<IDictionary<string, object>, Task>;
    using Microsoft.Owin;
 
    public class Startup
    {
        // Invoked once at startup to configure your application.
        public void Configuration(IAppBuilder builder)
        {
            // Middleware created with in startup class.
            builder.Use(new Func<AppFunc, AppFunc>(ignoredNextApp => (AppFunc)Invoke));
 
            // Specify the stage for the OMC
            builder.UseStageMarker(PipelineStage.Authenticate);
        }
 
        // Invoked once per request.
        public Task Invoke(IDictionary<string, object> environment)
        {
            string responseText = "Hello World";
 
            // writing response header asynchronously
            byte[] responseBytes = Encoding.UTF8.GetBytes(responseText);
 
            // See http://owin.org/spec/owin-1.0.0.html for standard environment keys.
            Stream responseStream = (Stream)environment["owin.ResponseBody"];
            IDictionary<string, string[]> responseHeaders =
                (IDictionary<string, string[]>)environment["owin.ResponseHeaders"];
 
            responseHeaders["Content-Length"] = new string[] { responseBytes.Length.ToString(CultureInfo.InvariantCulture) };
            responseHeaders["Content-Type"] = new string[] { "text/plain" };
 
            return responseStream.WriteAsync(responseBytes, 0, responseBytes.Length);
        }
    }

Similary you can any of the stage listed above and customize the Asp.net pipeline to process your request or response.

In our next blog post we’ll be talking about the self host OWIN process. Say no to System.web and run your web request on an individual server with no boundation of Plateform or host/server.

No comments:

Post a Comment