<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # ASP.NET 開發必備知識點(1):如何讓Asp.net網站運行在自定義的Web服務器上 ## 一、前言 大家都知道,在之前,我們Asp.net 的網站都只能部署在IIS上,并且IIS也只存在于Windows上,這樣Asp.net開發的網站就難以做到跨平臺。由于微軟的各項技術的開源,所以微軟自然要對跨平臺做出支持的。OWIN技術就可以使得Web 服務器不再依賴于IIS,從而使得Asp.net 網站不再依賴于Windows。是不是有了OWIN,就不需要安裝MONO就可以實現跨平臺呢?顯然不是,有了OWIN要實現跨平臺還是要依賴與MONO,因為MONO提供了在Liunx環境下.NET代碼的運行環境,而OWIN只是分離了Web應用程序與Web Server之間的緊耦合罷了。 ## 二、使Asp.net網站跨平臺成為可能的機制——OWIN 前面我們已經引出了使得Asp.net網站跨平臺成為可能的機制就是OWIN,下面讓我們具體看看什么是OWIN。 OWIN全稱是——Open Web Interface For .NET。從名字上可以看出,它是一套接口定義,它完整定義如下: OWIN在.NET Web Servers與Web Application之間定義了一套標準接口,OWIN的**目標是用于解耦Web Server和Web Application**。基于此標準,鼓勵開發者開發簡單、靈活的模塊,從而推進.NET Web Development開源生態系統的發展。 至于為什么需要OWIN,在前面部分已經介紹過了,就是為了使得Web Application和Web Server解耦,這樣就可以使得Asp.net 網站不再依賴與IIS Web Server,從而就不會緊耦合與Windows 操作系統了。(看到這里,你是不是和我學習OWIN有一樣的疑問呢?問題是:之前沒有OWIN規范不是照樣可以通過Mono來實現asp.net 網站的跨平臺嗎?現在還需要OWIN干什么的?) 對于這個上面的疑問,我后面給出答案。既然OWIN是一套規范,則自然有它定義規范了。OWIN規范中定義了4個組件: ![](https://box.kancloud.cn/2016-01-23_56a2eb45b1a14.png) Host:主要負責托管應用程序的進程,可以是IIS,也可以自己寫的程序等。主要用來啟動,加載OWIN組件,以及合理地管理它們。 Server:指的實際的Web Server,負責綁定套接字并對Http請求進行監聽,將Request和ReponsedeBody、Header封裝成服務OWIN規范的字典并發送到OWIN Middleware Pipeline中進行處理。 Middleware:這個中間件就是用來在OWIN管道中處理請求的組件(**可以把它想象成一個自定義的Http Module**),它會被注冊到Owin管道中一起處理Http request。 Application:這個就是我們自己開發的應用程序,或者是網站。 **應用程序代理(Application Delegate)** Owin規范另一個重要的組成部分是接口的定義,它通過將服務器與應用程序之間的交互歸納為一個方法簽名,稱之為“應用程序代理”(Applacation Delegate)。具體定義如下: 上面委托的定義中第一參數稱為環境字典,而第二個參數Task指的異步執行的方法。之前我們通過HttpContext對象來獲得request、Response等對象,基于Owin的應用是通過這個環境字典來獲得相應的對象。有了Owin之后,我們就不再與Asp.net管道打交道了,取而代之則是Owin管道。 ### Microsoft對OWIN規范的實現——Katana 既然OWIN是一套規范,自然就有其具體的實現,微軟根據OWIN規范在Windows下實現了Katana(武士刀)。其開源地址:[http://katanaproject.codeplex.com/](http://katanaproject.codeplex.com/)。 Katana實現了OWIN的4個組件。 1)Host: Kataba為我們提供了3種Host的選擇: * IIS:使用IIS是最簡單和向后兼容方式。在這種場景中OWIN管道通過標準的HttpModule和HttpHandler啟動。使用此Host你必須使用System.Web作為OWIN Server * Custom Host: 你也可以選擇創建一個自定義宿主來托管應用程序 * OwinHost:Katana自己實現了宿主程序——OwinHost.exe。我們可以利用該宿主來宿主我們的應用程序。 2)Server: Katana對Owin Server的實現提供如下幾類實現: * System.Web: System.Web與IIS兩者彼此耦合,當你選擇使用System.Web作為Server,此時必須選擇IIS為宿主。 * HttpListener:這是OwinHost.exe和定義Host默認的Server。 * WebListener:這是ASP.NET vNext默認的輕量級Server。它目前無法使用在Katana張。 3) Middleware: 中間件(Middleware)用來處理Pipeline中的請求。Middleware是Owin Pipeline中處理請求的單元,它可以是Log組件,也可以是Asp.net Web API、SignlR等組件。 4)Application:應用程序的實現代碼,可以為Asp.net MVC站點,也可以為Asp.net Web API和SignalR具體的應用實現。 Katana,它只能夠運行在Windows中,使得在Windows環境下,我們的Asp.net 網站不完全依賴于IIS;而在Liunx環境下也有OWIN規范的具體實現,就是Jexus Web Server(簡稱JWS)。所以,我們可以利用Mono+OWIN+Jexus在Liunx環境下部署我們的Asp.net站點,具體部署參考:[ASP.NET Linux部署(2) - MS Owin + WebApi + Mono + Jexus](http://www.cnblogs.com/zergcom/p/4619106.html)。 到這里,你們還記得我文章開頭的疑問嗎?大家應該都知道,在OWIN規范出現之前,我們就已經可以利用Mono來講我們的Asp.net 站點部署在Liunx環境下了,之前采用的部署方式是:Mono+Apache/nginx + XSP2。具體部署請參考:[Linux下的.NET之旅:第一站,CentOS+Mono+Xsp構建最簡單的ASP.NET服務器](http://www.cnblogs.com/edisonchou/p/3526588.html)。既然以前也可以實現Asp.net 網站在liunx環境下部署,則利用Owin的實現Jexus自然就有其優勢,不能也就沒有其存在的意義了,這里就涉及到Mono Xsp與基于Owin實現Jexus的一個對比:  _ Mono Xsp 和Jexus有什么區別呢:_ 1. _**速度方面:** 對于ASP.NET網頁,大壓力訪問時Jexus處理速度更快; 對于靜態文件,Jexus遠快于XSP,而且對磁盤的要求和影響小N倍;_ 2. _**功能方面:** XSP是以ASP.NET測試工作開發的,功能單調,而Jexus是作為生產環境使用的真實的WEB服務開發的,功能全面,因此,xsp與Jexus在功能上可比性_ 3. _**穩定性方面:** Jexus有良好的容錯和自動糾錯能力,可以長期不間斷運行,而XSP是單進程程序,沒有任何自動糾錯機制,無法保持不間斷運行。_ 4. _**安全性方面:** Jexus有關鍵的入侵檢測功能,XSP沒有任何安全檢測功能,沒有可比性;_ 5. _**多站點支持:** XSP支持一站,Jexus支持任意多網站。_ _更詳細內容請參考:[http://www.cnblogs.com/alsw/p/3255984.html](http://www.cnblogs.com/alsw/p/3255984.html)。_ ## 三、使用IIS托管Katana-based Asp.net網站 因為Katana為了向后兼容,依然支持IIS作為宿主,下面通過一個例子看看如何將Asp.net 站點托管在Katana-based的IIS中。 1\. 創建一個空的Web Application: 2\. 從Nuget中添加 Microsoft.Owin.Host.SystemWeb包 3\. 添加OWIN Startup類,并添加如下代碼在Startup1.Configuration方法中: ``` public void Configuration(IAppBuilder app) { // 有關如何配置應用程序的詳細信息,請訪問 http://go.microsoft.com/fwlink/?LinkID=316888 **app.Run(context => { context.Response.ContentType = "text/plain"; return context.Response.WriteAsync("Hello, world."****); });** } ``` 按F5運行,你將看到瀏覽器中打印出“Hello, world”的字樣。 雖然同樣是托管在IIS,但是所有的請求都會被OWIN來處理。Kanata除了支持IIS托管外,還支持自定義宿主,接下來介紹就是通過創建一個控制臺程序來宿主Web 應用程序。 ## 四、利于Microsoft.Owin.Host.HttpListener實現自寄宿 OWIN目標就是使得Web Server與Web Application解耦,接下來就具體看看如何將Web應用程序實現自我宿主。 1\. 首先創建一個控制臺應用程序 2\. 通過Nuget安裝Microsoft.Owin.Hosting和Microsoft.Owin.HttpListener包 3\. 創建OWIN Startup類,該類的具體實現代碼: ``` public void Configuration(IAppBuilder app) { // 有關如何配置應用程序的詳細信息,請訪問 http://go.microsoft.com/fwlink/?LinkID=316888 app.Run(context => { context.Response.ContentType = "text/plain"; return context.Response.WriteAsync("Hello, this is Self host"); }); } ``` 4\. 在Main方法中加入下面代碼來啟動我們的網站: ``` static void Main(string[] args) { using (WebApp.Start<Startup>( new StartOptions(url: "http://localhost:8888"))) { Console.ReadLine(); } Console.ReadLine(); } ``` 運行該控制臺程序,然后在瀏覽器中輸入“http://localhost:8888/”將看到如下界面: ![](https://box.kancloud.cn/2016-01-23_56a2eb45bf51e.png) 當然,Katana還支持OwinHost.exe程序來進行宿主,其實現步驟如下所示: 1\. 創建一個空的Web應用程序 2\. 通過Nuget安裝OwinHost包 3\. 添加OWIN Startup類,并添加如下代碼: ``` public void Configuration(IAppBuilder app) { // 有關如何配置應用程序的詳細信息,請訪問 http://go.microsoft.com/fwlink/?LinkID=316888 app.Run(context => { **context.Response.ContentType** **= "text/plain"; return context.Response.WriteAsync("Hello, This is host in OwinHost.exe."****);** }); } ``` 4\. 設置Web應用程序屬性,將宿主從IIS Express更改為OwinHost。具體設置如下圖所示: ![](https://box.kancloud.cn/2016-01-23_56a2eb45ceff8.png) 然后運行該網站,你將在瀏覽器中看到“Hello, This is host in OwinHost.exe.”的字樣。 ## 五、讓Asp.net網站運行在定義的Web服務器上 前面我們簡單應用了Kanata支持的三種宿主方式。但如果我們想將我們的Asp.net 網站運行到自定義的Web 服務器上該怎么辦呢?朋友們,你們是否還記到,我在C#網絡編程系列中,已經實現一個輕量的Web 服務器了。既然OWIN規范可以使得我們可以將Asp.net網站不再依賴于IIS Web 服務器,那自然我們就可以通過自定義Web 服務器,然后讓Asp.net運行在我們自定義的Web 服務器上了。接下來讓我們具體看看,如何實現Asp.net網站運行在我們自定義的Web 服務器上的。 1\. 首先自定義Web 服務器。具體的實現代碼如下所示: ``` using System.Net; using System.Net.Sockets; using AppFunc = Func<IDictionary<string, object>, Task>; public class CustomServer { public CustomServer() { // Create a configurable instance } public void Start(AppFunc next, IList<IDictionary<string, object>> addresses) { // 獲得本機的Ip地址,即127.0.0.1 IPAddress localaddress = IPAddress.Loopback; // 創建可以訪問的斷點,49155表示端口號,如果這里設置為0,表示使用一個由系統分配的空閑的端口號 IPEndPoint endpoint = new IPEndPoint(localaddress, 8888); // 創建Tcp 監聽器 TcpListener tcpListener = new TcpListener(endpoint); // 啟動監聽 tcpListener.Start(); Console.WriteLine("Wait an connect Request..."); while (true) { // 等待客戶連接 TcpClient client = tcpListener.AcceptTcpClient(); if (client.Connected == true) { // 輸出已經建立連接 Console.WriteLine("Created connection"); } // 獲得一個網絡流對象 // 該網絡流對象封裝了Socket的輸入和輸出操作 // 此時通過對網絡流對象進行寫入來返回響應消息 // 通過對網絡流對象進行讀取來獲得請求消息 NetworkStream netstream = client.GetStream(); // 把客戶端的請求數據讀入保存到一個數組中 byte[] buffer = new byte[2048]; int receivelength = netstream.Read(buffer, 0, 2048); string requeststring = Encoding.UTF8.GetString(buffer, 0, receivelength); // 在服務器端輸出請求的消息 Console.WriteLine(requeststring); // 服務器端做出相應內容 // 響應的狀態行 string statusLine = "HTTP/1.1 200 OK\r\n"; byte[] responseStatusLineBytes = Encoding.UTF8.GetBytes(statusLine); string responseBody = "<html><head><title>Default Page</title></head><body><p style='font:bold;font-size:24pt'>Welcome my custom server</p></body></html>"; string responseHeader = string.Format( "Content-Type: text/html; charset=UTf-8\r\nContent-Length: {0}\r\n", responseBody.Length); byte[] responseHeaderBytes = Encoding.UTF8.GetBytes(responseHeader); byte[] responseBodyBytes = Encoding.UTF8.GetBytes(responseBody); // 寫入狀態行信息 netstream.Write(responseStatusLineBytes, 0, responseStatusLineBytes.Length); // 寫入回應的頭部 netstream.Write(responseHeaderBytes, 0, responseHeaderBytes.Length); // 寫入回應頭部和內容之間的空行 netstream.Write(new byte[] { 13, 10 }, 0, 2); // 寫入回應的內容 netstream.Write(responseBodyBytes, 0, responseBodyBytes.Length); // 關閉與客戶端的連接 client.Close(); Console.ReadKey(); break; } // 關閉服務器 tcpListener.Stop(); } } using AppFunc = Func<IDictionary<string, object>, Task>; public static class OwinServerFactory { /// <summary> /// Optional. This gives the server the chance to tell the application about what capabilities are supported. /// </summary> /// <param name="properties"></param> public static void Initialize(IDictionary<string, object> properties) { // TODO: Add Owin.Types.BuilderProperties for setting capabilities, etc.. // Consider adding a configurable object to the properties if the application needs to set some specific server settings. properties[typeof(CustomServer).FullName] = new CustomServer(); } public static CustomServer Create(AppFunc app, IDictionary<string, object> properties) { object obj; // Get the user configured server instance, if any. CustomServer server = null; if (properties.TryGetValue(typeof(CustomServer).FullName, out obj)) { server = obj as CustomServer; } server = server ?? new CustomServer(); // Get the address collection IList<IDictionary<string, object>> addresses = null; if (properties.TryGetValue("host.Addresses", out obj)) { addresses = obj as IList<IDictionary<string, object>>; } server.Start(app, addresses); return server; } } ``` 2\. 創建一個控制臺應用程序來對Web應用程序進行自我宿主。 3\. 通過Nuget添加“Microsoft.Owin.Hosting”包 4\. 添加OWIN Startup類 5\. 往Main方法中添加下面代碼: ``` static void Main(string[] args) { using (WebApp.Start<Startup>( new StartOptions(url: "http://localhost:8888") { ServerFactory = "CustomWebServer" })) { Console.WriteLine("Started, Press any key to stop."); Console.ReadLine(); Console.WriteLine("Stopped"); } } ``` 此時,運行該控制臺程序,然后在瀏覽器中輸入“localhost:8888”,你將看到如下結果: ![](https://box.kancloud.cn/2016-01-23_56a2eb45e8a09.png) 到此,我們已經將Asp.net站點運行在我們自定義的Web 服務器上了。 ## 六、總結 到這里,關于OWIN的介紹就到此結束了,接下來將介紹Asp.net最新的用戶權限管理:Asp.net Identity的相關內容。 本文的所有源碼下載:[OWINDemo](http://files.cnblogs.com/files/zhili/OWINDemo.rar)
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看