繼昨天的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 }
這里實例化了一個新的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 }
運行一下,看看效果
正是我們要的結果。下面來看看視圖有沒有問題。
五、建一個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>
同時對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
持續降溫。。注意保暖。。