Self Host模式下的ASP.NET Web API与WCF非常相似,都可以寄宿在任意类型的托管应用程序中,宿主可以是Windows Form 、WPF、控制台应用以及Windows Service,寄宿的请求的监听、接收 和响应功能,是通过一个类型为HttpBinding的Binding对象创建的ChannelListener管道来完成的。
一、Binding绑定模型
Binding用来创建处理和传输消息的信道栈,信道栈有一组Channel组成,Binding也由一组BindingElement组成,每个BindingElement会创建一个ChannelListener,ChannelListener再创建对应的Channel,而每个Channel负责处理消息的单独一块功能,Binding启动时候会创建多个Channel组成一个消息处理管道,对请求依次进行处理,对相应按相反方向进行处理,有点类似ASP.NET Web API的消息处理管道,而这是宿主内部的功能,要注意区分。
//初始化HttpBinding public HttpBinding() { Initialize(); }
public HttpBinding(HttpBindingSecurityMode securityMode) : this() { _security.Mode = securityMode; }
//从URL中确定主机名的比较模式
public HostNameComparisonMode HostNameComparisonMode
{ get { return _httpTransportBindingElement.HostNameComparisonMode; }
set { _httpTransportBindingElement.HostNameComparisonMode = value; _httpsTransportBindingElement.HostNameComparisonMode = value; } }
public long MaxBufferPoolSize { get { return _httpTransportBindingElement.MaxBufferPoolSize; }
set { _httpTransportBindingElement.MaxBufferPoolSize = value; _httpsTransportBindingElement.MaxBufferPoolSize = value; } }
//Buffered模式下的消息的最大缓冲区大小,Buffered模式,即消息先会保存于内存缓冲区后一并传输 [DefaultValue(TransportDefaults.MaxBufferSize)] publicint MaxBufferSize { get { return _httpTransportBindingElement.MaxBufferSize; }
set { _httpTransportBindingElement.MaxBufferSize = value; _httpsTransportBindingElement.MaxBufferSize = value; } }
//请求消息的最大尺寸,默认为65536 [DefaultValue(TransportDefaults.MaxReceivedMessageSize)] public long MaxReceivedMessageSize { get { return _httpTransportBindingElement.MaxReceivedMessageSize; }
set { _httpTransportBindingElement.MaxReceivedMessageSize = value; _httpsTransportBindingElement.MaxReceivedMessageSize = value; } }
publicAction<HttpTransportBindingElement> ConfigureTransportBindingElement { get { return _configureTransportBindingElement; }
set { if (value == null) { throw Error.PropertyNull(); }
_configureTransportBindingElement = value; } }
//传输模式
[DefaultValue(HttpTransportDefaults.TransferMode)] publicTransferMode TransferMode { get { return _httpTransportBindingElement.TransferMode; }
set { _httpTransportBindingElement.TransferMode = value; _httpsTransportBindingElement.TransferMode = value; } }
Binding处理管道中处理的消息是HttpMessage,其继承自Message,它是对ASP.NET Web API处理的消息HttpRequestMessage和HttpResponseMessage的封装;HttpMessageEncoder对请求消息解码后得到HttpMessage对象,其会转成—个HttpRequestMessage对象并传入ASP.NET WebAPI消息处理管道进行处理,处理完后返回HttpResponseMessage对象,其被封装成HttpMessage对象,在通过传输层将响应返回给客户端之前,需要利用HttpMessageEncoder对其进行编码,然后进行传输。
public HttpMessage(HttpRequestMessage request) { Contract.Assert(request != null, "The 'request' parameter should not be null."); _request = request; Headers.To = request.RequestUri; IsRequest = true; }
//响应对象为参数,进行封装
public HttpMessage(HttpResponseMessage response) { Contract.Assert(response != null, "The 'response' parameter should not be null."); _response = response; IsRequest = false; }
public overrideMessageVersion Version { get { EnsureNotDisposed(); returnMessageVersion.None; } }
public overrideMessageHeaders Headers { get { EnsureNotDisposed(); if (_headers == null) { _headers = newMessageHeaders(MessageVersion.None); }
return _headers; } }
public overrideMessageProperties Properties { get { EnsureNotDisposed(); if (_properties == null) { _properties = new MessageProperties(); _properties.AllowOutputBatching = false; }
return _properties; } }
public override bool IsEmpty { get { long? contentLength = GetHttpContentLength(); return contentLength.HasValue && contentLength.Value == 0; } }
public override bool IsFault { get { return false; } }
publicHttpRequestMessage GetHttpRequestMessage(bool extract) { EnsureNotDisposed(); Contract.Assert(IsRequest, "This method should only be called when IsRequest is true."); if (extract) { HttpRequestMessage req = _request;
publicHttpResponseMessage GetHttpResponseMessage(bool extract) { EnsureNotDisposed(); Contract.Assert(!IsRequest, "This method should only be called when IsRequest is false."); if (extract) { HttpResponseMessage res = _response;
public class HttpSelfHostConfiguration : HttpConfiguration { public HttpSelfHostConfiguration(string baseAddress) : this(CreateBaseAddress(baseAddress)) { }
//基地址Uri publicUri BaseAddress { get { return _baseAddress; } }
//最大请求并发量,默认值是100,若是多处理器,要cheny乘以处理器个数 public int MaxConcurrentRequests { get { return _maxConcurrentRequests; }
set { if (value < MinConcurrentRequests) { throwError.ArgumentMustBeGreaterThanOrEqualTo("value", value, MinConcurrentRequests); } _maxConcurrentRequests = value; } }
//消息传输模式,分Streamed和Buffered(默认) publicTransferMode TransferMode { get { return _transferMode; }
set { TransferModeHelper.Validate(value, "value"); _transferMode = value; } }
//从URI中获取主机名的匹配比较模式 publicHostNameComparisonMode HostNameComparisonMode { get { return _hostNameComparisonMode; }
set { HostNameComparisonModeHelper.Validate(value, "value"); _hostNameComparisonMode = value; } }
//Buffered模式的最大缓冲池大小,默认值65536
public int MaxBufferSize { get { if (_maxBufferSizeIsInitialized || TransferMode != TransferMode.Buffered) { return _maxBufferSize; }
long maxReceivedMessageSize = MaxReceivedMessageSize; if (maxReceivedMessageSize > Int32.MaxValue) { returnInt32.MaxValue; } return (int)maxReceivedMessageSize; }
set { if (value < MinBufferSize) { throwError.ArgumentMustBeGreaterThanOrEqualTo("value", value, MinBufferSize); } _maxBufferSizeIsInitialized = true; _maxBufferSize = value; } }
//允许请求消息的最大大小,默认为65536
public long MaxReceivedMessageSize { get { return _maxReceivedMessageSize; }
set { if (value < MinReceivedMessageSize) { throwError.ArgumentMustBeGreaterThanOrEqualTo("value", value, MinReceivedMessageSize); } _maxReceivedMessageSize = value; } }
//接收请求消息的超时时间,默认为10分钟
publicTimeSpan ReceiveTimeout { get { return _receiveTimeout; }
set { if (value < TimeSpan.Zero) { throwError.ArgumentMustBeGreaterThanOrEqualTo("value", value, TimeSpan.Zero); }
_receiveTimeout = value; } }
//发送响应消息的超时时间,默认为1分钟
publicTimeSpan SendTimeout { get { return _sendTimeout; }
set { if (value < TimeSpan.Zero) { throwError.ArgumentMustBeGreaterThanOrEqualTo("value", value, TimeSpan.Zero); }
_sendTimeout = value; } }
//客户端采用的用户凭证类型 publicHttpClientCredentialType ClientCredentialType { get { return _clientCredentialType; } set { _clientCredentialType = value; } }
请发表评论