Thursday, January 16, 2014

Self Hosting ASP.Net Web Api Service with OWIN - Katana

In the previous post, we looked at hosting ASP.Net Web API application using Microsoft.Aspnet.WebApi.SelfHost package. With this post, we are going to start looking at how we can host the same service using Katana. It's an interesting name, isn't it? Already feeling like a Samurai :)

OWIN is a Microsoft's specification for web applications. Open Web Interface for .Net. OWIN specification provides layers of host, server, framework and your application. So the same host and server can be used to host a number of applications with different frameworks. They would be part of same pipeline. So we can also hookup infrastructure and security components on the front re-using them for all of these applications. Since this is a composable architecture, you are building the whole pipeline. This is unlike other hosting stories.

As stated by Microsoft, Katana is an OWIN implementation by Microsoft. It is an attempt to decouple web applications from servers. This should enable hosting of web applications including Web API, Signal R and front-end applications without requiring IIS. This should allow a combination of hosts and servers for hosting an application developed using a particular framework including ASP.Net Web API.

Let's create a console application for hosting InstituteService we have been continuously developing. We name the project as InstituteService.Host.Console.Owin. It's just a three steps process after this.

Step I: Let's first install the required Nuget package required for OWIN Self Hosting. Here we are installing Microsoft.AspNet.WebApi.OwinSelfHost package.

As is general with Nuget packages, it should install all the required Nuget packages for the package installation.

Although OWIN is specified as a specification, there is also Owin.dll. The assembly just contains IAppBuilder. In order to host it, we need to implement a type. Here we have a type named Startup. As a convention, the type needs to have a method Configuration with a single parameter of type IAppBuilder. This is Step II. Actually, you compose the whole application stack here. Here UseWebApi is an extension method defined for IAppBuilder. This is the whole idea, all application you want to host would involve a nuget package installation for the framework. The package would generally provide an extension method like we have UseWebApi here. The method is passed with an HttpConfiguration instance. We are configuring it for attribute routing.

For Step III, now we need to host the service. The package provides WebApp type. The type provides a generic method Start with a type parameter as the type declared above.

And that's it!!! Now we can run the application. It would host the service and wait for a key to be pressed, then it exits the application. It shows up as the following console screen:

Here we are accessing the service. Since we configured the application with Attribute Routing, it would determine the correct action based on controller's route prefixes. Since we have requested the response in text/json format, the content-negotiation with server with the server would result in the same format. Please remember that ASP.Net Web API JSON.Net for this purpose by default (if I remember it right).

1 comment:

a said...

Hi. Great blog, but there is something technical which is somewhat spoils the pleasure of reading it. the left panel of your blog - where is the timeline, is "alzheimer" - it vibrates up and down with a very noticeable amplitude, at least on chrome on windows on 1920/1080 screen. I know it sounds weird but it indeed vibrates... Is there anything you can do with it, or maybe I can do something with chrome settings that it won't be vibrating?