Hosting Nancy from a console application

A few day ago I needed to add some functionality to an old ASP.NET 1.1 web application in Visual Studio 2003. The version of the .NET framework and Visual Studio are irrelevant to the story, but I just wanted to share my pain. What is relevant, is that the web application needed to perform an HTTP POST request from code-behind to an external website.

After I had added the required code to the old web application, I wanted to test this somehow. Maybe there are easier ways to do this, but I decided I’d quickly create a new blank ASP.NET web site and let the old application send the HTTP POST request to the new dummy site.

That was easier said than done. I don’t know why, but IIS was in a foul mood and refused to cooperate with me that day. I guess he had been flirting with a hot looking Apache server the day before and had tried to get frisky with her but she responded with a 411 error. Anyhow, after wrestling with for over an hour and throwing ASP.NET MVC 3 sites and regular ASP.NET WebForms sites at it, IIS only gave me 404 and 403 errors. At this point I was so mad that I didn’t even want to try Cassini or IIS Express.

Instead, I did what should have done a lot earlier. I just created a new console app, used NuGet to add Nancy.Hosting.Self, added a small NancyModule class and I was finished. I literally had a working solution in less than 3 minutes. And that was the first time I had ever created a self hosted instance of Nancy. I’m starting to understand what they mean with the super-duper-happy-path.

Talk is cheap, show me the codes!

No problem! Just start Visual Studio and create a new Console Application. Open the Package Manager Console and enter Install-Package Nancy.Hosting.Self. This will add Nancy and Nancy.Hosting.Self to your application.

Adjust your main method so it looks like this:

static void Main(string[] args)
{
    var nancyHost = new Nancy.Hosting.Self.NancyHost(new Uri("http://localhost:1234"));
    nancyHost.Start();

    Console.ReadLine();
    nancyHost.Stop();
}

Now add a new class called MainModule.cs (you can actually name it whatever you want) and add this code:

public class MainModule : Nancy.NancyModule
{
    public MainModule()
    {
        Get["/"] = x =>
                       {
                           return "Hello world!";
                       };
    }
}

Now run your application, open a browser and surf to http://localhost:1234. Impressive, no?

3 thoughts on “Hosting Nancy from a console application

  1. Neil

    Hi Kristof

    Is it possible to load views?

    I have the console application running, but setting it to return a view simply returns a blank response. I am struggling to find documentation on this.

    Thanks

    Neil

  2. Kristof Post author

    Hi Neil,

    I’m terribly sorry for the late response. Your question got lost between the loads of spam :-(

    In order to load views, you need to add a ViewEngine to your application. There are ViewEngines available on NuGet for Razor, Spark, NDjango and DotLiquid.

    If you want to use Razor views, just enter Install-Package Nancy.Viewengines.Razor in the Packet Manager Console.

Comments are closed.