In this recipe, we see how Nancy separates your application from where it is running. Not only is this a nice separation of concerns but also it allows you to write a Nancy application once, and then go run it in a number of different ways; for example, inside a WPF application, a Windows Service, on top of Katana, on Mono, or on top of ASP.NET.
We will take our TodoNancy
application and pull all the application code out of the ASP.NET project into a separate class library. We will then create a console application and set it up to host our Nancy application. The end result is that we have one Nancy application that we can run either inside a console application or on top of ASP.NET. In fact, we can have it running on both simultaneously as long as we use different ports for different instances.
Open up the code from the last recipe. As always, if you have not been coding along, you can get a copy of the code from the Authenticating users (Intermediate) recipe from the code download.
The following steps help you structure your Nancy application such that it can run under multiple hosts:
To accommodate having one Nancy application that we can host in two different ways, we need to do a little bit of restructuring. Looking at the Solution Explorer in Visual Studio, we want to end up with this structure:
In the previous screenshot, all the application code is in the TodoNancy project, the ASP.NET hosting is in the TodoNancyAspNet, and the self-hosting is in the TodoNancySelfHost. First step towards this structure is to rename the current
TodoNancy
project toTodoNancyAspNet
and also to rename the corresponding file system folder asTodoNancy
.Create a new class library called
TodoNancy
and move all code in the files fromTodoNancyAspNet
exceptweb.config
andpackages.config
to it.Install the Nancy, Nancy.Authentication.WorldDomination, Nancy.ViewEngines.Razor, NLog, mongocsharpdriver, and protobuf-net NuGet packages in
TodoNancy
.Uninstall the Nancy.Authentication.WorldDomination, Nancy.ViewEngines.Razor, NLog, mongocsharpdriver, and protobuf-net NuGet packages from
TodoNancyAspNet
.Add a project reference from
TodoNancyAspNet
toTodoNancy
.At this point, the solution should compile, all tests should run, and the application should work if you run the
TodoNancyAspNet
project. Try it out.Create a new console application called
TodoNancySelfHost
.Install the Nancy.Hosting.Self NuGet in
TodoNancySelfHost
.Add a project reference from
TodoNancySelfHost
toTodoNancy
.Add the following code to
program.cs
inTodoNancySelfHost
:namespace TodoNancySelfHost { using System; using Nancy.Hosting.Self; using TodoNancy; class Program { static void Main(string[] args) { TodosModule artificiaReference; var nancyHost = new NancyHost(new Uri("http://localhost:8080/")); nancyHost.Start(); Console.ReadKey(); nancyHost.Stop(); } } }
Add the following code to
app.config
inTodoNancy
:<appSettings> <add key="MONGOLAB_URI" value="mongodb://localhost:27017/todos"/> </appSettings>
The only thing left to do is to make the self-hosted version work, which allows your user to listen on
locahost:8080
. You can do this with the followingnetsh
command:PS C:\> netsh http add urlacl url=http://+:8080/ user=DOMAIN\USERNAME
If you run the
TodoNancySelfHost
project and go tolocalhost:8080
, you should see the familiarTodoNancy
application.