Running ASP.NET Web API via OWIN with Katana

Strath

December 17th, 2012

Running ASP.NET Web API with OWIN and Katana

Because you don't need IIS

One of the often repeated, and frankly, somewhat unfair, arguments against using .NET based technologies is that by doing so, you handcuff yourself to all kinds of various Microsoft tools and products.

If you choose to use ASP.NET Web API, by no means you are tied to IIS. Of course, one of the options is to self host it (and run the web service using the hardened WCF core), but if you want a true web host flexibility and independence, you can very easily host Web API using OWIN and Katana.

More after the jump.

OWIN and Katana

OWIN, short for the Open Web Interface for .NET, defines a common interface that decouples web apps from web servers. You can learn more about OWIN here. The primary goal of OWIN is to separate web app and a host, meaning that you can run your application on any OWIN-compatible platform.

Katana, on the ther hand, is an OWIN implementation for Microsoft hosts (HttpListener, System.Web) and the Web API framework.

If you want to learn more about OWIN and Katana, make sure to follow Louis DeJardin and Chris Ross, two of the smartest guys out there.

Getting started with Katana

To install Katana, we will need Chocolatey and PowerShell.

Once executed, Chocolatey is available on your machine (under c:/chocolatey), and its commands are globally available through the PATH variable we just set (you may need to restart PowerShell).

You can now execute the Chocolatey install command to install latest build of Katana:

This will get you the latest version, 0.16rc and it will make the command “Katana” available in Powershell/CMD all across your environment. You can now navigate to the directory where your Web API application resides and simply execute Katana command to start the host.

But before you do that, you need one more configuration piece.

Configuring your app

You need a simple Startup.cs to tell Katana what to run. In our case, it will contain the HttpConfiguration that our Web API needs.

Really all you need here, is to register at least one route so that you can use the app – but you are free to do any kind of configuration changes that your app needs (formatters, services, filters and whatnot).

You will also need a reference to Owin.dll, this you can get from Nuget – Microsoft.AspNet.WebApi.Owin package.

This command should add the following packages to your project:
– Owin 1.0
– Microsoft.AspNet.WebApi.Owin 0.12-alpha2′.

Now you can compile your application, return to PowerShell, navigate to the base directory and type Katana. This will trigger the Katana.bat from Choclatey and start the Katana host. You can navigate in the browser to the default URL http://localhost:8080/ where your Web API application is now running and is available.

Katana will expect your assemblies to be under a /bin folder, so it’s important you trigger the above command from the Web API app base folder.

Voila, your app is running on Katana through OWIN HttpListener server. A small note here, HttpListener implementation is .NET 4.5 only.

Alternative option – run on top of System.Web

You can also run using Katana’s System.Web implementation. You can do that using exactly the same configuration and code base as before, the only change you need is to add the folloing entry to the web.config.

Now if you run from Visual Studio (F5), Katana will handle the requests.

Summary

Katana & OWIN are very healthy and attractive options for the .NET community. A thriving open source ecosystem is necessary for a technology/platform to grow, and OWIN is slowly emerging as an important specification helping us developers build applications that are decoupled from the servers.

Because we don’t have to use IIS or Cassini!

If you want to follow more on the OWIN/Katana development, join the Google group .NET HTTP Abstractions.

Be Sociable, Share!