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. Kaggle新手入门之路(完结)

    学完了Coursera上Andrew Ng的Machine Learning后,迫不及待地想去参加一场Kaggle的比赛,却发现从理论到实践的转变实在是太困难了,在此记录学习过程. 一:安装Anaco ...

  2. Vue2.0 分页插件pagination使用详细说明

    Vue2.0 分页pagination使用 插件下载地址:Vue_Pagination 插件描述:基于jQuery的分页插件大家都用过很多了吧,今天分享一下基于Vue的分页插件pagination.j ...

  3. Object -c基础知识(5)--release 之后 retainCount为何为1

    在XCode中加入如下代码:  UILabel *label=[UILabel alloc]; [label setText:@"TestLabel"]; NSLog(@" ...

  4. 【opencv基础】imread-第二个参数

    问题1: 显示的是灰色的界面,不能正常显示图像. 解决方法:在imshow之后加上waitKey即可.原因here: Note:This function should be followed by ...

  5. URAL - 1397:Points Game (博弈,贪心)

    Two students are playing the following game. There are 2· n points on the plane, given with their co ...

  6. Arpa’s obvious problem and Mehrdad’s terrible solution 思维

    There are some beautiful girls in Arpa’s land as mentioned before. Once Arpa came up with an obvious ...

  7. CH4302 Interval GCD

    题意 4302 Interval GCD 0x40「数据结构进阶」例题 描述 给定一个长度为N的数列A,以及M条指令 (N≤5*10^5, M<=10^5),每条指令可能是以下两种之一: &qu ...

  8. hive 分区表

    hive中创建分区表没有什么复杂的分区类型(范围分区.列表分区.hash分区.混合分区等).分区列也不是表中的一个实际的字段,而是一个或者多个伪列.意思是说在表的数据文件中实际上并不保存分区列的信息与 ...

  9. SQL Server MERGE

    Merge关键字是一个神奇的DML关键字.它在SQL Server 2008被引入,它能将Insert,Update,Delete简单的并为一句.MSDN对于Merge的解释非常的短小精悍:”根据与源 ...

  10. PHP独立环境搭建细节

    一.安装前准备: 准备安装软件此处以以下软件为例: Appache:httpd-2.2.21-win32-x86-openssl-0.9.8r.msi MySQL: mysql-5.5.21-win ...