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;
    6 
    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();
   14 
   15       Console.ReadKey();
   16 
   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.

12 comments:

  1. Excellent pieces. Keep posting such kind of information on your blog. I really impressed by your blog.
    Android app developer| Android apps development

    ReplyDelete
  2. It is really nice to hear your discussion on specific topic here. I too agree with your points here. keep posting good blogs. Thanks.web hosting provider

    ReplyDelete
  3. Hey
    Frictionless .NET Web App Development with Nancy Part IV - Hosting
    Your website is incredible, thank you in your effort!
    Do you have more great articles like this one
    Thank you!

    ReplyDelete
  4. It was nice to know about how Nancy Hosting dealt with .Net web app development. It seems that you are really working hard to get something fruitful out of this.

    ReplyDelete
  5. I personally love the concept of Nancy and the idea of separating the application from the server. As a .Net developer I am uber tired of the bloated .Net approach to web applications.

    I have been toying with the idea of rebuilding our workwear store using Node.js, my major reservation is that I already have a big investment in Visual Studio and TFS, all of that would be lost moving to node and I would be forced to use eclipse :-(

    Discovering Nancy and this article has given me a new perspective; I am now thinking Nancy could become a serious competitor to Node in terms of time to market and prototyping.

    Looking forward to getting 5 minutes to try it out!

    ReplyDelete
  6. Hi Sir;

    Thanks for your great works, but i wonder how can we start the console in ShortUrlDesktopApp (ShortUrlDesktopApp.exe) then it will run direct to the ShortUrlWebApp?
    Because i want to run the Web Application through the desktop (self host for nancy).

    Thanks you very much.

    ReplyDelete
  7. @Kuong: I'm not sure I understand your question, but if you download the sample from Github and want to run the desktop version (i.e. the selfhosting) from visual studio you have to set the ShortUrlDesktopApp project as startup project. Alternatively just compile the whole solution, find the .exe and run it.

    Notice that, what is started in the main method is just the self hosting and the Nancy framework. The ShortUrlDesktopApp project references the ShortUrl project, which means Nancy will pick up any NancyModules in that project.

    Also notice that the ShortUrlDesktopApp project does not reference the ShortUrlWebApp project, so nothing from the project is started up when the ShortUrlDesktopApp.exe is fired up.

    Hope this helps :-)

    ReplyDelete
  8. Hi @Christian;

    thanks you so much for the answer. Please let me clarify my question. I would like to develop one web application that can run on desktop without configuring the IIS, so i will use nancy selfhost for this project. Then i create one Console Application (selfhost for nancy) and another web project is WebApplication. So when i start double click on SelfHost Project (mydesktopproject.exe) my WebApplication will also start and show the page in WebApplication.

    I am very sorry to bother your time, because i am just a new for nancy.

    Vutha

    ReplyDelete
    Replies
    1. @Christian; i follow your instruction and it work great. But can you guide me how we can stop nancy self host (desktop console) from the WebApplication. ie. I click log off link in my page and it will stop nancy selfhost?

      I really love nancy, and thanks for your time.

      Vutha

      Delete
    2. @Koung You pass the process itself into your bootstrapper and register it in your container as a singleton. That way our modules can get hold of it and they can commit suicide if they really want :-).

      Btw. the Nancy google group (https://groups.google.com/forum/?fromgroups#!forum/nancy-web-framework) is very active and a great place to get these sorts of answers.

      Delete