Book Image

Instant Nancy Web Development

By : Christian Horsdal
Book Image

Instant Nancy Web Development

By: Christian Horsdal

Overview of this book

Nancy is a C# web framework which aims to provide you ,the application developer, with an easy path to follow, where things will naturally fall into place and work. Nancy is a powerful, flexible, and lightweight web framework that allows you to get on with your job. Instant Nancy Web Development will give Readers practical introduction to building, testing, and deploying web applications with Nancy. You will learn how to take full advantage of Nancy to build clean application code, and will see how this code lends itself nicely to test driven development. You will also learn how to hook into Nancy to easily extend the framework. Instant Nancy Web Development offers you an in-depth exploration of all the major features of the Nancy web framework, from basic routing to deployment in the Cloud, and from model binding to automated tests. You will learn how to build web applications with Nancy and explore how to build web sites using Razor views. Next, you will learn how to build web based APIs suitable for JavaScript clients, mobile clients, and even desktop applications. In fact, you will learn how to easily combine the two into one. Finally, you will learn how to leverage Nancy to write clean and maintainable web applications quickly.
Table of Contents (7 chapters)

Separating applications and hosting (Advanced)


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.

Getting ready

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.

How to do it...

The following steps help you structure your Nancy application such that it can run under multiple hosts:

  1. 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:

  2. 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 to TodoNancyAspNet and also to rename the corresponding file system folder as TodoNancy.

  3. Create a new class library called TodoNancy and move all code in the files from TodoNancyAspNet except web.config and packages.config to it.

  4. Install the Nancy, Nancy.Authentication.WorldDomination, Nancy.ViewEngines.Razor, NLog, mongocsharpdriver, and protobuf-net NuGet packages in TodoNancy.

  5. Uninstall the Nancy.Authentication.WorldDomination, Nancy.ViewEngines.Razor, NLog, mongocsharpdriver, and protobuf-net NuGet packages from TodoNancyAspNet.

  6. Add a project reference from TodoNancyAspNet to TodoNancy.

  7. 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.

  8. Create a new console application called TodoNancySelfHost.

  9. Install the Nancy.Hosting.Self NuGet in TodoNancySelfHost.

  10. Add a project reference from TodoNancySelfHost to TodoNancy.

  11. Add the following code to program.cs in TodoNancySelfHost:

    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();
        }
      }
    }
  12. Add the following code to app.config in TodoNancy:

    <appSettings>
      <add key="MONGOLAB_URI" value="mongodb://localhost:27017/todos"/>
    </appSettings>
  13. 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 following netsh command:

    PS C:\> netsh http add urlacl url=http://+:8080/ user=DOMAIN\USERNAME
  14. If you run the TodoNancySelfHost project and go to localhost:8080, you should see the familiar TodoNancy application.