The Open Web Interface for .NET (OWIN) 源码阅读

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了The Open Web Interface for .NET (OWIN) 源码阅读相关的知识,希望对你有一定的参考价值。

katana开源许久,网上仍未搜索到对其源码的阅读总结,本人在工作中正好遇到数据处理流程框架设计,想来跟服务器处理requestresponse差不多,遂起了阅读katana源码,并借鉴其设计的想法,磕磕碰碰,困难重重,所幸有一些收获,与大家交流交流。

katana源码 https://katanaproject.codeplex.com/

owin官网 http://owin.org/

两个最重要的数据结构

1 Environment

IDictionary<string, object>

官方解释:

This data structure is responsible for storing all of the state necessary for processing an HTTP request and response, as well as any relevant server state. An OWIN-compatible Web server is responsible for populating the environment dictionary with data such as the body streams and header collections for an HTTP request and response. It is then the responsibility of the application or framework components to populate or update the dictionary with additional values and write to the response body stream.

Environment是在pipeline中流动的数据,代表着一个具体的requestresponse,后文会介绍在每个pipeline stage中会对这个dictionary中自己关心的数据进行处理,并在进入下一个stage的时候丢弃引用,采用的是原子操作,因而每个Environment只存在一个pipeline stage中。数据举例:

Key Name

Value Description

"owin.RequestBody"

A Stream with the request body, if any. Stream.Null MAY be used as a placeholder if there is no request body. See Request Body.

"owin.RequestHeaders"

An IDictionary<string, string[]> of request headers. 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.

 

2 AppFunc

Func<IDictionary<string, object>, Task>;

官方解释:

The second key element of OWIN is the application delegate. This is a function signature which serves as the primary interface between all components in an OWIN application. The definition for the application delegate is as follows:

The application delegate then is simply an implementation of the Func delegate type where the function accepts the environment dictionary as input and returns a Task. This design has several implications for developers:

  • There are a very small number of type dependencies required in order to write OWIN components. This greatly increases the accessibility of OWIN to developers.
  • The asynchronous design enables the abstraction to be efficient with its handling of computing resources, particularly in more I/O intensive operations.
  • Because the application delegate is an atomic unit of execution and because the environment dictionary is carried as a parameter on the delegate, OWIN components can be easily chained together to create complex HTTP processing pipelines.

这就是middleware,也是每个pipeline stage中具体的处理方法,采用异步调用的方式,由StartUp类进行注册,并生成一条链,实际上就是压进一个List中。

 

源码阅读,能学到很多东西,肯定有很多理解有偏差的地方,欢迎指正,我将从一个具体的middleware注册和StartUp的执行切入,大致勾勒一个pipeline的构造和流动过程。

以上是关于The Open Web Interface for .NET (OWIN) 源码阅读的主要内容,如果未能解决你的问题,请参考以下文章

文件方式实现完整的英文词频统计实例

文件方式实现完整的英文词频统计实例

文件方式实现完整的英文词频统计实例

[Django] The admin interface

simplify the design of the hardware forming the interface between the processor and thememory system

CUPTI - The CUDA Profiling Tools Interface