Web API源码剖析之HttpServer
2016-11-22 22:47
204 查看
上一节我们讲述全局配置。本节将讲述全局配置的DefaultServer,它是一个HttpServer类型。 主要作用就是接受每一次请求,然后分发给消息处理程序链依次处理。从HttpServer定义可以看出,其本质是一个消息处理程序,其继承于DelegatingHandler。从其代码定义如下:
Dispatcher :承担分发中介者,实际上一个 HttpRoutingDispatcher类型。我们回顾一下上一节部分代码:
将消息处理程序链顺序倒置,依次设置消息处理程序。这样就实现了消息处理程序,先入先出的原理:例如
config.MessageHandlers.Add(new M2DelegatingHandler());
config.MessageHandlers.Add(new M1DelegatingHandler());
则是先调用M2DelegatingHandler,然后在调用M1DelegatingHandler,再次调用HttpRoutingDispatcher,最后将委托分发给HttpControllerDispatcher
从上面可以看出HttpServer,大部分工作就是协调作用,分发作用。
有兴趣的朋友可以下载web Api 源码查看。http://aspnetwebstack.codeplex.com/wikipage?title=Contributors.
//参数为
public HttpServer(HttpConfiguration configuration, HttpMessageHandler dispatcher);
// 摘要:
// 获取用于配置此实例的 System.Web.Http.HttpConfiguration。
//
// 返回结果:
// 用于配置此实例的 System.Web.Http.HttpConfiguration。
public HttpConfiguration Configuration { get; }
//
// 摘要:
// 获取用于处理传入请求的 HTTP 调度程序。
//
// 返回结果:
// 用于处理传入请求的 HTTP 调度程序。
public HttpMessageHandler Dispatcher { get; }
公开两个只读属性。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<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属性。内部的实现机制是: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));
}
将消息处理程序链顺序倒置,依次设置消息处理程序。这样就实现了消息处理程序,先入先出的原理:例如
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
- Python http.server源码剖析(一)
- Web API 源码剖析之默认消息处理程序链--》路由分发器(HttpRoutingDispatcher)
- Web API 源码剖析之默认消息处理程序链之路由分发器(HttpRoutingDispatcher)
- Asp.net web Api源码分析-HttpResponseMessage
- Netty4.x 源码实战系列(三):NioServerSocketChannel全剖析
- SpringMVC源码剖析(五)-消息转换器HttpMessageConverter
- 简单剖析一下 HttpServer的实现方式
- SpringMVC源码剖析(五)-消息转换器HttpMessageConverter
- Java源码剖析之server2008上拉不出验证码
- Nginx源码剖析--HTTP模块配置结构体在conf_ctx中的组织
- Nginx源码剖析--HTTP模块配置信息的merge
- SpringMVC源码剖析(五)-消息转换器HttpMessageConverter
- 【源码剖析】tinyhttpd —— C 语言实现最简单的 HTTP 服务器
- tio-http-server 源码浅析(一)HttpRequestDecoder的实现
- kafka源码剖析(二)之kafka-server的启动
- SpringMVC源码剖析(五)-消息转换器HttpMessageConverter
- Asp.net web Api源码分析-HttpActionDescriptor的创建
- 【源码剖析】tinyhttpd —— C 语言实现最简单的 HTTP 服务器