您所在的位置:读书频道 >设计开发 > .Net系列 > 3.3.2 HttpSelfHostServer(1)
3.3.2 HttpSelfHostServer(1)
3.3.2 HttpSelfHostServer(1)
通过本书第1 章的介绍我们知道,ASP.NET Web API 的Self Host 寄宿模式是通过一个System.Web.Http.SelfHost.HttpSelfHostServer 对象来完成的,那么HttpSelfHostServer 与前面介绍的HttpBinding 又有何关系呢?HttpSelfHostServer 与ASP.NET Web API 的消息处理管道又是如何集成的呢?
如下面的代码片段所示,HttpSelfHostServer 其实是HttpServer 的子类,所以在Self Host模式下HttpSelfHostServer 本身就是消息管道的组成部分。换句话说,以HttpSelfHostServer为“龙头”的消息处理管道本身具有请求监听、接收、处理和响应的能力。
- public sealed class HttpSelfHostServer : HttpServer
- {
- public HttpSelfHostServer(HttpSelfHostConfiguration configuration);
- public HttpSelfHostServer(HttpSelfHostConfiguration configuration,
- HttpMessageHandler dispatcher);
- public Task OpenAsync();
- public Task CloseAsync();
- protected override void Dispose(bool disposing);
- }
ASP.NET Web API 的消息处理管道的配置利用通过HttpServer 的Configuration 属性表示的HttpConfiguration 对象来完成,对于HttpSelfHostServer 来说,它的Configuration 属性返回一个HttpSelfHostConfiguration 对象(HttpSelfHostConfiguration 类型定义在“System.Web.Http.SelfHost”命名空间下)。如上面的代码片段所示,当我们调用构造函数创建一个HttpSelfHostServer 对象时,需要通过参数指定此HttpSelfHostConfiguration。
HttpSelfHostConfiguration
如下面的代码片段所示,HttpSelfHostConfiguration 直接继承自HttpConfiguration。我们在创建一个HttpSelfHostConfiguration 对象的时候需要指定一个Uri 对象作为监听基地址,这个地址通过只读属性BaseAddress 返回。
- public class HttpSelfHostConfiguration : HttpConfiguration
- {
- public HttpSelfHostConfiguration(Uri baseAddress);
- public Uri BaseAddress { get; }
- public HostNameComparisonMode HostNameComparisonMode { get; set; }
- public TransferMode TransferMode { get; set; }
- public int MaxBufferSize { get; set; }
- public int MaxConcurrentRequests { get; set; }
- public long MaxReceivedMessageSize { get; set; }
- public TimeSpan ReceiveTimeout { get; set; }
- public TimeSpan SendTimeout { get; set; }
- public HttpClientCredentialType ClientCredentialType { get; set; }
- public UserNamePasswordValidator UserNamePasswordValidator { get; set; }
- public X509CertificateValidator X509CertificateValidator { get; set; }
- }
由于Self Host 寄宿模式下请求的监听、接收和响应基本上全部是通过HttpBinding 实现的,所以定义在HttpSelfHostConfiguration 中的众多属性实际上基本都用于对创建的HttpBinding 进行配置。从如下给出的代码片段可以看出,HttpBinding 类型与HttpSelfHostConfiguration 具有类似的属性定义。
- public abstract class Binding : IDefaultCommunicationTimeouts
- {
- //其他成员
- public TimeSpan ReceiveTimeout { get; set; }
- public TimeSpan SendTimeout { get; set; }
- }
- public class HttpBinding : Binding, IBindingRuntimePreferences
- {
- //其他成员
- public HostNameComparisonMode HostNameComparisonMode { get; set; }
- public TransferMode TransferMode { get; set; }
- public long MaxBufferPoolSize { get; set; }
- public int MaxBufferSize { get; set; }
- public long MaxReceivedMessageSize { get; set; }
- public HttpBindingSecurity Security { get; set; }
- }
由于Binding 在WCF 中是一个核心组件,其设计本身相对复杂,要深入了解定义在HttpBinding 中的这些属性需要相关的背景知识。篇幅所限,我们不能因为这些属性将Binding相关的内容全部搬过来,所以在这里我们仅仅通过表3-1 对它们进行概括性的介绍。
表3-1 定义在HttpSelfHostConfiguration 中的配置属性
续表
喜欢的朋友可以添加我们的微信账号:
51CTO读书频道二维码
51CTO读书频道活动讨论群:342347198
回书目 上一节 下一节 |
《ASP.NET Web API 2框架揭秘》本书详细讲解了ASP.NET Web API 从接收请求到响应回复的整个流程,包括路由、Http Controller 的激活、Action 方法的选择与执行、参数的绑定与验证、过滤器的执行和安全等相关的机制。本节为大家介绍HttpSelfHostServer。