码迷,mamicode.com
首页 > Windows程序 > 详细

OWIN Kantana

时间:2016-06-12 18:10:50      阅读:490      评论:0      收藏:0      [点我收藏+]

标签:

Kantana是遵守OWIN规范的应用框架,下面四层架构也正是OWIN的规范

架构

技术分享

 

 

Host

宿主层,是OWIN程序的载体,这个宿主可以是IIS, IIS Express, Console, Windows Service等。

Katana通过在IIS上构建一个类似适配器,使得OWIN管道能够运行在IIS上,同时Katana也已经提供了一个可以直接运行Host——OwinHost.exe

作用
1. 管理底层进程
2. 当有请求过来的时候,选择相应的Server和构建OWIN管道处理请求。

 

Server

负责绑定到 TCP 端口,监听端口发送过来的请求,同时将请求的信息依照OWIN规范,包装成字典格式,传递到下层的Middleware.
IIS既可以作为Host层,也可以作为Server层

Microsoft.Owin.Host.SystemWeb

这个是用来对应IIS的,由于IIS既是Host,又是Server. 所以这个Server实现的作用是注册ASP.NET HttpModule和HttpHandler阻断原有的处理流程,转而把请求发送到OWIN管道中处理

Microsoft.Owin.Host.HttpListener
使用HttpListener打开Socket端口,监听请求,然后将请求包装发送到OWIN管道中处理。

字典: OWIN将web应用中的request, response, session, cookie等所有相关信息都简化成下面的字典

"owin.RequestBody"

请求报文体数据流. 如果没有报文体使用Stream.Null作为占位符.

"owin.RequestHeaders"

一个 IDictionary<string, string[]><string, string[]="">  类型请求头字典. See Headers.

"owin.RequestMethod"

A string containing the HTTP request method of the request (e.g., "GET", "POST").

"owin.RequestPath"

A string containing the request path. The path MUST be relative to the "root" of the application delegate; see Paths.

"owin.RequestPathBase"

A string containing the portion of the request path corresponding to the "root" of the application delegate; see Paths.

"owin.RequestProtocol"

A string containing the protocol name and version (e.g. "HTTP/1.0" or "HTTP/1.1").

"owin.RequestQueryString"

A string containing the query string component of the HTTP request URI, without the leading “?” (e.g., "foo=bar&baz=quux"). The value may be an empty string.

"owin.RequestScheme"

A string containing the URI scheme used for the request (e.g., "http", "https"); see URI Scheme.

 

Middleware

类似Asp.Net的Module的职责,OWIN 管道中的组件,Katana提供了一个OwinMiddleware基类更加方便我们继承来实现OWIN Middleware.

 

Application

应用程序代码

 

 

Owin核心定义

 

OWIN将web应用中的request, response, session, cookie等所有相关信息都简化成字典(表见上)

这是所有运行在OWIN协议下的组件都需要遵循的接口Func<IDictionary<string, object>, Task>;

 

 

 

简单的Web Host Demo

 

上面提到了Kantana支持多种宿主,这里只介绍Web Host

根据Owin协议,Middleware需要通过Func<IDictionary<string, object>, Task>添加

 

在空的MVC5项目中右键添加项可以创建一个Owin Startup类

  1. [assembly: OwinStartup(typeof(OwinDemo.Startup))]
  2. namespace OwinDemo
  3. {
  4. public class Startup
  5. {
  6. public void Configuration(IAppBuilder app)
  7. {
  8. // 有关如何配置应用程序的详细信息,请访问 http://go.microsoft.com/fwlink/?LinkID=316888
  9. app.Run(context =>
  10. {
  11. context.Response.ContentType = "text/plain";
  12. return context.Response.WriteAsync("Hello, world.");
  13. });
  14. }
  15. }
  16. }

Startup类可以理解为传统的Global.asax

上面代码演示了注册一个Middleware到Owin管道,app.Run的方法签名如下

public static void Run(this IAppBuilder app, Func<IOwinContext, Task> handler);

这是个扩展方法,它有一个委托参数,正是Owin定义要求的那个委托,利用他我们能给管道注册Middleware

而委托参数是一个IOwinContext接口,他的成员如下

  1. public interface IOwinContext
  2. {
  3. IAuthenticationManager Authentication { get; }
  4. IDictionary<string, object> Environment { get; }
  5. IOwinRequest Request { get; }
  6. IOwinResponse Response { get; }
  7. TextWriter TraceOutput { get; set; }
  8. T Get<T>(string key);
  9. IOwinContext Set<T>(string key, T value);
  10. }

可以看到Http请求中要用到的东西都能通过这个接口获得.另外这次请求相关的数据都封装成字典保存在Environment技术分享

 

 

 

上述代码中的[assembly: OwinStartup(typeof(OwinDemo.Startup))]是用来设置Owin启动类

更多的设置方法看下文

 

Middleware

 

我们通过集成OwinMiddleware来编写自己的中间件

  1. public class HelloWorldMiddleware : OwinMiddleware
  2. {
  3. public HelloWorldMiddleware(OwinMiddleware next) : base(next)
  4. {
  5. }
  6. public override Task Invoke(IOwinContext context)
  7. {
  8. var response = "Hello World! It is " + DateTime.Now;
  9. context.Response.Write(response);
  10. return Next.Invoke(context);
  11. }
  12. }?

中间件的执行过程根据Startup类中注册的顺序.上述代码的Next.Invoke代表执行下一个中间件


  1. public class Startup
  2. {
  3. public void Configuration(IAppBuilder app)
  4. {
  5. app.Use((x, next) =>
  6. {
  7. x.Response.ContentType = "text/html";
  8. return next.Invoke();
  9. });
  10. app.Use((x, next) =>
  11. {
  12. x.Response.WriteAsync("1 Before");
  13. next.Invoke();
  14. return x.Response.WriteAsync("1 After");
  15. });
  16. app.Use((x, next) =>
  17. {
  18. x.Response.WriteAsync("2 Before");
  19. next.Invoke();
  20. return x.Response.WriteAsync("2 After");
  21. });
  22. app.Run(x => x.Response.WriteAsync("<br/>hello world<br/>"));
  23. }
  24. }

技术分享

 

注意app.Use是添加中间件,而app.Run仅仅只是执行,app.Run后面的代码是不执行的,也就是在app.Run后面使用app.Use是无效的

Owin启动

 

一.约定

Kantana寻找一个名字程序集名称或全局命名空间匹配的命名空间中的 Startup类

 

二.OwinStartup 特性

在类名上面添加

1
[assembly:OwinStartup(typeof(startClassType))]

 

三.使用配置文件

在web.config中的appSetting添加相关配置

指定类名

1
<addkey="owin:appStartup"value="StartupDemo.ProductionStartup"/>

指定类名和程序集

1
<addkey="owin:appStartup"value="StartupDemo.ProductionStartup,StartupDemo"/>

带friendlyname的配置方式

1
<addkey="owin:appStartup"value="ProductionConfiguration"/>

这种方式要求类名上面也要使用特性标签,并注明friendlyname

1
[assembly:OwinStartup("ProductionConfiguration",typeof(StartupDemo.ProductionStartup2))]

 

 

 

摘抄至

http://www.asp.net/aspnet/overview/owin-and-katana

http://www.cnblogs.com/JustRun1983/p/3967757.html

http://www.cnblogs.com/neverc/p/4864414.html

 

 





OWIN Kantana

标签:

原文地址:http://www.cnblogs.com/Recoding/p/5578089.html

踩
(0)
赞
(0)
   
举报
评论 一句话评论(0)
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!