Nancy's Demo Based on Nancy. Hosting. Self, nancy. hosting. self

Source: Internet
Author: User

Nancy's Demo Based on Nancy. Hosting. Self, nancy. hosting. self

Following the Demo of Nancy. Hosting. Aspnet,

Today, we will create a Demo Based on Nancy. Hosting. Self.

For information about Self Hosting Nancy, the official documentation is as follows:

Https://github.com/NancyFx/Nancy/wiki/Self-Hosting-Nancy

I will not translate the specific content of the document one by one, mainly to demonstrate a process from start to end, and then look at the source code of Nancy. Hosting. Self.

1. Create a Console Application 2. Add the Nancy package we need through NuGet

Here, we can directly add Nancy. Hosting. Self, which will be followed by Nancy.

Here, our basic work is KO.

3. Open Program. cs, started to write the code 1 class Program 2 {3 static void Main (string [] args) 4 {5 using (var nancySelfHost = new NancyHost (new Uri ("http: // localhost: 8888/") 6 {7 nancySelfHost. start (); 8 Console. writeLine ("NancySelfHost is started .. "); 9 try10 {11 Console. writeLine ("Starting http: // localhost: 8888/"); 12 System. diagnostics. process. start ("http: // localhost: 8888/"); 13 Console. writeLine ("http: // localhost: 8888/" successfully started); 14} 15 catch (Exception) 16 {17} 18 Console. read (); 19} 20 Console. writeLine ("http: // localhost: 8888 has stopped \ n NancySelfHost has been disabled .. "); 21} 22}Program. cs

Here, a new NancyHosting is instantiated and a webpage is opened directly with Process. Start.

In this way, you do not need to manually open the browser after starting the program to enter http: // localhost: 8888

If you are not familiar with Process, you can take a look at this

Https://msdn.microsoft.com/en-us/library/e8zac0ca (v = vs.110). aspx

4. Create a new Modules folder to store our Modules

Create a HomeModule. cs in the Modules folder.

1 public class HomeModule: NancyModule2 {3 public HomeModule () 4 {5 Get ["/"] = _ => "I'm from Nancy. Hosting. Self! "; 6} 7}HomeModule. cs

Run the command to check the effect.

The result is exactly what we want. Let's see if the view has any problems.

5. Create a view folder for storing Views

Create a new homefile folder and create a new index.html. We don't need Razor here. Nancy supports multiple view engines !! This is good.

1 <! DOCTYPE html> 2 Modify HomeModule. cs

1 public class HomeModule: NancyModule 2 {3 public HomeModule () 4 {5 Get ["/"] ==> 6 {7 return View ["index"]; 8 }; 9 10} 11}HomeModule. cs

Run it. Oh no ~~ Error...

Why is an error? No !!

Now that there is an error, we need to eliminate the error and see it says there is a problem:

Nancy. RequestExecutionException: Oh noes! ---> Nancy. viewEngines. viewNotFoundException: Unable to locate view 'index' Currently available view engine extensions: sshtml, html, htmLocations inspected: views/Home/index-zh-CN, views/Home/index, home/index-zh-CN, Home/index, views/index-zh-CN, views/index, index-zh-CN, indexRoot path: D: \ GithubCode \ Demos \ NancyDemoWithSelfHosting \ SelfHostingDemo \ bin \ DebugIf you were expecting raw data back, make sure you set the 'accept'-header of the request to correct format, for example 'application/json'

The view is not found !! After a closer look, we will find the problem. Root path !!!!
The view should be placed in the Debug directory, where the console application is created, not the web application.
So we moved the Views folder to the Debug directory.
Run the command. OK. The operation is successful.

6. Put this demo in linux.

Create a folder mkdir nancydemo under/var/www /.
Upload the files in the bin directory of the program to/var/www/nancydemo. ls will check the content.

Run mono SelfHostingDemo.exe.
Check the effect. OK!


Here, a simple Demo has been completed.

Take a look at the internal implementation of Nancy. Hosting. Self. The Source Code address is as follows:
Https://github.com/NancyFx/Nancy/tree/master/src/Nancy.Hosting.Self

Do you still remember? This class is used in our Program. cs ---- NancyHost
var nancySelfHost = new NancyHost(new Uri("http://localhost:8888/"))
Take a closer look at what is in this class.
Https://github.com/NancyFx/Nancy/blob/master/src/Nancy.Hosting.Self/NancyHost.cs

There are six reloads. In fact, these six reloads are used to initialize the NancyHost. Three of them use the default configuration and three use the custom configuration.

The NancyHost we use adopts the default configuration. The parameter is a variable Uri array.

Then let's look at the Start method.

The listener mainly listens to our requests. The Listener mainly uses HttpListener and asynchronous callback.

In the Task. Factory. StartNew, you can see the introduction of msdn.

Https://msdn.microsoft.com/en-us/library/dd321439 (v = vs.110). aspx

 

Continuous cooling .. Keep warm ..

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.