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:
- ASP.NET IIS hosting pipeline
- 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:
- Fire up VisualStudio
- Add a new project
- A Console Application.
Install the following pre-requisites.
Install-Package Microsoft.Owin.SelfHost
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.
- public class Startup
- {
- public void Configuration(IAppBuilder appBuilder)
- {
-
- appBuilder.Run((owinContext) =>
- {
- owinContext.Response.ContentType = "text/plain";
-
-
-
- return owinContext.Response.WriteAsync("Hello World.");
- });
- }
- }
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.
- public static class Program
- {
- public static void Main(string[] args)
- {
- const string baseUrl = "http://localhost:5000/";
-
- using (WebApp.Start<Startup>(baseUrl))
- {
- Console.WriteLine("Press Enter to quit.");
- Console.ReadKey();
- }
- }
- }
Now you're ready to roll. Run the console application hitting F5 in Visual Studio and you're on.
Let's launch the browser and check the URL where we have our host available for serving:
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
Now of course you need to create a WebAPI controller. So let's do that:
- public class TestController : ApiController
- {
- public int[] GetValues()
- {
- return new int[] { 12, 13, 14, 15 };
- }
- }
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.
- public class Startup
- {
- public void Configuration(IAppBuilder appBuilder)
- {
-
- var configuration = new HttpConfiguration();
-
- configuration.Routes.Add("API Default", new HttpRoute("{Controller}"));
-
-
- appBuilder.UseWebApi(configuration);
-
-
-
- appBuilder.Run((owinContext) =>
- {
- owinContext.Response.ContentType = "text/plain";
-
-
- return owinContext.Response.WriteAsync("Api is availble at: /Test");
- });
- }
- }
So let's open the default page that will serve the normal HTTP content:
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.
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.
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.