Skip to main content

OWIN - Katana

OWIN defines a standard interface between .NET web servers and web applications.
It aims to decouple the relationship between ASP.NET applications and IIS by defining a standard interface. Developers of Web servers can be sure that, if they implement OWIN correctly, ASP.NET applications will run on their server.

In short: OWIN acts as an adapter between web servers and web applications. This is a specification, not an implementation.

Katana: It is an implementation of OWIN by Microsoft.
The Katana component architecture divides an application into four logical layers, as depicted below: host, server, middleware, and application. The component architecture is factored in such a way that implementations of these layers can be easily substituted, in many cases, without requiring recompilation of the application.


Host: The host is responsible for starting and maintaining the process within which the application runs. At present, there are below some primary hosting options for Katana-based applications:

  • IIS/ASP.NET
  • Custom Host/Self Host

Server: The responsibility of the server is to open a network socket, listen for requests, and send them through the pipeline of OWIN components.
A breakdown of the NuGet packages and their purposes are shown in the following diagram.

Middleware: As previously mentioned, when the server accepts a request from a client, it is responsible for passing it through a pipeline of OWIN components, which are specified by the developer’s startup code. These pipeline components are known as middleware.
At a very basic level, an OWIN middleware component simply needs to implement the OWIN application delegate so that it is callable
The middleware class can be easily added to the OWIN pipeline in the application startup code as follows:
public class Startup
{
   public void Configuration(IAppBuilder app)
   {
      app.Use<LoggerMiddleware>(new TraceLogger());

      var config = new HttpConfiguration();
      // configure Web API 
      app.UseWebApi(config);

      // additional middleware registrations      

   }
}

Application: This is your code.

Source Code Example:

Program.cs
namespace BasicOWIN
{
    using System;
    using Microsoft.Owin.Hosting;

    internal class Program
    {
        private static void Main(string[] args)
        {
            using (WebApp.Start<Startup>("http://+:8080"))
            {
                Console.ReadKey();
            }
        }
    }
}

Startup.cs
namespace BasicOWIN
{
    using System.Web.Http;
    using Owin;

    public class Startup
    {
        public void Configuration(IAppBuilder builder)
        {
            var config = new HttpConfiguration();
            config.Routes.MapHttpRoute(
                name: "DefaultApi", 
                routeTemplate: "api/{controller}/{id}", 
                defaults: new { id = RouteParameter.Optional });

            builder.UseWebApi(config);
        }
    }
}

HelloWorldController.cs

namespace BasicOWIN
{
    using System.Web.Http;

    public class HelloWorldController : ApiController
    {
        public IHttpActionResult Get()
        {
            return this.Ok("Hello World");
        }
    }
}