Skip to content

Instantly share code, notes, and snippets.

@tugberkugurlu
Created July 4, 2012 12:36
Show Gist options
  • Save tugberkugurlu/3047119 to your computer and use it in GitHub Desktop.
Save tugberkugurlu/3047119 to your computer and use it in GitHub Desktop.
DefaultServices class in ASP.NET Web API project
[SuppressMessage("Microsoft.Maintainability", "CA1506:AvoidExcessiveClassCoupling", Justification = "Class needs references to large number of types.")]
[SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope", Justification = "We're registering the ValidationCache to be disposed by the HttpConfiguration.")]
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(),
new InvalidModelValidatorProvider());
// 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()));
configuration.RegisterForDispose(validatorCache);
SetSingle<IModelValidatorCache>(validatorCache);
_serviceTypesSingle = new HashSet<Type>(_defaultServicesSingle.Keys);
_serviceTypesMulti = new HashSet<Type>(_defaultServicesMulti.Keys);
// Reset the caches and the known dependency scope
ResetCache();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment