Self-Hosting OWIN and ASP.Net WebAPI

In this fourth article of the OWIN and Katana series, we'll be discussing the various hosting options available for Katana. As the OWIN specification says, the server and host are separated so we can use a list of types of hosts to host the OWIN middleware, Katana.

The application will be working in a normal hosting environment.

Warning

This article is valid with the version of Microsoft.Owin.SelfHost 2.1.0.

Let's first discuss why Katana was provided with the various hosting types. In the legacy framework of ASP.NET the only available option was using System.Web or IIS. We have already seen the Hosting of Katana in the IIS in our previous posts.

In the initial phase of releases the Katana was supposed to support the following hosting types:

  1. ASP.NET IIS hosting pipeline
  2. OWIN self host

First we'll discuss how to start a self-host for OWIN. As the new release are coming for the Katana the more it's becoming a more charming and easy to use in a way of usability. The documentation of MSDN, in other words the ASP.NET website, is outdated. So I've placed a disclaimer in the start of this article too.

Let's:

  1. Fire up VisualStudio
  2. Add a new project
  3. A Console Application.

Install the following pre-requisites.

Install-Package Microsoft.Owin.SelfHost

Install-Package

Now we need a Startup class that will be identified as the Configuration setup class. The naming is important for this class and should be named Startup. If you're interested in knowing the detection of the Startup for OWIN then read this article on OWIN Startup Class detection.

Here's the supplementary package for you.

  1. public class Startup  
  2. {  
  3.     public void Configuration(IAppBuilder appBuilder)  
  4.     {  
  5.         // Those who are familier with HttpContext, owinContext is just a brother from another mother.  
  6.         appBuilder.Run((owinContext) =>  
  7.         {   
  8.              owinContext.Response.ContentType = "text/plain";  
  9.    
  10.              // here comes the performance, everythign in the Katana is Async. Living in the current century.  
  11.              // Let's print our obvious message: :)  
  12.              return owinContext.Response.WriteAsync("Hello World.");  
  13.        });  
  14.     }  
  15. }  
For more options, Install-Package Owin.Extensions.

This will provide you many extension methods with various options to invoke the Usage, StageMarker and Map functions. Go as you like.

Now let's add some code to the Main function to start the code.

  1. public static class Program  
  2. {  
  3.     public static void Main(string[] args)  
  4.     {  
  5.         const string baseUrl = "http://localhost:5000/";  
  6.    
  7.         using (WebApp.Start<Startup>(baseUrl))  
  8.         {  
  9.             Console.WriteLine("Press Enter to quit.");  
  10.             Console.ReadKey();  
  11.         }  
  12.     }  
  13. }  

 Now you're ready to roll. Run the console application hitting F5 in Visual Studio and you're on.

run program

Let's launch the browser and check the URL where we have our host available for serving:

launch the browser

There it is. No IIS, no System.Web, just an individual recipe for you.

Now let's do something interesting. Katana is the best support for SingalR and WebAPI until now because both of these technologies are completely independent of System.Web, in other words ASP.NET IIS.

To host an WebAPI in our custom host just use the following procedure:

Install the following pre-requisites.

Install-Package Microsoft.Aspnet.WebApi

Install-Package Microsoft.Aspnet.WebApi

Now of course you need to create a WebAPI controller. So let's do that:

  1. public class TestController : ApiController  
  2. {  
  3.     public int[] GetValues()  
  4.     {  
  5.         return new int[] { 12, 13, 14, 15 };  
  6.     }  
  7. }  
That's not all, It's all about Nuget; don't leave the cart, we're still travelling to the shore.

Install the WebAPI support via Nuget.

Install-Package Microsoft.AspNet.WebApi.Owin

Now go back to the Startup.cs and add few configuration that, of course, is required to start a WebAPI.

  1. public class Startup  
  2. {  
  3.     public void Configuration(IAppBuilder appBuilder)  
  4.     {  
  5.         // Setup WebAPI configuration  
  6.         var configuration = new HttpConfiguration();  
  7.    
  8.         configuration.Routes.Add("API Default"new HttpRoute("{Controller}"));  
  9.    
  10.         // Register the WebAPI to the pipeline  
  11.         appBuilder.UseWebApi(configuration);  
  12.    
  13.         // let's keep the old stuff too....   
  14.         // Those who are familier with HttpContext, owinContext is just a brother from another mother.  
  15.          appBuilder.Run((owinContext) =>  
  16.          {   
  17.              owinContext.Response.ContentType = "text/plain";    
  18.              // here comes the performance, everythign in the Katana is Async. Living in the current century.  
  19.              // Let's print our obvious message: :)  
  20.              return owinContext.Response.WriteAsync("Api is availble at:  /Test");  
  21.         });  
  22.     }  
  23. }  
So let's open the default page that will serve the normal HTTP content:

open the default page

I have used simply the controller name to invoke the WebAPI controller function. Which make sense for the demo. Now let's try this in Chrome, because it will ask you for the action to save the JSON format file. Our URL will be:

http://localhost:5000/Test.

WebAPI controller function

But this not JSON, right? OK, I'm gonna provide an interesting tip to open any WebAPI method that returns JSON. If you have Git Bash installed, then launch it and run the following command CURL with the URL of your WebApi URL. This supports invoking any Web HTTP request that gives JSON in response in a formatted result as output.

formatted result as output

I hope you would love this way to get the JSON in a formatted way. So I enjoyed writing this article, I hope you enjoyed reading it too.

See you on the other side of OWIN. Give a holler.


Similar Articles