3.3.2 HttpSelfHostServer(1)

424 篇文章 1 订阅
348 篇文章 0 订阅

您所在的位置:读书频道 >设计开发 > .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为“龙头”的消息处理管道本身具有请求监听、接收、处理和响应的能力。


  
  
  1. public sealed class HttpSelfHostServer : HttpServer  
  2. {  
  3. public HttpSelfHostServer(HttpSelfHostConfiguration configuration);  
  4. public HttpSelfHostServer(HttpSelfHostConfiguration configuration,  
  5. HttpMessageHandler dispatcher);  
  6. public Task OpenAsync();  
  7. public Task CloseAsync();  
  8. 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 返回。
 

  
  
  1. public class HttpSelfHostConfiguration : HttpConfiguration  
  2. {  
  3. public HttpSelfHostConfiguration(Uri baseAddress);  
  4. public Uri BaseAddress { get; }  
  5. public HostNameComparisonMode HostNameComparisonMode { get; set; }  
  6. public TransferMode TransferMode { get; set; }  
  7. public int MaxBufferSize { get; set; }  
  8. public int MaxConcurrentRequests { get; set; }  
  9. public long MaxReceivedMessageSize { get; set; }  
  10. public TimeSpan ReceiveTimeout { get; set; }  
  11. public TimeSpan SendTimeout { get; set; }  
  12. public HttpClientCredentialType ClientCredentialType { get; set; }  
  13. public UserNamePasswordValidator UserNamePasswordValidator { get; set; }  
  14. public X509CertificateValidator X509CertificateValidator { get; set; }  


由于Self Host 寄宿模式下请求的监听、接收和响应基本上全部是通过HttpBinding 实现的,所以定义在HttpSelfHostConfiguration 中的众多属性实际上基本都用于对创建的HttpBinding 进行配置。从如下给出的代码片段可以看出,HttpBinding 类型与HttpSelfHostConfiguration 具有类似的属性定义。


  
  
  1. public abstract class Binding : IDefaultCommunicationTimeouts  
  2. {  
  3. //其他成员  
  4. public TimeSpan ReceiveTimeout { get; set; }  
  5. public TimeSpan SendTimeout { get; set; }  
  6. }  
  7. public class HttpBinding : Binding, IBindingRuntimePreferences  
  8. {  
  9. //其他成员  
  10. public HostNameComparisonMode HostNameComparisonMode { get; set; }  
  11. public TransferMode TransferMode { get; set; }  
  12. public long MaxBufferPoolSize { get; set; }  
  13. public int MaxBufferSize { get; set; }  
  14. public long MaxReceivedMessageSize { get; set; }  
  15. public HttpBindingSecurity Security { get; set; }  


由于Binding 在WCF 中是一个核心组件,其设计本身相对复杂,要深入了解定义在HttpBinding 中的这些属性需要相关的背景知识。篇幅所限,我们不能因为这些属性将Binding相关的内容全部搬过来,所以在这里我们仅仅通过表3-1 对它们进行概括性的介绍。


表3-1 定义在HttpSelfHostConfiguration 中的配置属性
 



续表
 



喜欢的朋友可以添加我们的微信账号:


51CTO读书频道二维码




51CTO读书频道活动讨论群:342347198


【责任编辑: book TEL:(010)68476606】


回书目   上一节   下一节




2014-08-19 21:39 蒋金楠 电子工业出版社 字号: T | T
一键收藏,随时查看,分享好友!

《ASP.NET Web API 2框架揭秘》本书详细讲解了ASP.NET Web API 从接收请求到响应回复的整个流程,包括路由、Http Controller 的激活、Action 方法的选择与执行、参数的绑定与验证、过滤器的执行和安全等相关的机制。本节为大家介绍HttpSelfHostServer。

AD:51CTO 网+ 第十二期沙龙:大话数据之美_如何用数据驱动用户体验

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值