Introduction to OWIN and KATANA Using VS2013

OWIN

Open Web Interface .Net (OWIN) defines a standard interface between a .NET web server and web applications. it is a community-owned and open-source project. OWIN is designed to decouple the relationship between an ASP.NET application and IIS by defining a standard interface. If the web server id decouples from the application framework then we can use the application with a different web server and other components.



Layers

The following describes the layers of an application that the OWIN Specification is built on.

The approach travels from the bottom to the top.


Host

The function of the host is to create the pipeline by fetcheing the configuration information from our application. The host is responsible for starting the process.

Server

The HTTP server directly communicates with the client and then uses OWIN rules to process the request. the server binds to a port and listens for the request. It receives the request and passes the request in the pipeline that our application has configured. IIS is an example of both a Host and a Server.

Framework

The web framework may require an adapter layer that converts from OWIN rules. The Application Framework provides a development model to our application. MVC ia an example of such a framework.

Application

Application is the application that we are developing. The preceding component-based approach is to decouple the web server from our application that gives us multiple options to use any component for any of the various layers. This is in contrast to the coupling between System.Web and IIS. We can swap System.Web with Web Listener, IIS with a Console Application, MVC with WebAPI without effecting our existing application.

OWIN Components

There are only two main pieces in OWIN.

A Environment Dictionary

The Environment dictionary helps create the decoupling OWIN architecture. This dictionary is passed to our application by the server with the request and response header and bodies. Our application interacts with this dictionary instead of communicating with the server. The environment dictionary must not be null. Keys must be compared using StringComparer.Ordinal.

A generic Function Delegate

This delegate take a dictionary parameter and returns a task. We can plug-in a different middle wear component using this. The environment dictionary can contain only the designated keys. Some of the keys related to the Request are the following:

Key Description
"owin.RequestBody" A stream with the request body
"owin.RequestMethod" A string containing the HTTP methods
"owin.RequestHeaders" A I Dictionary<string,string[]>
"owin.RequestPath" A string that contain the request path

Katana is a collection of projects for supporting OWIN with various Microsoft components.

A simple Katana application

To create our OWIN application we will start by creating an ASP. NET project with empty template.




For creating our application we need to add two references to our web application. To add the references we will just run the following commands in the Nugget Package Manager console.

PM>Install-Package Microsoft.Owin.Host.SystemWeb

PM>Install-Package Nancy.Owin




In this application we will use IIS as the host and System.Web as the server since these two are well integrated with each other, so we can use them for our sample application.

In your "Startup1.cs" file write the following code:

  1. using System;  
  2. using System.Threading.Tasks;  
  3. using Microsoft.Owin;  
  4. using Owin;  
  5.   
  6. [assembly: OwinStartup(typeof(WebApplication2.Startup1))]  
  7.   
  8. namespace WebApplication2  
  9. {  
  10.      public class Startup1  
  11.      {  
  12.           public void Configuration(IAppBuilder app)  
  13.            {  
  14. // For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=316888  
  15.  
  16.               app.Run(OurApplicationStartMethod);   
  17.   
  18.             }  
  19.   
  20.       private Task OurApplicationStartMethod(IOwinContext context)  
  21.        {  
  22.      
  23.           context.Response.ContentType = "text/plane";  
  24.           return context.Response.WriteAsync("Welcome to the Demo of Katana Application");  
  25.         }  
  26. }  
  27.   

Output of the preceding code


Summary 

Open source OWIN is for implementing Katana, that decouples the web app.  

Up Next
    Ebook Download
    View all
    Learn
    View all