Running ASP.NET Web API with OWIN and Katana

Β· 691 words Β· 4 minutes to read

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.

@powershell -NoProfile -ExecutionPolicy unrestricted  
-Command "iex ((new-object net.webclient).DownloadString('http://bit.ly/psChocInstall'))"  
&& SET PATH=%PATH%;%systemdrive%chocolateybin 

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:

cinst katana -source "http://www.myget.org/F/Katana/" -pre -f  

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.

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.UseHttpServer(config);  
}  
}  

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.

PM> Install-Package Microsoft.AspNet.WebApi.Owin -Pre  

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.

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

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.

About


Hi! I'm Filip W., a software architect from ZΓΌrich πŸ‡¨πŸ‡­. I like Toronto Maple Leafs πŸ‡¨πŸ‡¦, Rancid and quantum computing. Oh, and I love the Lowlands 🏴󠁧󠁒󠁳󠁣󠁴󠁿.

You can find me on Github, on Mastodon and on Bluesky.

My Introduction to Quantum Computing with Q# and QDK book
Microsoft MVP