OWIN and Katana - what's the big deal?

 Date: September 20, 2013

OWIN stands for The Open Web Interface for .NET. It is a standard for communication between .NET web servers and web applications. It defines required elements for HTTP request (details). It is inspired by Rack from Ruby on Rails World. Katana is implementation of this standard. We can say that it is a lightweight web server for .NET. In fact, it is more than that (more info here).

Demo

First, we need to create application project. Let's create 'Empty Web Application' (it might be also Console App).

OWIN - empty Project

Next, we will install two NuGet packages (using Package Manager Console):

Install-Package Microsoft.Owin.Host.SystemWeb

Install-Package Owin.Extensions

Then, we need to create 'Startup class'.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Owin;

namespace OwinDemo
{
    public class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            app.UseHandlerAsync((req, res) =>
            {
                res.ContentType = "text/plain";
                return res.WriteAsync('Hello Katana!");
            });
        }
    }
}

Now we are ready to run our web server, but you may get following error:

OWIN - error

Fortunately there is easy fix for that. You need to modify Web.config file, adding following code in configuration section:

<appSettings>
    <add key="owin:HandleAllRequests" value="true"/>
</appSettings>

Then you can run server (CTRL+F5) and you should see:

OWIN - Hello Katana

Summary

So, what is big deal? We have web server in 7 lines of code! We do not need IIS as only one, right choice.

Of course we can do much more sophisticated things. Such as routing, WebAPI or even SignalR. You can also debug it easily.

More info about OWIN and Katana on ASP.NET website: An Overview of Project Katana
There is also screencast on Channel9: The Katana Project - OWIN for ASP.NET (it shows e.g. how to use WebAPI from 35:40).
Here is very nice article how to use SignalR with Katana.

Katana is Open Source and available on CodePlex.

 Tags:  programming

Previous
⏪ scriptcs - C# in console

Next
Polish Coffee Hour ⏩