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/jsontext/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 HttpConfiguration

//设置web api configuration
public static void Register(HttpConfiguration config)
{
   config.Services.Replace(typeof(ITraceWriter), new NLogger());
}

//设置asp.net 应用程序 全局configuration
GlobalConfiguration.Configuration.Services.Replace(typeof(ITraceWriter), new NLogger());

//获取 全局configuration
var writer = GlobalConfiguration.Configuration.Services.GetTraceWriter();

//获取 actionContext 当前configuration
ITraceWriter writer = actionContext.ControllerContext.Configuration.Services.GetTraceWriter();

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值