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.

下面将继续讲解剖析HttpServer。

时间: 2024-08-09 08:09:26

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

Web API 源码剖析之全局配置

Web API  均指Asp.net Web API .本节讲述的是基于Web API 系统在寄宿于IIS. 本节主要讲述Web API全局配置.它是如何优雅的实现这个配置.做过MVC 都知道Global文件来初始化.Web API 本质上也是ASP.NET applications.所以也是在Global定义里一个GlobalConfiguration静态类.该类作用就是初始化ASP.NET applications.如下是GlobalConfiguration的定义. //    // 摘要

Web API源码剖析之HttpServer

上一节我们讲述全局配置.本节将讲述全局配置的DefaultServer,它是一个HttpServer类型. 主要作用就是接受每一次请求,然后分发给消息处理程序链依次处理.从HttpServer定义可以看出,其本质是一个消息处理程序,其继承于DelegatingHandler.从其代码定义如下:      //参数为      public HttpServer(HttpConfiguration configuration, HttpMessageHandler dispatcher);    

STL源码剖析——空间的配置与释放

C++的内存配置基本操作是  ::operator new(),内存释放的基本操作是 ::operator delete().这两个全局函数相当于C的malloc()和free()函数.是的,正是如此,STL正是以malloc()和free()完成内存的配置与释放. 但是考虑到小型区块所可能造成的内存破碎问题,STL中设计了双层级配置器, 第一级配置器直接使用malloc()和free(),第二级配置器则视情况采用不用的策略:当配置区块超过128bytes时,视之为"足够大",便调用第

《STL源码剖析》空间配置器

空间配置器(allocator) 空间配置器按我的理解就是C++ STL进行内存管理的组件(包括内存的申请和释放):当然,不只是内存,还可以向硬盘申请空间: 我主要看了内存的配置与释放(这里"配置"就应该是"申请"的意思).STL对此设计的哲学主要包括以下四个方面: 1.向系统堆空间申请内存空间 2.考虑了多线程的情况下的申请: 3.考虑内存不足的应变措施: 4.考虑过多"小型区块"的内存碎片的问题: C++ 申请的基本动作是::operator

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

配置载入器类ConfigurationLoader主要从指定的路径中加载保存星球相关参数的xml文件,从中读取数据来构造星球对象及其所关联的可渲染子对象列表并返回.该类的类图如下所示. 该类所包含的主要的方法基本都是静态的,功能说明如下: public static double ParseDouble(string s)将字符串s解析为Double型数字 private static bool ParseBool(string booleanString) 将字符串s解析为bool型 publ

STL源码剖析:空间配置器

看完自己重写了一下,不知道的又看了一遍,里面没有考虑多线程的问题,主要是想实现以下内存池. Mempool.h #ifndef MEMPOOL_H_ #define MEMPOOL_H_ #include <cstddef> #include <new> #include <cstdlib> namespace flysnow { enum {STEP_ = 8}; enum {MAX_BYTES_ = 128}; enum {FREELIST_NUM_ = MAX_B

菜鸟nginx源码剖析 配置与部署篇(一) 手把手实现nginx &quot;I love you&quot;

菜鸟nginx源码剖析 配置与部署篇(一) 手把手配置nginx "I love you" Author:Echo Chen(陈斌) Email:[email protected] Blog:Blog.csdn.net/chen19870707 Date:Nov 7th, 2014 还记得在前几年的CSDN泄漏账号事件中,统计发现程序员的账号中含有love的最多,这里我也俗套下,在这篇文章中将讲解如何 一步一步实用Nginx在一台机器上搭建一个最简单的显示"I love yo

STL源码剖析 — 空间配置器(allocator)

前言 以STL的实现角度而言,第一个需要介绍的就是空间配置器,因为整个STL的操作对象都存放在容器之中. 你完全可以实现一个直接向硬件存取空间的allocator. 下面介绍的是SGI STL提供的配置器,配置的对象,是内存.(以下内容来自<STL源码剖析>) 空间配置器的标准接口 根据STL的规范,allocator的必要接口 各种typedef 1 allocator::value_type 2 allocator::pointer 3 allocator::const_pointer 4

STL源码剖析 --- 空间配置器 std::alloc

STL是建立在泛化之上的.数组泛化为容器,参数化了所包含的对象的类型.函数泛化为算法,参数化了所用的迭代器的类型.指针泛化为迭代器,参数化了所指向的对象的类型.STL中的六大组件:容器.算法.迭代器.配置器.适配器.仿函数. 这六大组件中在容器中分为序列式容器和关联容器两类,正好作为STL源码剖析这本书的内容.迭代器是容器和算法之间的胶合剂,从实现的角度来看,迭代器是一种将operator*.operator->.operator++.operator-等指针相关操作予以重载的class tem