OWIN与Katana
一、OWIN
二、Katana
三、Middleware中間件
一、OWIN
1,OWIN介紹
OWIN是Open Web Server Interface for .Net的首字母縮寫。OWIN在.Net Web Server與Web Application之間定義了一套標(biāo)準(zhǔn)接口。OWIN的目標(biāo)用于解耦Web Server與Web Application。OWIN只是一個(gè)契約、規(guī)范,而非代碼的實(shí)現(xiàn)(Katana實(shí)現(xiàn)了OWIN)
2,OWIN規(guī)范
1)OWIN定義了Host、Server、Middleware、Application四層
①Host:主要負(fù)責(zé)應(yīng)用程序的配置和啟動(dòng)進(jìn)程,包括初始化OWIN Pipeline(管道,包含了Middleware)、運(yùn)行Server
②Server:綁定套接字并監(jiān)聽Http請(qǐng)求,然后將Request和Response的Body、Header封裝成符合OWIN規(guī)范的字典并發(fā)送到OWIN Middleware Pipeline中處理
②Middleware:中間件、組件,位于Server與Application之間,用來(lái)處理發(fā)送到Pipeline中的請(qǐng)求
④Application:具體的應(yīng)用程序代碼
2)Application Delegate(應(yīng)用程序委托),用于Server與Middleware的交互。他并不是嚴(yán)格意義上的接口,而是一個(gè)委托并且每個(gè)OWIN中間件組件必須提供。
3)Environment Dictionary(環(huán)境字典),對(duì)http請(qǐng)求的封裝
request data:
| Required | Key Name | Value Description |
| Yes | "owin.RequestBody" | 請(qǐng)求體。Stream類型 |
| Yes | "owin.RequestHeaders" | 請(qǐng)求頭。IDictionary<string, string[]>類型 |
| Yes | "owin.RequestMethod" | 包含請(qǐng)求的HTTP請(qǐng)求方法(例如,“GET”,“POST”)的字符串。 |
| Yes | "owin.RequestPath" | 包含請(qǐng)求路徑的字符串。 該路徑必須是相對(duì)于應(yīng)用程序委托的“根”的 |
| Yes | "owin.RequestPathBase" | 包含與應(yīng)用程序委托的“根”對(duì)應(yīng)的請(qǐng)求路徑部分的字符串 |
| Yes | "owin.RequestProtocol" | 包含協(xié)議名稱和版本的字符串(例如“HTTP / 1.0”或“HTTP / 1.1”)。 |
| Yes | "owin.RequestQueryString" | 包含HTTP請(qǐng)求URI的查詢字符串組件的字符串,不含前導(dǎo)“?”(例如,“foo = bar&baz = quux”)。 該值可能是一個(gè)空字符串。 |
| Yes | "owin.RequestScheme" | 包含用于請(qǐng)求的URI方案(例如“http”,“https”)的字符串 |
response data:
| Required | Key Name | Value Description |
| Yes | "owin.ResponseBody" | 響應(yīng)體。Stream類型 |
| Yes | "owin.ResponseHeaders" | 響應(yīng)頭。IDictionary<string, string[]>類型 |
| No | "owin.ResponseStatusCode" | 包含RFC 2616第6.1.1節(jié)中定義的HTTP響應(yīng)狀態(tài)代碼的可選項(xiàng)。 默認(rèn)值是200。 |
| No | "owin.ResponseReasonPhrase" | 包含原因短語(yǔ)的可選字符串關(guān)聯(lián)給定的狀態(tài)碼。 如果沒有提供,則服務(wù)器應(yīng)該按照RFC 2616第6.1.1節(jié)的規(guī)定提供默認(rèn)值 |
| No | "owin.ResponseProtocol" | 包含協(xié)議名稱和版本的可選字符串(例如“HTTP / 1.0”或“HTTP / 1.1”)。 如果沒有提供,則“owin.RequestProtocol”鍵的值是默認(rèn)值。 |
other data:
| Required | Key Name | Value Description |
| Yes | "owin.CallCancelled" | 指示請(qǐng)求是否被取消/中止 |
| Yes | "owin.Version" | 表示OWIN版本的字符串“1.0” |
?
二、Katana
1,Katana介紹
①Katana實(shí)現(xiàn)了OWIN的Layers
②Pileline中的Middleware是鏈?zhǔn)綀?zhí)行的,有開發(fā)人員控制
③層
?
2,使用ASP.NET/IIS托管Katana-based應(yīng)用程序
nuget Microsoft.Owin.Host.SystemWeb
添加Startup啟動(dòng)類將Middleware中間件注冊(cè)到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){// 有關(guān)如何配置應(yīng)用程序的詳細(xì)信息,請(qǐng)?jiān)L問 https://go.microsoft.com/fwlink/?LinkID=316888 app.Run(context => { return context.Response.WriteAsync("holle World"); });}} } Startup3,自托管Katana-based應(yīng)用程序
nuget Microsoft.Owin.SelfHost
在Main方法中使用Startup配置項(xiàng)構(gòu)建Pipeline并監(jiān)聽端口
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("程序啟動(dòng)");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){// 有關(guān)如何配置應(yīng)用程序的詳細(xì)信息,請(qǐng)?jiān)L問 https://go.microsoft.com/fwlink/?LinkID=316888 app.Run(context => { return context.Response.WriteAsync("holle World"); });}} } Startup3.1,使用Katana Diagnostic Helpers
將應(yīng)用程序運(yùn)行在自定義Host中,將失去IIS中所有的管理、診斷功能,但我們可以使用Katana提供的內(nèi)置Helper比如Diagnostic helper來(lái)獲取
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("程序啟動(dòng)");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){// 有關(guān)如何配置應(yīng)用程序的詳細(xì)信息,請(qǐng)?jiān)L問 https://go.microsoft.com/fwlink/?LinkID=316888//將一個(gè)WelcomePage Middleware注冊(cè)到pipeline中//(在此輸出WelcomePage渲染頁(yè)面,并且不會(huì)執(zhí)行余下的pipeline middleware組件)app.UseWelcomePage("/");//將一個(gè)錯(cuò)誤頁(yè)注冊(cè)到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應(yīng)用程序
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){// 有關(guān)如何配置應(yīng)用程序的詳細(xì)信息,請(qǐng)?jiān)L問 https://go.microsoft.com/fwlink/?LinkID=316888 app.Run(context => { return context.Response.WriteAsync("holle World"); });}} } Startup6,幾種制定Startup啟動(dòng)項(xiàng)方式
①默認(rèn)名稱約束:Host會(huì)去查找root namespace 下名為Startup的類作為啟動(dòng)項(xiàng)
②OwinStartup Attribute:當(dāng)創(chuàng)建Owin Startup類時(shí),自動(dòng)會(huì)加上Attribute如:
?[assembly: OwinStartup(typeof(Katana.OwinHost.WebApp.Startup))]?
③配置文件:如:
<add key="owin:appStartup" value="Katana.OwinHost.WebApp.Startup">④如果使用自定一host,那么可以通過?WebApp.Start<DiagnosticStartup>("http://localhost:7000")?來(lái)設(shè)置啟動(dòng)項(xiàng)
⑤如果使用OwinHost,那么可以通過命名行參數(shù)來(lái)實(shí)現(xiàn)
三、創(chuàng)建自定義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){// 有關(guān)如何配置應(yīng)用程序的詳細(xì)信息,請(qǐng)?jiān)L問 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
轉(zhuǎn)載于:https://www.cnblogs.com/zd1994/p/7820338.html
總結(jié)
以上是生活随笔為你收集整理的OWIN与Katana的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 项目管理中用什么工具可以增强团队协作?
- 下一篇: 在线查毒网站