Swap Your WebHost With OWIN / Katana

By | July 10, 2013

The OWIN (Open Web Interface for .NET) specification defines how to decouple web applications from the web server. Detailed information you can get here: http://owin.org Because it’s specification, you won’t find any applications or libraries. One of the concret impelmentations of OWIN is called Katana. Katana is developed by Microsoft and the Community. The code is available on Codeplex. http://katanaproject.codeplex.com
Here is a picture of the Katana components.
Katana

Server The server handles the requests. There are two libraries for now. SystemWeb and HttpListener. And the third (WebListener) is coming soon and it is a light-weight server for high performance!
Host You can host your application in IIS, OwinHost.exe or in a custom application/console/service

The cool thing is. You can host your application on “IIS/SystemWeb” or “OwinHost.exe/HttpListener” or “OwinHost.exe/WebListener” etc.

I don’t want to example OWIN/Katana detail. On Channel9 is a good video about OWIN.

Here is an example of an simple application.
The Configuration method is called by the OWIN host. When I start the application the response will be “Hello world {DateTime}”

public class Startup
{
    public void Configuration(IAppBuilder app)
    {
        app.UseHandlerAsync((request, response) => 
        {
            response.ContentType = "text/plain";
            return response.WriteAsync("Hello world " + DateTime.Now.ToString()  + " !");
        });
    }
}

Now let’s “enable” WebAPI in the application.

public class Startup
{
    public void Configuration(IAppBuilder app)
    {
        var config = new HttpConfiguration();
        config.Routes.MapHttpRoute(name: "Default API", routeTemplate: "{controller}");
        app.UseWebApi(config);
    }
}

public class HelloWorldController : ApiController
{
    public string[] Get()
    { 
        return new string[] { "Hello", "World", DateTime.Now.ToString() };
    }
}

Now the application provides only WebAPI support. The cool thing is, with OWIN we enable only the features we really need. And so we have less overhead in runtime. By default nothing is configured/enabled.

You can play with my solution: https://github.com/dmicic/Dmicic.Example.Owin.Basic
Why I used a “Empty Web Project” template:
1) You can start the application with F5.
2) OWIN excepts the DLLs in the bin folder and not bin/debug or bin/release.

My application runs on WebAPI, HttpListener, OwinHost.exe

You can start the application with this command from command line: ..\packages\OwinHost.1.1.0-beta2\tools\OwinHost.exe (you have to be in the Dmicic.Example.Owin.Basic.Web project folder.)

Test url: http://localhost:5000/HelloWorld