原文: Nancy之基于Nancy.Hosting.Self的小Demo 繼昨天的 Nancy之基于Nancy.Hosting.Aspnet的小Demo
後,
今天來做個基于Nancy.Hosting.Self的小Demo。
關于Self Hosting Nancy,官方文檔的介紹如下
https://github.com/NancyFx/Nancy/wiki/Self-Hosting-Nancy 文檔具體的内容我就不一一翻譯了,主要是示範從頭到尾的一個過程,然後看看Nancy.Hosting.Self的源碼一、建立一個控制台應用程式(Console Application)
二、通過NuGet添加我們需要的Nancy包

這裡我們可以直接添加Nancy.Hosting.Self,添加這個會順帶添加Nancy。
到這裡我們的基本工作就KO了。
三、打開Program.cs,開始寫代碼了
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已啟動。。");
9 try
10 {
11 Console.WriteLine("正在啟動 http://localhost:8888/ ");
12 System.Diagnostics.Process.Start("http://localhost:8888/");
13 Console.WriteLine("成功啟動 http://localhost:8888/ ");
14 }
15 catch (Exception)
16 {
17 }
18 Console.Read();
19 }
20 Console.WriteLine("http://localhost:8888 已經停止 \n NancySelfHost已關閉。。");
21 }
22 }
Program.cs
這裡執行個體化了一個新的NancyHosting,并直接用Process.Start打開了一個網頁。
這樣做是為了省時省力偷下懶,不用在啟動程式之後再手動去打開浏覽器去輸入http://localhost:8888
如果不熟悉Process,可以看一下這個
https://msdn.microsoft.com/en-us/library/e8zac0ca(v=vs.110).aspx四、建立一個Modules檔案夾,用來存放我們的Modules
在Modules檔案夾建立一個HomeModule.cs
1 public class HomeModule:NancyModule
2 {
3 public HomeModule()
4 {
5 Get["/"] = _ => "I'm from Nancy.Hosting.Self!";
6 }
7 }
HomeModule.cs
運作一下,看看效果
正是我們要的結果。下面來看看視圖有沒有問題。
五、建一個Views檔案夾,用于存放視圖
建立Home檔案夾,建立index.html,這裡我們就不用Razor了,Nancy支援多種視圖引擎!!這個很不錯。
1 <!DOCTYPE html>
2 <html lang="en" xmlns="http://www.w3.org/1999/xhtml">
3 <head>
4 <meta charset="utf-8" />
5 <title>NancyDemo</title>
6 </head>
7 <body>
8 <p style="font-size:xx-large">SelfHostingDemo</p>
9 </body>
10 </html>
index.html
同時對HomeModule.cs進行修改
1 public class HomeModule:NancyModule
2 {
3 public HomeModule()
4 {
5 Get["/"] = _ =>
6 {
7 return View["index"];
8 };
9
10 }
11 }
運作試試。oh no~~ 出錯了。。。
為什麼會出現錯誤呢?不應該啊!!
既然有錯誤,就要排除錯誤,看看它說那裡有問題:
Nancy.RequestExecutionException: Oh noes! ---> Nancy.ViewEngines.ViewNotFoundException: Unable to locate view 'index'
Currently available view engine extensions: sshtml,html,htm
Locations inspected: views/Home/index-zh-CN,views/Home/index,Home/index-zh-CN,Home/index,views/index-zh-CN,views/index,index-zh-CN,index
Root path: D:\GithubCode\Demos\NancyDemoWithSelfHosting\SelfHostingDemo\SelfHostingDemo\bin\Debug
If you were expecting raw data back, make sure you set the 'Accept'-header of the request to correct format, for example 'application/json'
提示的居然是沒有找到視圖!!再細細看一下就會發現問題了。Root path!!!!
視圖應該在放到Debug目錄下,這裡建的是控制台應用程式,不是web應用程式。
是以就把Views檔案夾搬家到Debug下面。
運作看看,OK,成功了
六、把這個demo放到linux下看看
在 /var/www/ 下面建立一個檔案夾 mkdir nancydemo
将程式bin目錄下的檔案傳到 /var/www/nancydemo 中,ls看一下裡面的内容
執行 mono SelfHostingDemo.exe
看看效果,OK!
到這裡,已經完成了一個簡單的Demo了。
趁着時間還早,看看Nancy.Hosting.Self的内部實作,源碼位址:
https://github.com/NancyFx/Nancy/tree/master/src/Nancy.Hosting.Self
還記得否?我們的Program.cs中有用到這個類----NancyHost
var nancySelfHost = new NancyHost(new Uri("http://localhost:8888/"))
細細看看這個類裡面有什麼東西。
https://github.com/NancyFx/Nancy/blob/master/src/Nancy.Hosting.Self/NancyHost.cs
有六個重載,其實這六個重載都是為了初始化 NancyHost ,有三個是用了預設配置,有三個是用了自定義配置。
我們用到的NancyHost是采用的預設配置,參數就是一個可變的Uri數組。
然後看看Start 方法
主要是監聽我們的請求,這個監聽過程主要用到了HttpListener,還有異步回調。
裡面的 Task.Factory.StartNew 可以看看msdn的介紹
https://msdn.microsoft.com/en-us/library/dd321439(v=vs.110).aspx持續降溫。。注意保暖。。