I am talking here based on the latest bits which are available on
Codeplex ASP.NET Web Stack repo.
该命令由用户控制,没有任意命令。 让我解释:
Let s say we have two message handlers: MyMessageHandler
and MyMessageHandler2
. Assuming that we register them as below:
protected void Application_Start(object sender, EventArgs e) {
RouteConfig.RegisterRoutes(GlobalConfiguration.Configuration.Routes);
GlobalConfiguration.Configuration.MessageHandlers.Add(new MyMessageHandler());
GlobalConfiguration.Configuration.MessageHandlers.Add(new MyMessageHandler2());
}
What you expect here is for the MyMessageHandler
to run first and MyMessageHandler2
as second one, in other words FIFO.
如果我们看一看框架中的一环,我们将看到<条码>>><条码>方法>。 HttpServer instance isciting CreatePipeline
methods of System.Net.Http.HttpClientFactory
(以前称为<代码>) HttpPipelineFactory.Create methods as Ali indicated.)CreatePipeline
方法接受两个参数:HttpMessageHandler
和IE数<DelegatingHandler>
。 <代码>HttpServer.Initialize方法是通过<编码>System.Web.Http.Displer,作为最后<编码>。 HttpMessageHandler within the chain and HttpConfiguration. 信号Handlers for IEvidable<DelegatingHandler>
para amount.
<代码>CreatePipeline内发生的情况 这种方法非常切合海事组织:
public static HttpMessageHandler CreatePipeline(HttpMessageHandler innerHandler, IEnumerable<DelegatingHandler> handlers)
{
if (innerHandler == null)
{
throw Error.ArgumentNull("innerHandler");
}
if (handlers == null)
{
return innerHandler;
}
// Wire handlers up in reverse order starting with the inner handler
HttpMessageHandler pipeline = innerHandler;
IEnumerable<DelegatingHandler> reversedHandlers = handlers.Reverse();
foreach (DelegatingHandler handler in reversedHandlers)
{
if (handler == null)
{
throw Error.Argument("handlers", Properties.Resources.DelegatingHandlerArrayContainsNullItem, typeof(DelegatingHandler).Name);
}
if (handler.InnerHandler != null)
{
throw Error.Argument("handlers", Properties.Resources.DelegatingHandlerArrayHasNonNullInnerHandler, typeof(DelegatingHandler).Name, "InnerHandler", handler.GetType().Name);
}
handler.InnerHandler = pipeline;
pipeline = handler;
}
return pipeline;
}
如你所知,电文手令被推翻,。 Matryoshka doll 这里虽已建立,但需谨慎:确保<代码>。 HttpControllerDispader 是链条内最后的电传手。
至于两个问题,这实际上并不完全。 贺电手得双倍,而你提供的持续方法则会是这样。 你们有责任做到这一点。 如果你提供反馈(换言之,继续),你的信息处理员将被要求向客户提供你可以提供的回复信息。
例如,请假设以下两点是上文所述的信息处理器:
public class MyMessageHandler : DelegatingHandler {
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, System.Threading.CancellationToken cancellationToken) {
//inspect request here
return base.SendAsync(request, cancellationToken).ContinueWith(task => {
//inspect the generated response
var response = task.Result;
return response;
});
}
}
这是另一个:
public class MyMessageHandler2 : DelegatingHandler {
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, System.Threading.CancellationToken cancellationToken) {
//inspect request here
return base.SendAsync(request, cancellationToken).ContinueWith(task => {
//inspect the generated response
var response = task.Result;
return response;
});
}
}
随着我们的持续,我们的信息处理员将回到劳工组织命令的客户手中。 因此,<代码>内的延续方法 MyMessageHandler2将是背路上首先要援引的,是内的。 MyMessageHandler
将是第二个。