We create the first application on NancyFX. Part Six Nancy.Selfhosting

    At the end of the series, I would like to describe such an important advantage of NancyFX as Nancy.SelfHosting. This Nancy module allows us to host our application without using IIS, on those operating systems where there is .NET or MONO. Let's look at the following example of using this module. First, create an empty console application.



    Then add to the application using NuGet Nancy and Nancy.Hosting.Self.



    Add a module class with the following code to our application.

    using Nancy;
    namespace NancySelfHosting
    {
        public class NancyFXModule : NancyModule
        {
            public NancyFXModule()
            {
                Get["/"] = param => "I'm Nancy Self Host Application.";
            }
        }
    }
    


    Next, we modify the Main method of the Program class as follows

    using System;
    using Nancy.Hosting.Self;
    namespace NancySelfHosting
    {
        class Program
        {
            static void Main(string[] args)
            {
                var nancyHost = new NancyHost(new Uri("http://localhost:1234"));
                nancyHost.Start();
                Console.WriteLine("Service started!");
                Console.ReadLine();
                nancyHost.Stop();
                Console.WriteLine("Service stoped!");
            }
        }
    }
    


    And run our application. In the console that appears, we will see the following:



    Next, launch the browser and go to localhost : 1234 /. We will see the following picture:



    As you can see, now we have a full-fledged web application for which we do not need installed and configured IIS. And for the implementation of this application we needed just a few minutes.

    Since this article was the final article in the series, I want to thank comrades lexkazakov, kekekeks and others who helped me in creating this series of articles for their reviews and advice. Many thanks to all the people who followed these articles. I hope I did not disappoint you with my opus.

    Also popular now: