OWIN self-hosting using Nancy

One of the problems surrounding OWIN self-hosting currently, is that MVC is not compatible. Due to System.Web's tight integration with IIS.

vNext will solve these issues, but what about now? I want the portability of OWIN self-hosting (using HttpListener) but I still have a need for a server side MVC framework to build my application.

The primary reason for this post is to make anybody interested in OWIN aware that you do have options.

I've looked into OWIN in the past as a host for api's, but after attending an awesome talk on OWIN recently by Steve Higgs it inspired me to experiment further.

So you want to use OWIN? you want to un-hook your application from IIS and be portable? However you still need an MVC framework? In comes Nancy.

Nancy

NancyFx is a lightweight framework inspired by the Rails framework Sinatra. That guys daughter is called Nancy - see that? Scott Hanselman has a great post introducing Nancy.

Nancy has been around for quite some time, but I’ve only recently stumbled across it. Compared to ASP MVC it's very lightweight. It's also modular so you only download and include the packages you need, you don't have to drag around a great big dll full of stuff you probably aren't using.

As soon as you begin to use Nancy, you can see it's very convention over configuration. How you take this news is entirely subjective. It's not all convention however, only by default; Nancy still gives you the option to override default configuration classes.

Similarities to .NET MVC

If you're coming from the .NET MVC world and visiting Nancy then there are some similarities you can make which will make learning quicker. I'll outline the basics to get you started.

Modules

Modules are basically controllers. Making a module is simple, assuming you're using the default settings; in the root of your project create a Modules directory. This is where Nancy will begin to look for your modules.

A module looks like this:

public class HomeModule : NancyModule
	{
		public HomeModule()
		{
			Get["/"] = _ =>
			{
            	return View["Home"];
			}	
		}
	}

It's easy to see what's going on here. Each NancyModule has a RouteBuilder property for each http verb. Take the above example - When we do a get request to and pass in the url request path to match, our lambda will be called - which looks for a view called "Home" and builds the response.

Views

Again, similar to .NET MVC, Nancy (by default) will look for views in a View directory.

When it comes to choosing a view engine, you also aren't short of choice. Nancy supports all the big players, the most notable being for this article being Razor. Nancy even offers a way to implement/plug in your own view engine.

Nancy ViewEngine NuGet packages


What we need from OWIN

class Program
{
	static void Main(string[] args)
	{
		string uri = "http://localhost:1234/";

		using (WebApp.Start<Startup>(uri))
		{
			Console.WriteLine("Started");
			Console.ReadKey();
			Console.WriteLine("Stopping");
		}
	}
}

public class Startup
{
	public void Configuration(IAppBuilder app)
	{
		app.UseNancy();
	}
}

That's about it. We tell our application to use the Nancy middleware. Done.

You can find out more about Nancy and it's uses from their github wiki.