HttpControllerDispatcher作为ASPNET WEB API消息处理管道中重要的部分,负责最后控制器系统的激活,action方法的执行,以及最后的响应生成.
HtppControllerDispatcher继承HttpMessageHandler,重载了sendAsync方法.
}
public class HttpControllerDispatcher : HttpMessageHandler
{
//some
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken){
//1.内部调用SendAsncCore方法
//2.其他处理
} private Task<HttpResponseMessage> SendAsyncCore(HttpRequestMessage request, CancellationToken cancellationToken){
//1. HttpRequestMessage::GetRouteData 获取请求消息相关的路由信息,如果不存在直接return
//2. ControllerSelector::SelectController 获取关联控制器描述符 HttpControllerDescriptor
//3. HttpControllerDescriptor::CreateController 创建目标控制器对象 IHttpController
//4. 根据配置,请求上下文执行IHttpController::ExecuteAsync
}
由上可知HttpControllerDispatcher::SendAsyncCore工作
1. 请求路由数据验证
2. 激活控制器系统,获取控制器对象IHttpController
3. 执行,返回响应数据
控制器系统激活由HttpControllerDescripto::CreateController完成
public class HttpControllerDescriptor{
public virtual IHttpController CreateController(HttpRequestMessage request){
/*
1. 通过HttpConfiguration.Services.GetHttpControllerActivator方法获取控制器激活对象
IHttpControllerActivator 2. IhttpControllerActivator::Create 创建目标控制器对象
*/ }
}
HttpConfiguration.Services是个服务容器,在整个消息管道中使用了IOC技术用于服务对象注册
HttpControllerDescriptor包含了关于控制相关信息,例如:控制器名,控制器类型