Web API 源码剖析之默认配置(HttpConfiguration)

我们在上一节讲述了全局配置和初始化。本节我们将就全局配置的Configuration只读属性进行展开,她是一个类型为HttpConfiguration。 它在Web Api 主要为整个API 做一些最基础的工作,比如定义默认

  1. 路由表(Routes)
  2. 过滤器(Filters)
  3. 默认消息处理程序(MessageHandlers)
  4. 属性字典(Properties)
  5. 依赖注入解耦器(DependencyResolver)
  6. 错误处理策略(IncludeErrorDetailPolicy)
  7. 服务(Services,这里的服务是之为Web ApI 框架的服务对应的接口和实现)
  8. 媒体格式程序(Formatters)
  9. 参数绑定规则(ParameterBindingRules)。

以上 就是配置的属性。 接下来将就部分属性展开。

Formatters

默认格式化程序,是一个MediaTypeFormatterCollection类型。

API 里定义4个默认格式:

  1. JsonMediaTypeFormatter:对应的是用来处理请求头是application/json或text/json格式,
  2. XmlMediaTypeFormatter:对应的是用来处理请求头是application/xml格式
  3. FormUrlEncodedMediaTypeFormatter:对应的是用来处理请求头是application/x-www-form-urlencoded,
  4. 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(); 
       }

默认的Action绑定规则:ParameterBindingRules

ParameterBindingRules = DefaultActionValueBinder.GetDefaultParameterBinders();

有兴趣的朋友可以下载web Api 源码查看。http://aspnetwebstack.codeplex.com/wikipage?title=Contributors.

Web API 源码剖析之默认配置(HttpConfiguration)的更多相关文章

  1. Web API 源码剖析之默认消息处理程序链之路由分发器(HttpRoutingDispatcher)

    Web API 源码剖析之默认消息处理程序链-->路由分发器(HttpRoutingDispatcher) 我们在上一节讲述了默认的DefaultServer(是一个类型为HttpServer的 ...

  2. Web API 源码剖析之全局配置

    Web API 源码剖析之全局配置 Web API  均指Asp.net Web API .本节讲述的是基于Web API 系统在寄宿于IIS. 本节主要讲述Web API全局配置.它是如何优雅的实现 ...

  3. Web API 源码剖析之默认消息处理程序链--》路由分发器(HttpRoutingDispatcher)

    我们在上一节讲述了默认的DefaultServer(是一个类型为HttpServer的只读属性,详情请参考 Web API 源码剖析之全局配置).本节将讲述DefaultHandler(是一个Http ...

  4. Web API源码剖析之HttpServer

    Web API源码剖析之HttpServer 上一节我们讲述全局配置.本节将讲述全局配置的DefaultServer,它是一个HttpServer类型. 主要作用就是接受每一次请求,然后分发给消息处理 ...

  5. Web Api源码(路由注册)

    这篇文章只是我学习Web API框架的输出,学习方法还是输出倒逼输入比较行得通,所以不管写的好不好,坚持下去,肯定有收获.篇幅比较长,仔细思考阅读下来大约需要几分钟. 做.NET开发有好几年时间了,从 ...

  6. STL源码剖析之空间配置器

    本文大致对STL中的空间配置器进行一个简单的讲解,由于只是一篇博客类型的文章,无法将源码表现到面面俱到,所以真正感兴趣的码农们可以从源码中或者<STL源码剖析>仔细了解一下. 1,为什么S ...

  7. 《STL源码剖析》环境配置

    首先,去侯捷网站下载相关文档:http://jjhou.boolan.com/jjwbooks-tass.htm. 这本书采用的是Cygnus C++ 2.91 for windows.下载地址:ht ...

  8. 长期作业:web框架源码剖析

    Tornado框架 1.1. 手动安装 1.2. 从简单的开始:分析红框部分的源码 Django框架

  9. WorldWind源码剖析系列:配置载入器类ConfigurationLoader

    配置载入器类ConfigurationLoader主要从指定的路径中加载保存星球相关参数的xml文件,从中读取数据来构造星球对象及其所关联的可渲染子对象列表并返回.该类的类图如下所示. 该类所包含的主 ...

随机推荐

  1. HTTP Header之Content-Type

    HTTP Header之Content-Type   目录 1. HTTP Header 2. 文件请求和接口请求 3. 几种 Content-Type 3.1 application/x-www-f ...

  2. 触电(by quqi99)

    高压电线杆相关的触电方式主要是两种: 一是跨步电压,高压电线落在地面时,如果人恰好在这个范围内步行时,就会从一只脚到跨下再到另一只脚到地形成回路,这叫跨步电压.步子越大,电压越大(以落地点为圆心向外电 ...

  3. tmux安装与使用

    安装 用法 重点 一prefix前缀键 二window和pane的区分 tmux 按照官方给出的介绍是:终端复用工具.说白了就是可以仅仅在开启一个终端的情况下同时处理多个任务. 比如下面我设置的这样一 ...

  4. [LeetCode&Python] Problem 561. Array Partition I

    Given an array of 2n integers, your task is to group these integers into n pairs of integer, say (a1 ...

  5. PS如何批量处理图片

    喜爱摄影的朋友可能都有这样的体会,相机里面存了大量的图片,一般都是2048×1536或者更大像素的照片,每张都有1M以上,如果设置的清晰度高,则照片就更大,这样的图片是无法上传到博客中的(博客要求每张 ...

  6. CodeForces - 1097D:Makoto and a Blackboard (积性)

    Makoto has a big blackboard with a positive integer n written on it. He will perform the following a ...

  7. beautiful number 数位DP codeforces 55D

    题目链接: http://codeforces.com/problemset/problem/55/D 数位DP 题目描述: 一个数能被它每位上的数字整除(0除外),那么它就是beautiful nu ...

  8. Python3中 sys.argv的用法

    sys.avgr 是一个Python的引用模块.刚好做一个作业需要用到它,在sublime上编辑后运行,试图从结果发现它的用途,然而结果一直都是没结果. 后面在网上查了资料,才明白过来.sys.arg ...

  9. CTF-练习平台-Misc之 细心的大象

    十五.细心的大象 打开图片属性 发现备注里有短信息,看着也不像flag,仔细观察里面只有只有大小写字母和数字应该是base64编码,解密后得到:MSDS456ASD123zz 好像也不是flag,题目 ...

  10. Hash表的平均查找长度ASL计算方法

    Hash表的“查找成功的ASL”和“查找不成功的ASL” ASL指的是 平均查找时间 关键字序列:(7.8.30.11.18.9.14) 散列函数: H(Key) = (key x 3) MOD 7 ...