Tuesday, November 8, 2011

Frictionless .NET Web App Development with Nancy Part IV - Hosting

Continuing my URL shortener sample (part I, II, III), I'll talk about one the things Nancy does quite differently from your average web framework: Nancys concept of hosting. At this point you may be thinking serves or clouds or storage etc, but that's not exactly what Nancy hosting is about.

Nancy Hosting
Nancy hosting is about which stack Nancy runs on top of. Such stacks include ASP.NET, WCF, OWIN, and self hosting. That means that a Nancy app can be run on top of any of those four hosts. You decide. So if you have an existing ASP.NET/IIS setup you can use it to run Nancy, if you have a WCF setup already you can use that.

The two last hosts may need a bit of introduction:

OWIN is Open Web Interface for .NET, which is an attempt to create a common interface between web frameworks and web servers. Currently it's not implemented by that many web servers, but could potentially allow web frameworks and the apps built on them to be move unchanged from one web server to another. One example of an OWIN compliant web server is Kayak, which can run on Mono. Combining these things means that our Nancy app can run on a stack of Nancy/Hosting.OWIN/Kayak/Mono/Linux. In other words a web app written in C# running on OSS all the way down. Who'd have thunk!

Self hosting is another interesting option: Self hosting means running a Nancy app in any .NET or Mono executable. This means that you can slap a web interface onto a desktop app or a windows service. Both of these possiblities are very interesting IMO.

Multiple Hosts - One Code Base
In the URL shortener sample I'd like to use the same application in two different settings: In a stand alone executable (just for kicks) and in ASP.NET stack. To do that I add two projects to my solution, one for each host:

First lets look at bit closer at the ASP.NET hosting project. I.e. the one called ShortUrlWebApp. It's simply an empty web project, as created by Visual Studio, with the Nancy.Hosting.Aspnet NuGet package and a project reference to ShortUrl added. Not one line of code wirtten. The project contains:

The Nancy.Hosting.Aspnet NuGet rewrites the web.config to make Nancy handle all incoming request. Nancy automatically picks up the Bootstrapper and Module in the ShortUrl assembly. That's all there is to running our url shortener on top of ASP.NET.

Second lets loot at the self hosting project. I.e. the one called ShortUrlDesktopApp. It's just a console application with a project reference to ShortUrl and the Nancy.Hosting.Self NuGet added:

In this case a little bit of work is needed in the program.cs - but not much, just instantiating and starting the NancyHost:

    1 namespace ShortUrlDesktopApp
    2 {
    3   using System;
    4   using Nancy.Hosting.Self;
    5   using ShortUrl;
    7   class Program
    8   {
    9     static void Main(string[] args)
   10     {
   11       ShortUrlModule artificiaReference;
   12       var nancyHost = new NancyHost(new Uri("http://localhost:8080/"));
   13       nancyHost.Start();
   15       Console.ReadKey();
   17       nancyHost.Stop();
   18     }
   19   }
   20 }

With this setup we can run the app on ASP.NET of from a console app. There is no difference in the browser and the application code is the same. This - IMO - is very cool indeed.

And the code is still on GitHub.