Web API源码剖析之HttpServer

上一节我们讲述全局配置。本节将讲述全局配置的DefaultServer,它是一个HttpServer类型。 主要作用就是接受每一次请求,然后分发给消息处理程序链依次处理。从HttpServer定义可以看出,其本质是一个消息处理程序,其继承于DelegatingHandler。从其代码定义如下:

     //参数为

     public HttpServer(HttpConfiguration configuration, HttpMessageHandler dispatcher);

        // 摘要: 
        //     获取用于配置此实例的 System.Web.Http.HttpConfiguration。 
        // 
        // 返回结果: 
        //     用于配置此实例的 System.Web.Http.HttpConfiguration。 
        public HttpConfiguration Configuration { get; } 
        // 
        // 摘要: 
        //     获取用于处理传入请求的 HTTP 调度程序。 
        // 
        // 返回结果: 
        //     用于处理传入请求的 HTTP 调度程序。 
        public HttpMessageHandler Dispatcher { get; }

公开两个只读属性。

Dispatcher :承担分发中介者,实际上一个 HttpRoutingDispatcher类型。我们回顾一下上一节部分代码:

//以下都使用延迟加载。

private static Lazy<HttpConfiguration> _configuration = CreateConfiguration();

private static Lazy<HttpMessageHandler> _defaultHandler = CreateDefaultHandler();

private static Lazy<HttpServer> _defaultServer = CreateDefaultServer(); 

private static Lazy<HttpConfiguration> CreateConfiguration() 

           return new Lazy<HttpConfiguration>(() => 
           { 
               HttpConfiguration config = new HttpConfiguration(new HostedHttpRouteCollection(RouteTable.Routes)); 
               ServicesContainer services = config.Services; 
               Contract.Assert(services != null); 
               services.Replace(typeof(IAssembliesResolver), new WebHostAssembliesResolver()); 
               services.Replace(typeof(IHttpControllerTypeResolver), new WebHostHttpControllerTypeResolver()); 
               services.Replace(typeof(IHostBufferPolicySelector), new WebHostBufferPolicySelector()); 
               services.Replace(typeof(IExceptionHandler), 
                   new WebHostExceptionHandler(services.GetExceptionHandler())); 
               return config; 
           }); 
}

//默认的消息处理程序,实际就是路由分发器

private static Lazy<HttpMessageHandler> CreateDefaultHandler() 

           return new Lazy<HttpMessageHandler>(() => new HttpRoutingDispatcher(_configuration.Value)); 
}

//这里传递的是路由分发器。

private static Lazy<HttpServer> CreateDefaultServer() 

           return new Lazy<HttpServer>(() => new HttpServer(_configuration.Value, _defaultHandler.Value)); 

以上代码实现了HttpServer的初始化工作。同时他重写了 InnerHandler属性。内部的实现机制是:

将消息处理程序链顺序倒置,依次设置消息处理程序。这样就实现了消息处理程序,先入先出的原理:例如

config.MessageHandlers.Add(new M2DelegatingHandler()); 
config.MessageHandlers.Add(new M1DelegatingHandler());

则是先调用M2DelegatingHandler,然后在调用M1DelegatingHandler,再次调用HttpRoutingDispatcher,最后将委托分发给HttpControllerDispatcher

从上面可以看出HttpServer,大部分工作就是协调作用,分发作用。

有兴趣的朋友可以下载web Api 源码查看。http://aspnetwebstack.codeplex.com/wikipage?title=Contributors.

Web API源码剖析之HttpServer的更多相关文章

  1. Web API 源码剖析之默认消息处理程序链之路由分发器(HttpRoutingDispatcher)

    Web API 源码剖析之默认消息处理程序链-->路由分发器(HttpRoutingDispatcher) 我们在上一节讲述了默认的DefaultServer(是一个类型为HttpServer的 ...

  2. Web API 源码剖析之全局配置

    Web API 源码剖析之全局配置 Web API  均指Asp.net Web API .本节讲述的是基于Web API 系统在寄宿于IIS. 本节主要讲述Web API全局配置.它是如何优雅的实现 ...

  3. Web API 源码剖析之默认消息处理程序链--》路由分发器(HttpRoutingDispatcher)

    我们在上一节讲述了默认的DefaultServer(是一个类型为HttpServer的只读属性,详情请参考 Web API 源码剖析之全局配置).本节将讲述DefaultHandler(是一个Http ...

  4. Web API 源码剖析之默认配置(HttpConfiguration)

    Web API 源码剖析之默认配置(HttpConfiguration) 我们在上一节讲述了全局配置和初始化.本节我们将就全局配置的Configuration只读属性进行展开,她是一个类型为HttpC ...

  5. Web Api源码(路由注册)

    这篇文章只是我学习Web API框架的输出,学习方法还是输出倒逼输入比较行得通,所以不管写的好不好,坚持下去,肯定有收获.篇幅比较长,仔细思考阅读下来大约需要几分钟. 做.NET开发有好几年时间了,从 ...

  6. 长期作业:web框架源码剖析

    Tornado框架 1.1. 手动安装 1.2. 从简单的开始:分析红框部分的源码 Django框架

  7. 老李推荐:第3章3节《MonkeyRunner源码剖析》脚本编写示例: MonkeyImage API使用示例 1

    老李推荐:第3章3节<MonkeyRunner源码剖析>脚本编写示例: MonkeyImage API使用示例   在上一节的第一个“增加日记”的示例中,我们并没有看到日记是否真的增加成功 ...

  8. 老李推荐: 第3章1节《MonkeyRunner源码剖析》脚本编写示例: MonkeyRunner API使用示例

    老李推荐: 第3章1节<MonkeyRunner源码剖析>脚本编写示例: MonkeyRunner API使用示例   MonkeyRunner这个类可以说是编写monkeyrunner脚 ...

  9. RestFramework——API基本实现及dispatch基本源码剖析

    基于Django实现 在使用RestFramework之前我们先用Django自己实现以下API. API完全可以有我们基于Django自己开发,原理是给出一个接口(URL),前端向URL发送请求以获 ...

随机推荐

  1. Python 水果统计

    f = open("水果.txt", mode="r", encoding="utf-8") lst = [] for line in f: ...

  2. HAWQ + MADlib 玩转数据挖掘之(六)——主成分分析与主成分投影

    一.主成分分析(Principal Component Analysis,PCA)简介 在数据挖掘中经常会遇到多个变量的问题,而且在多数情况下,多个变量之间常常存在一定的相关性.例如,网站的" ...

  3. iOS 开发 Framework

    制作Framework 的好处和缺点 好处:       1.如果模块间接口定义的比较完善,模块化的程序具有很好的可扩展性与内聚性:       2.物理上的模块化便于开发过程的管理与测试,尤其是在程 ...

  4. 4.1 shell文本过滤、find、awk、grep

    文本过滤涉及到以下知识: 正则表达式.find.grep.awk.sed.合并与分割(sort.uniq.join.cut.paste.split). 正则表达式: 基本元字符集及其含义: 匹配IP地 ...

  5. rest_cherrypy

    一.简介 A REST API FOR SALT 二.安装,配置 yum -y install salt-api rest_cherrypy: port: # disable_ssl: True ss ...

  6. can-utils源码解析cansend

    前言 本文主要介绍socketCan中的发送函数cansend的源码解析. 代码 /* * cansend.c - simple command line tool to send CAN-frame ...

  7. [LeetCode&Python] Problem 908. Smallest Range I

    Given an array A of integers, for each integer A[i] we may choose any x with -K <= x <= K, and ...

  8. ZOJ - 3216:Compositions (DP&矩阵乘法&快速幂)

    We consider problems concerning the number of ways in which a number can be written as a sum. If the ...

  9. CTF-练习平台-Misc之 Linux基础1

    十四.Linux基础1 下载打开文件,解压后发下是一个没有后缀名的文件,添加后缀名为txt,搜索关键词“KEY”,发现flag Linux???不存在的!

  10. (转) C++中成员初始化列表的使用

    C++在类的构造函数中,可以两种方式初始化成员数据(data member). 1,在构造函数的实现中,初始类的成员数据.诸如: class point{private: int x,y;public ...