📜 ⬆️ ⬇️

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

At the end of a series of articles 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 consider the following example of using this module. First, create an empty console application.



Then add to applications using NuGet Nancy and Nancy.Hosting.Self.
')


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

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


Next, we modify the Main method, the Program class in the following way.

 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, run 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 to install and configure IIS. And for the implementation of this application, we needed just a few minutes.

Since this article was the final article of the cycle, I want to thank the comrades lexkazakov, kekekeks and others who helped me in creating this cycle 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 opuses.

Source: https://habr.com/ru/post/203350/


All Articles