Click here to Skip to main content
15,506,599 members
Articles / Web Development / HTML
Posted 9 Oct 2014

Tagged as


81 bookmarked

Understanding OWIN and Katana

Rate me:
Please Sign up or sign in to vote.
4.81/5 (49 votes)
9 Oct 2014CPOL10 min read
OWIN is used to create loosely coupled web applications


ASP.NET Web forms is fairly popular as a Rapid Application Development framework for developing web applications. It took the event based approach of Visual basic and provided a better alternative to  the asp developers for developing  the web applications.

ASP.NET has been around since more than a decade now. The web development world  has changed drastically in the last couple of years.Some of the challenges that we all might have faced while working with ASP.NET are:


In ASP.NET the assembly System. Web includes all the ASP.NET functionality. This means that any new functionality can be released as a part of new framework release only.

As the framework contains many other components besides ASP.NET , framework releases cannot happen very frequently, thus new features in ASP.NET are dependent upon the framework .This is a disadvantage with Web Forms.

As we have seen in the last couple of years web technologies have evolved at a very rapid pace. Earlier we had server side scripting technologies like asp, then compiled languages like java and c# became quite popular for server side development. Now the web development world is fast moving towards Single Page applications.

This problem was later addressed  with  the release of technologies like MVC.MVC can be downloaded as a separate nuget package. What this means is that the updates in MVC can happen  more frequently independent of the .NET framework releases. We don't have to wait for the release of new version of the .NET framework for new MVC features.

MVC functionality is included in System.Web.Mvc which we can add as a nuget package.

Tight coupling to IIS

The System. Web assembly has a tight coupling to IIS. Though we have other hosts also like visual studio web server but they support only limited ASP.NET features .So there is only one hosting option for deployment.

Image 1

No easy way to turn off features like the HTTP modules in the HTTP Pipeline

There is a request pipeline in ASP.NET which consists of things like the different types of httpmodules, page and control life cycle . They are all part of the request pipeline and there is no easy way to disable them.

We may not want to execute all the layers like the page lifecycle to happen for our simple application. We may just want to return the response from the handler and ignore the rest of the layers.

So every web forms application no matter how simple we want it to be always follows a certain request path and forces us to use features we do not require .Like the authentication and lot of other HTTP Modules.

This should make us realize some of the attributes that should be present in a modern web framework.

Loose coupling

Creating a loose coupling between the application components has the advantage of swapping the  components with different ones. Wouldn’t it be great if we can easily replace the server that is hosting our web application  without  effecting the rest of the application.

Provide minimal set of features

There should be only the minimal set of features provided in a framework with the capability to add new features as and when required. Taking the example of web forms we should able to run our applications using just the HTTP protocol, as it is what defines web applications, without using the rest of the features like Page lifecycle ,HTTP modules and other HTTP pipeline components.

Promote open source

The software world have changed drastically over the years. Now the world is fast moving towards open source in which anybody can contribute.

The OWIN specification  provides solution  to the above mentioned problems.OWIN stands for Open Web Interface for .Net 

OWIN  defines  a standard interface between .NET web servers and web applications. The goal of the OWIN interface is to decouple server and application

Image 2

OWIN decouples the web server from the application frameworks .This may sound to be a simple principle ,and it is, but if it has many implications if we think about it. As the web server is decoupled from the application framework we can use the application with different web servers and other components.

Flexibility  We have the option of selecting the different components and easily replacing one component with another one.We can mix and match different components as we see fit based on our requirement.

Portable  Owin lets us us connect the existing components with new components which are not restricted to any particaular vendor.We can repalce the host,server or the middleware anytime.

Efficiency  Owin is Lightweight. Owin provides us the capability to add and use features we require .As there are different components  to handle specific responsibilities we can use only the ones we require,rather than being forced to use something we never use.

OWIN is just a specification and there could be many different implementations of OWIN.Katana is an example of one such implementation.

An application built upon OWIN  specification has the following layers

Host is responsible for starting the process. It also creates the pipeline by fetching the information from our application. It fetches the configuration information from  our application to create the pipeline.

Server  binds to a port and listens for the requests. Once it receives the request it passes the request in the pipeline that our application has configured.IIS is an example of both Host and Server .

Application framework provides a development model to our application.MVC is a an example of one such framework.

Application is the application that we are developing.


Image 3

The advantage of using this component based approach to decouple web server and our application is that it gives us multiple options to use any component for any of the different layers.This is in contrast to the coupling between System.Web and IIS.

Image 4

We can swap IIS with ConsoleApplication ,   MVC with WebAPISystemWeb with  WebListener without effecting our existing application.Contrast this with the traditional web development model.It gives us lot of options doesn't it?

OWIN components

There are only two main pieces in OWIN

An environment dictionary   This dictionary is passed to  our application by the server with request and response header and bodies.Our application works directly  with this dictionary instead of communicating with the server.It is this dictionary which helps create the decoupled OWIN architecture.

A generic Func delegate  This delegate takes as a parameter the above mentioned environment dictionary and returns a Task.We can plugin different middleware components using this.

The environment dictionary can contain only the designated keys.Some of the keys related to the Request are






A Stream with the request body



A IDictionary<string, string[]> containing request headers. 



A string containing the HTTP method of the request (GET, POST etc. )



A string which contains the request path



A string which contains the portion of the request path corresponding to the "root" of the application delegate



A string which contains the protocol name and version (HTTP/1.0, HTTP/1.1  etc.)


Following happens when the host starts

  • The environment dictionary is created  by the Host and passed to the server.Host also populates this  ,prior to passing this to the server,to inform the server about itself.<o:p>
  • Server populates this dictionary to announce  its capabilities to other OWIN components, like our application and the server.
  • Host passes the dictionary to our application's setup code.<o:p>
  • Our application creates the request pipeline and returns the generic Func delegate.<o:p>
  • Host now starts the server by launching the server startup code with the dictionary and the delegate.

<o:p>After the above steps have completed the server is ready accept the requests using the delegate passed to it by our application.<o:p>

<o:p> As we saw host is responsible for creating the server ,as well as communicating with our application to create the  request pipeline.


As OWIN is just a specification ,for impleamtion of the OWIN specifications we need some concrete implementation.This is where Katana comes into the picture.Katana is a set of open source owin components built by Microsoft .It is used to build OWIN based web applications.

As we have seen earlier owin host communicates with the our application before loading the server ,host needs information from our application about how to create the OWIN pipeline. This pipeline is used to process our request.

When loading our application it discovers the startup class by looking at the information in the following order

  • If there is a  owin:AppStartup appSetting key in web.config  having value equal to a valid .net type ,then  type is used as the startup class.                                                                                                                    <appSetting  key=” owin:AppStartup”  value=”typeName”>
  • If there is a [assembly: OwinStartup(typeof(MyStartup))] attribute in our  assembly then the value of the type is used as the startup class.
  • If neither of the above succeeds in finding the startup class then katana looks for a class with a name Startup having  a method  with signature  void Configure(IAppBuilder app).

Below is the sample code that demonstrate the third method of discovering the startup class.We have defined a class called Startup with a method Configuration which takes IAppBuilder as a parameter.

public class Startup
       public void Configuration(IAppBuilder app)

      public Task OurApplicationStartMethod(IOwinContext context)
           context.Response.ContentType = "text/plain";
           return context.Response.WriteAsync("Hello World");



A simple Katana application

For the purpose of creating our OWIN application we will start by creating an ASP.NET project with an Empty template.Since we don't need ASP.NET functionality here we have started with the empty template.

Image 5

For creating our application we need to add two references to our web application.The reference to OWIN and the Nancy web framework.We will look into Nancy later.To add the references we will just run the below commnds in the nuget package manager console

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

PM>Install-Package Nancy.Owin

Image 6


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

When the server receives an HTTP request, the OWIN pipeline invokes the middleware. We will be implementing our middleware using Nancy web framework which complements Katana very well.Nancy is a framework for building web applications (HTTP bases applications).It’s  build using the lessons learnt from the previous web frameworks.Nancy has the following attributes:-

  • Open source    software world is moving fast moving towards open source.
  • Self contained     This means that it doesn’t have any dependency on other frameworks
  • Conventions over configuration  By following convention over configuration we can build simple Nancy applications in as less as few minutes.

Startup Class

We can define a class similar to the above class.One extra line we have included here is to add the Nancy to the OWIN pipeline using the UseNancy() extension method.

namespace KatanaApplication
    public class Startup
        public void Configuration(IAppBuilder app)


Now that we have defined our startup class we can define a module.Module in a Nancy application inherits from the NancyModule class and has the following important responsibilities

  • It defines the application behaviour.It uses routes for this purpose
  • It access to the request and response information
  • It returns the view to render to the end user.

Nancy uses routes to match the URL requests .Routes consists of the following three parts

  • Method   It is the HTTP verb such as GET or POST which is used to access the application
  • Pattern   This is matched with the URL
  • Action  This is the action that is performed if the route matches the request

In the below example Get is the method,"/" is the pattern and our lambda expression defines the action

namespace KatanaApplication
    public class HomeModule : NancyModule
        public HomeModule()
            Get["/"] = x =>
                var model = new { title = "Welcome From Owin Nancy Application",body="This is the sample body of the owin application view" };
                return View["home", model];


A nancy view engine serves the same purpose as the view engine in other frameworks like MVC.It  takes a template and a model and generates the HTML for the end user.It comes with a default view engine called SuperSimpleViewEngine and the cool thing is that we can directly use it without any other dependency.

<!DOCTYPE html>
<html xmlns="">


Below is the result of running our sample application.

Image 7

Points of Interest

OWIN decouples the Web application from the Web server.

OWIN specification is open source ,Katana is an OWIN implementation by Microsoft.




This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Written By
India India
I have a keen interest in technology and software development.I have worked in C#,ASP.NET WebForms,MVC,HTML5 and SQL Server.I try to keep myself updated and learn and implement new technologies.
Please vote if you like my article ,also I would appreciate your suggestions.For more please visit Code Compiled

Comments and Discussions

QuestionError Pin
Member 103444487-Dec-19 10:07
Member 103444487-Dec-19 10:07 
AnswerRe: Error - fix. Pin
Dogan Akyurek17-Dec-19 22:36
Dogan Akyurek17-Dec-19 22:36 
GeneralIt is copy paste of pluralsight course by Scott Allen :D Pin
Shobhit Vaish5-May-16 4:13
Shobhit Vaish5-May-16 4:13 
GeneralRe: It is copy paste of pluralsight course by Scott Allen :D Pin
ashish__shukla5-May-16 7:45
ashish__shukla5-May-16 7:45 
Questiongood article Pin
atulonweb@gmail.com11-Dec-15 0:27
atulonweb@gmail.com11-Dec-15 0:27 
GeneralGood article Pin
Daniel Miller19-Oct-15 14:29
professionalDaniel Miller19-Oct-15 14:29 
GeneralNice Explanation Pin
Alireza_13627-Oct-15 10:43
Alireza_13627-Oct-15 10:43 
GeneralComment Pin
Aamir Pervez30-Jul-15 20:42
Aamir Pervez30-Jul-15 20:42 
GeneralNice article Pin
Gorakhnath U Choudhari17-Apr-15 4:57
Gorakhnath U Choudhari17-Apr-15 4:57 
QuestionDifference between Host and Server Pin
Pankaj Bhandari083-Feb-15 0:42
Pankaj Bhandari083-Feb-15 0:42 
AnswerRe: Difference between Host and Server Pin
Alexandru Lungu7-Feb-15 1:38
professionalAlexandru Lungu7-Feb-15 1:38 
AnswerRe: Difference between Host and Server Pin
singsuyash9-Sep-15 22:55
singsuyash9-Sep-15 22:55 
QuestionMy vote of 5 Pin
Assil2-Dec-14 2:49
professionalAssil2-Dec-14 2:49 
QuestionGreat article! Pin
Paulo C B S16-Oct-14 8:27
Paulo C B S16-Oct-14 8:27 
AnswerRe: Great article! Pin
ashish__shukla17-Oct-14 17:30
ashish__shukla17-Oct-14 17:30 
QuestionNice article, but... Pin
abdurahman ibn hattab12-Oct-14 14:28
abdurahman ibn hattab12-Oct-14 14:28 
AnswerRe: Nice article, but... Pin
ashish__shukla14-Oct-14 20:10
ashish__shukla14-Oct-14 20:10 
GeneralMy vote of 5 Pin
Humayun Kabir Mamun9-Oct-14 23:13
Humayun Kabir Mamun9-Oct-14 23:13 
GeneralMy vote of 5 Pin
Thorsten Bruning9-Oct-14 21:36
Thorsten Bruning9-Oct-14 21:36 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.