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. apache的日志access_log分析

    正常日志格式:客户端地址 访问者的标识 访问者的验证名字 请求的时间 请求类型 请求的HTTP代码 发送给客户端的字节数   当网站出问题时分析日志,第一步一般都不会是看访问日志.但是也不能忽视它,在 ...

  2. 一个TED演讲背后的文化论

    0. 前言 写这个前言让我很难受,当然不是心情难受哈,此时的状态是很High的哦,大中午觉都省了, 说难受是我觉得我这语言文字太渣了,相比今天的主题确实很没“文化”.但我也很庆幸,能 看到这么个人认为 ...

  3. 使用dig命令解析域名

    Linux下解析域名除了使用nslookup之外,开可以使用dig命令来解析域名,dig命令可以得到更多的域名信息. dig的全称是 (domain information groper).它是一个用 ...

  4. 【计算机视觉】ARM平台实现人脸检测YSQfastfd

    ARM平台实现于仕琪人脸检测库YSQfastfd 平台要求 ARM32 platform hardware board Ubuntu 16.04 with GTK3 library USB camer ...

  5. leetcode-1-TwoNums

    flag -everyday do leetcode problems at least one and at most three. problem here 需要学习的是c++的map类型,之前竟 ...

  6. Navicat #1045 - Access denied for user 'root'@'localhost' (using password: NO)

    Navicat #1045 - Access denied for user 'root'@'localhost' (using password: YES) 出现上述问题,原因在于本机还开了APMS ...

  7. (1)变量、常量、程序交互、数据类型、bool、基本运算符

    什么是变量 变量由变量名和变量值组成 name = 'Alex Li'  这个算式就是将一个值赋予给变量,也就是声明变量的意思 name 就是一个变量,也是一个变量的名字 'Alex Li' 就是一个 ...

  8. CTF-练习平台-WEB之 web2

    二.web2 打开连接发现... 在火狐浏览器里,按F12点击查看器就可以发现flag

  9. 各种CTF的WP

    http://l-team.org/archives/43.html PlaidCTF-2014-twenty/mtpox/doge_stege-Writeup http://l-team.org/a ...

  10. List<Map<String, Integer>> 同key的value全部累加合并

    public static void main(String[] args){ List<Map<String,Object>> list1 = new ArrayList&l ...