OWIN Middleware components (OMC) to that of an HttpModule in the ASP.NET world, an OMC must be registered to the correct pre-defined pipeline event. In this article we’ll see how to create a middleware and register it with the pipeline event. Also default stage for the middleware execution in the pipeline. So let’s get started:
1. Create an empty Asp.net website.
2. Install the Microsoft.Owin.Host.SystemWeb Nuget package. You can either use the Nuget package manager UI from visual studio or directly run the below command in the Nuget package manager console:
This command will install the package library and other dependencies which includes “Owin”, Microsoft.Owin”. Note that the package Microsoft.Owin.Host.SystemWeb is support for System.Web in Owin.
3. Create a Startup class and here you can configure the startup of the server e.g. registering custom pipelines for stages, we’ll talk about about pipeline stages in later posts. Also host server will be supporting full debugging with in visual studio. Let’s just create a simple server startup configuration.
Run the server with F5. The host will be running on localhost:55666 port showing output like this.
As a default behaviour since we are using the SystemWeb the server will be serving the content using the Asp.net pipeline which in fact is IIS server. Now let’s now use the IIS server and use a little lighter host server to run our webapplication.
That was the simplest example of create an Asp.net IIS hosted application. Let’s do something interesting to understand the abstraction and flexibility between the server and application.
Let’s create a sample startup configuration that will display the information about the browser agent by reading the request header. Just update the code in the Configuration method of Startup class.
So I get the information about the User Agent for different browser:
Visual studio Inbuilt browser
Internet Explorer 11
This was simple isn’t it, let’s take a little dive with in and create some Middleware to interpret the request and response. We can create owin middleware implementation by two ways.
1. Creating a separate class for each Middleware for pipeline.
2. Using the Func delegate template and use it with in the Startup class.
First let’s create a BasicMiddleware to use in the pipeline. Also let’s add some code to see when this Middleware will be going to execute. Here’s the complete code for the class. In this class we’re adding a user item in the response header. That’s it.
Let’s run the server by hitting F5 again and see what happens.
As you can see the new item in the Response header is captured. Now let’s look at the stage of the pipeline as we did some tracing to get the stage when the response header was updated.
So Now we know the default stage to execute the Owin middleware is PreExecuteRequestHandler event. But here comes the interesting fact, You can customize this stage by using the StageMarker.
We’ll discuss this in our next post, How to run the middleware during various Asp.net pipeline stage events? Also, how to use a custom OWIN self-hosting process to host the web application? Don’t miss it.