OWIN与Katana
一、OWIN
二、Katana
三、Middleware中間件
一、OWIN
1,OWIN介紹
OWIN是Open Web Server Interface for .Net的首字母縮寫。OWIN在.Net Web Server與Web Application之間定義了一套標準接口。OWIN的目標用于解耦Web Server與Web Application。OWIN只是一個契約、規范,而非代碼的實現(Katana實現了OWIN)
2,OWIN規范
1)OWIN定義了Host、Server、Middleware、Application四層
①Host:主要負責應用程序的配置和啟動進程,包括初始化OWIN Pipeline(管道,包含了Middleware)、運行Server
②Server:綁定套接字并監聽Http請求,然后將Request和Response的Body、Header封裝成符合OWIN規范的字典并發送到OWIN Middleware Pipeline中處理
②Middleware:中間件、組件,位于Server與Application之間,用來處理發送到Pipeline中的請求
④Application:具體的應用程序代碼
2)Application Delegate(應用程序委托),用于Server與Middleware的交互。他并不是嚴格意義上的接口,而是一個委托并且每個OWIN中間件組件必須提供。
3)Environment Dictionary(環境字典),對http請求的封裝
request data:
| Required | Key Name | Value Description |
| Yes | "owin.RequestBody" | 請求體。Stream類型 |
| Yes | "owin.RequestHeaders" | 請求頭。IDictionary<string, string[]>類型 |
| Yes | "owin.RequestMethod" | 包含請求的HTTP請求方法(例如,“GET”,“POST”)的字符串。 |
| Yes | "owin.RequestPath" | 包含請求路徑的字符串。 該路徑必須是相對于應用程序委托的“根”的 |
| Yes | "owin.RequestPathBase" | 包含與應用程序委托的“根”對應的請求路徑部分的字符串 |
| Yes | "owin.RequestProtocol" | 包含協議名稱和版本的字符串(例如“HTTP / 1.0”或“HTTP / 1.1”)。 |
| Yes | "owin.RequestQueryString" | 包含HTTP請求URI的查詢字符串組件的字符串,不含前導“?”(例如,“foo = bar&baz = quux”)。 該值可能是一個空字符串。 |
| Yes | "owin.RequestScheme" | 包含用于請求的URI方案(例如“http”,“https”)的字符串 |
response data:
| Required | Key Name | Value Description |
| Yes | "owin.ResponseBody" | 響應體。Stream類型 |
| Yes | "owin.ResponseHeaders" | 響應頭。IDictionary<string, string[]>類型 |
| No | "owin.ResponseStatusCode" | 包含RFC 2616第6.1.1節中定義的HTTP響應狀態代碼的可選項。 默認值是200。 |
| No | "owin.ResponseReasonPhrase" | 包含原因短語的可選字符串關聯給定的狀態碼。 如果沒有提供,則服務器應該按照RFC 2616第6.1.1節的規定提供默認值 |
| No | "owin.ResponseProtocol" | 包含協議名稱和版本的可選字符串(例如“HTTP / 1.0”或“HTTP / 1.1”)。 如果沒有提供,則“owin.RequestProtocol”鍵的值是默認值。 |
other data:
| Required | Key Name | Value Description |
| Yes | "owin.CallCancelled" | 指示請求是否被取消/中止 |
| Yes | "owin.Version" | 表示OWIN版本的字符串“1.0” |
?
二、Katana
1,Katana介紹
①Katana實現了OWIN的Layers
②Pileline中的Middleware是鏈式執行的,有開發人員控制
③層
?
2,使用ASP.NET/IIS托管Katana-based應用程序
nuget Microsoft.Owin.Host.SystemWeb
添加Startup啟動類將Middleware中間件注冊到Pileline中
using System; using System.Threading.Tasks; using Microsoft.Owin; using Owin;[assembly: OwinStartup(typeof(Katana.Systen.Web.Startup))]namespace Katana.Systen.Web {public class Startup{public void Configuration(IAppBuilder app){// 有關如何配置應用程序的詳細信息,請訪問 https://go.microsoft.com/fwlink/?LinkID=316888 app.Run(context => { return context.Response.WriteAsync("holle World"); });}} } Startup3,自托管Katana-based應用程序
nuget Microsoft.Owin.SelfHost
在Main方法中使用Startup配置項構建Pipeline并監聽端口
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Microsoft.Owin.Hosting;namespace Katana.SelfHost {class Program{static void Main(string[] args){using (WebApp.Start<Startup>("http://localhost:7000")){Console.WriteLine("程序啟動");Console.ReadLine();}}} } Program using System; using System.Threading.Tasks; using Microsoft.Owin; using Owin;[assembly: OwinStartup(typeof(Katana.SelfHost.Startup))]namespace Katana.SelfHost {public class Startup{public void Configuration(IAppBuilder app){// 有關如何配置應用程序的詳細信息,請訪問 https://go.microsoft.com/fwlink/?LinkID=316888 app.Run(context => { return context.Response.WriteAsync("holle World"); });}} } Startup3.1,使用Katana Diagnostic Helpers
將應用程序運行在自定義Host中,將失去IIS中所有的管理、診斷功能,但我們可以使用Katana提供的內置Helper比如Diagnostic helper來獲取
nuget Micorost.Owin.Diagnostic
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Microsoft.Owin.Hosting;namespace Katana.SelfHost {class Program{static void Main(string[] args){using (WebApp.Start<DiagnosticStartup>("http://localhost:7000")){Console.WriteLine("程序啟動");Console.ReadLine();}}} } Program using System; using System.Threading.Tasks; using Microsoft.Owin; using Owin; using System.Diagnostics;[assembly: OwinStartup(typeof(Katana.SelfHost.DiagnosticStartup))]namespace Katana.SelfHost {public class DiagnosticStartup{public void Configuration(IAppBuilder app){// 有關如何配置應用程序的詳細信息,請訪問 https://go.microsoft.com/fwlink/?LinkID=316888//將一個WelcomePage Middleware注冊到pipeline中//(在此輸出WelcomePage渲染頁面,并且不會執行余下的pipeline middleware組件)app.UseWelcomePage("/");//將一個錯誤頁注冊到pipeline中 app.UseErrorPage();app.Run(context =>{if (context.Request.Path.ToString().Equals("/error")){Trace.WriteLine(context.Request.Path);throw new Exception("拋出異常");}return context.Response.WriteAsync("Hello World");});}} } DiagnosticStartup5,使用OwinHost.exe托管Katana-based應用程序
nuget OwinHost
nuget Micosoft.Owin
配置web屬性
using System; using System.Threading.Tasks; using Microsoft.Owin; using Owin;[assembly: OwinStartup(typeof(Katana.OwinHost.WebApp.Startup))]namespace Katana.OwinHost.WebApp {public class Startup{public void Configuration(IAppBuilder app){// 有關如何配置應用程序的詳細信息,請訪問 https://go.microsoft.com/fwlink/?LinkID=316888 app.Run(context => { return context.Response.WriteAsync("holle World"); });}} } Startup6,幾種制定Startup啟動項方式
①默認名稱約束:Host會去查找root namespace 下名為Startup的類作為啟動項
②OwinStartup Attribute:當創建Owin Startup類時,自動會加上Attribute如:
?[assembly: OwinStartup(typeof(Katana.OwinHost.WebApp.Startup))]?
③配置文件:如:
<add key="owin:appStartup" value="Katana.OwinHost.WebApp.Startup">④如果使用自定一host,那么可以通過?WebApp.Start<DiagnosticStartup>("http://localhost:7000")?來設置啟動項
⑤如果使用OwinHost,那么可以通過命名行參數來實現
三、創建自定義Middleware中間件
using System; using System.Threading.Tasks; using System.Collections; using Microsoft.Owin; using Owin;[assembly: OwinStartup(typeof(Katana.OwinHost.WebApp.Startup))]namespace Katana.OwinHost.WebApp {public class Startup{public void Configuration(IAppBuilder app){// 有關如何配置應用程序的詳細信息,請訪問 https://go.microsoft.com/fwlink/?LinkID=316888app.Use(async (context, next) => {await context.Response.WriteAsync("begin"+Environment.NewLine);await next();await context.Response.WriteAsync("end" + Environment.NewLine);});app.Use<MyMiddleware>();app.Run(context => { return context.Response.WriteAsync("holle World" + Environment.NewLine); });}} } using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using System.Web; using Microsoft.Owin;namespace Katana.OwinHost.WebApp {public class MyMiddleware : OwinMiddleware{public MyMiddleware(OwinMiddleware next) : base(next) { }public override async Task Invoke(IOwinContext context){await context.Response.WriteAsync("MyMiddleware-begin" + Environment.NewLine);await this.Next.Invoke(context);await context.Response.WriteAsync("MyMiddleware-end" + Environment.NewLine);}} }案例下載:http://pan.baidu.com/s/1qXAmu2w
轉載于:https://www.cnblogs.com/zd1994/p/7820338.html
總結
以上是生活随笔為你收集整理的OWIN与Katana的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 项目管理中用什么工具可以增强团队协作?
- 下一篇: 在线查毒网站