Web API 源码剖析之默认配置(HttpConfiguration)
2016-12-11 22:15
344 查看
我们在上一节讲述了全局配置和初始化。本节我们将就全局配置的Configuration只读属性进行展开,她是一个类型为HttpConfiguration。 它在Web Api 主要为整个API 做一些最基础的工作,比如定义默认
路由表(Routes)
过滤器(Filters)
默认消息处理程序(MessageHandlers)
属性字典(Properties)
依赖注入解耦器(DependencyResolver)
错误处理策略(IncludeErrorDetailPolicy)
服务(Services,这里的服务是之为Web ApI 框架的服务对应的接口和实现)
媒体格式程序(Formatters)
参数绑定规则(ParameterBindingRules)。
以上 就是配置的属性。 接下来将就部分属性展开。
API 里定义4个默认格式:
JsonMediaTypeFormatter:对应的是用来处理请求头是application/json或text/json格式,
XmlMediaTypeFormatter:对应的是用来处理请求头是application/xml格式
FormUrlEncodedMediaTypeFormatter:对应的是用来处理请求头是application/x-www-form-urlencoded,
JQueryMvcFormUrlEncodedFormatter
有兴趣的朋友可以下载web Api 源码查看。http://aspnetwebstack.codeplex.com/wikipage?title=Contributors.
下面将继续讲解剖析HttpServer。
路由表(Routes)
过滤器(Filters)
默认消息处理程序(MessageHandlers)
属性字典(Properties)
依赖注入解耦器(DependencyResolver)
错误处理策略(IncludeErrorDetailPolicy)
服务(Services,这里的服务是之为Web ApI 框架的服务对应的接口和实现)
媒体格式程序(Formatters)
参数绑定规则(ParameterBindingRules)。
以上 就是配置的属性。 接下来将就部分属性展开。
Formatters
默认格式化程序,是一个MediaTypeFormatterCollection类型。API 里定义4个默认格式:
JsonMediaTypeFormatter:对应的是用来处理请求头是application/json或text/json格式,
XmlMediaTypeFormatter:对应的是用来处理请求头是application/xml格式
FormUrlEncodedMediaTypeFormatter:对应的是用来处理请求头是application/x-www-form-urlencoded,
JQueryMvcFormUrlEncodedFormatter
Services
默认服务定义如下: public DefaultServices(HttpConfiguration configuration)
{
if (configuration == null)
{
throw Error.ArgumentNull("configuration");
}
_configuration = configuration;
// Initialize the dictionary with all known service types, even if the list for that service type is
// empty, because we will throw if the developer tries to read or write unsupported types.
SetSingle<IActionValueBinder>(new DefaultActionValueBinder());
SetSingle<IApiExplorer>(new ApiExplorer(configuration));
SetSingle<IAssembliesResolver>(new DefaultAssembliesResolver());
SetSingle<IBodyModelValidator>(new DefaultBodyModelValidator());
SetSingle<IContentNegotiator>(new DefaultContentNegotiator());
SetSingle<IDocumentationProvider>(null); // Missing
SetMultiple<IFilterProvider>(new ConfigurationFilterProvider(),
new ActionDescriptorFilterProvider());
SetSingle<IHostBufferPolicySelector>(null);
SetSingle<IHttpActionInvoker>(new ApiControllerActionInvoker());
SetSingle<IHttpActionSelector>(new ApiControllerActionSelector());
SetSingle<IHttpControllerActivator>(new DefaultHttpControllerActivator());
SetSingle<IHttpControllerSelector>(new DefaultHttpControllerSelector(configuration));
SetSingle<IHttpControllerTypeResolver>(new DefaultHttpControllerTypeResolver());
SetSingle<ITraceManager>(new TraceManager());
SetSingle<ITraceWriter>(null);
// This is a priority list. So put the most common binders at the top.
SetMultiple<ModelBinderProvider>(new TypeConverterModelBinderProvider(),
new TypeMatchModelBinderProvider(),
new KeyValuePairModelBinderProvider(),
new ComplexModelDtoModelBinderProvider(),
new ArrayModelBinderProvider(),
new DictionaryModelBinderProvider(),
new CollectionModelBinderProvider(),
new MutableObjectModelBinderProvider());
SetSingle<ModelMetadataProvider>(new DataAnnotationsModelMetadataProvider());
SetMultiple<ModelValidatorProvider>(new DataAnnotationsModelValidatorProvider(),
new DataMemberModelValidatorProvider());
// This is an ordered list,so put the most common providers at the top.
SetMultiple<ValueProviderFactory>(new QueryStringValueProviderFactory(),
new RouteDataValueProviderFactory());
ModelValidatorCache validatorCache = new ModelValidatorCache(new Lazy<IEnumerable<ModelValidatorProvider>>(() => this.GetModelValidatorProviders()));
SetSingle<IModelValidatorCache>(validatorCache);
SetSingle<IExceptionHandler>(new DefaultExceptionHandler());
SetMultiple<IExceptionLogger>();
_serviceTypesSingle = new HashSet<Type>(_defaultServicesSingle.Keys);
_serviceTypesMulti = new HashSet<Type>(_defaultServicesMulti.Keys);
// Reset the caches and the known dependency scope
ResetCache();
}
{
if (configuration == null)
{
throw Error.ArgumentNull("configuration");
}
_configuration = configuration;
// Initialize the dictionary with all known service types, even if the list for that service type is
// empty, because we will throw if the developer tries to read or write unsupported types.
SetSingle<IActionValueBinder>(new DefaultActionValueBinder());
SetSingle<IApiExplorer>(new ApiExplorer(configuration));
SetSingle<IAssembliesResolver>(new DefaultAssembliesResolver());
SetSingle<IBodyModelValidator>(new DefaultBodyModelValidator());
SetSingle<IContentNegotiator>(new DefaultContentNegotiator());
SetSingle<IDocumentationProvider>(null); // Missing
SetMultiple<IFilterProvider>(new ConfigurationFilterProvider(),
new ActionDescriptorFilterProvider());
SetSingle<IHostBufferPolicySelector>(null);
SetSingle<IHttpActionInvoker>(new ApiControllerActionInvoker());
SetSingle<IHttpActionSelector>(new ApiControllerActionSelector());
SetSingle<IHttpControllerActivator>(new DefaultHttpControllerActivator());
SetSingle<IHttpControllerSelector>(new DefaultHttpControllerSelector(configuration));
SetSingle<IHttpControllerTypeResolver>(new DefaultHttpControllerTypeResolver());
SetSingle<ITraceManager>(new TraceManager());
SetSingle<ITraceWriter>(null);
// This is a priority list. So put the most common binders at the top.
SetMultiple<ModelBinderProvider>(new TypeConverterModelBinderProvider(),
new TypeMatchModelBinderProvider(),
new KeyValuePairModelBinderProvider(),
new ComplexModelDtoModelBinderProvider(),
new ArrayModelBinderProvider(),
new DictionaryModelBinderProvider(),
new CollectionModelBinderProvider(),
new MutableObjectModelBinderProvider());
SetSingle<ModelMetadataProvider>(new DataAnnotationsModelMetadataProvider());
SetMultiple<ModelValidatorProvider>(new DataAnnotationsModelValidatorProvider(),
new DataMemberModelValidatorProvider());
// This is an ordered list,so put the most common providers at the top.
SetMultiple<ValueProviderFactory>(new QueryStringValueProviderFactory(),
new RouteDataValueProviderFactory());
ModelValidatorCache validatorCache = new ModelValidatorCache(new Lazy<IEnumerable<ModelValidatorProvider>>(() => this.GetModelValidatorProviders()));
SetSingle<IModelValidatorCache>(validatorCache);
SetSingle<IExceptionHandler>(new DefaultExceptionHandler());
SetMultiple<IExceptionLogger>();
_serviceTypesSingle = new HashSet<Type>(_defaultServicesSingle.Keys);
_serviceTypesMulti = new HashSet<Type>(_defaultServicesMulti.Keys);
// Reset the caches and the known dependency scope
ResetCache();
}
默认的Action绑定规则:ParameterBindingRules
ParameterBindingRules = DefaultActionValueBinder.GetDefaultParameterBinders();有兴趣的朋友可以下载web Api 源码查看。http://aspnetwebstack.codeplex.com/wikipage?title=Contributors.
下面将继续讲解剖析HttpServer。
相关文章推荐
- Web API 源码剖析之默认配置(HttpConfiguration)
- Web API 源码剖析之默认消息处理程序链--》路由分发器(HttpRoutingDispatcher)
- Web API 源码剖析之默认消息处理程序链之路由分发器(HttpRoutingDispatcher)
- Web API 源码剖析之全局配置
- Web API 源码剖析之全局配置
- Node 进阶:express 默认日志组件 morgan 从入门使用到源码剖析
- Redis源码剖析和注释(二十五)--- Redis Cluster 的通信流程深入剖析(载入配置文件、节点握手、分配槽)
- Spark读取配置源码剖析
- WorldWind源码剖析系列:配置载入器类ConfigurationLoader
- Nginx源码剖析--HTTP模块配置信息的merge
- STL源码剖析学习笔记之具备次配置力(sub-allocation)的SGI空间配置器
- 菜鸟nginx源码剖析 配置与部署篇(一) 手把手实现nginx "I love you"
- STL源码剖析_读书笔记:第二章 空间配置器 一级和二级配置器篇
- Android源码配置默认输入法
- Struts2-day01 环境搭建 源码导入 配置文件 默认action,method 详解Action 封装数据的方式
- Spring的AOP实现方式—ProxyFactoryBean配置方式实现源码剖析
- 菜鸟nginx源码剖析 配置与部署篇(一) 手把手实现nginx "I love you"
- 菜鸟nginx源码剖析 配置与部署篇(一) 手把手实现nginx "I love you"
- Nginx 模块自主开发六:源码剖析配置文件解析过程
- SGISTL源码探究-默认使用的配置器