private void EnsureResolved() { if (_resolvedDependencies == null) { HttpConfiguration configuration = _controller.Configuration; if (configuration == null) { throw new InvalidOperationException( SRResources.HttpControllerContext_ConfigurationMustNotBeNull); } ServicesContainer services = configuration.Services; Contract.Assert(services != null); IContentNegotiator contentNegotiator = services.GetContentNegotiator(); if (contentNegotiator == null) { throw new InvalidOperationException(Error.Format( SRResources.HttpRequestMessageExtensions_NoContentNegotiator, typeof(IContentNegotiator))); } HttpRequestMessage request = _controller.Request; if (request == null) { throw new InvalidOperationException(SRResources.ApiController_RequestMustNotBeNull); } IEnumerable <MediaTypeFormatter> formatters = configuration.Formatters; Contract.Assert(formatters != null); _resolvedDependencies = new DirectDependencyProvider(contentNegotiator, request, formatters); } }
private static IHttpActionResult CreateDefaultLastChanceResult(ExceptionContext context) { Contract.Assert(context != null); Exception exception = context.Exception; if (exception == null) { return(null); } HttpRequestMessage request = context.Request; if (request == null) { return(null); } HttpRequestContext requestContext = context.RequestContext; if (requestContext == null) { return(null); } HttpConfiguration configuration = requestContext.Configuration; if (configuration == null) { return(null); } ServicesContainer services = configuration.Services; Contract.Assert(services != null); IContentNegotiator contentNegotiator = services.GetContentNegotiator(); if (contentNegotiator == null) { return(null); } IEnumerable <MediaTypeFormatter> formatters = configuration.Formatters; Contract.Assert(formatters != null); return(new ExceptionResult( exception, requestContext.IncludeErrorDetail, contentNegotiator, request, formatters )); }
/// <summary> /// Callback invoked to set per-controller overrides for this controllerDescriptor. /// </summary> /// <param name="controllerSettings">The controller settings to initialize.</param> /// <param name="controllerDescriptor">The controller descriptor. Note that the <see /// cref="T:System.Web.Http.Controllers.HttpControllerDescriptor" /> can be associated with the derived /// controller type given that <see cref="T:System.Web.Http.Controllers.IControllerConfiguration" /> is /// inherited.</param> public void Initialize(HttpControllerSettings controllerSettings, HttpControllerDescriptor controllerDescriptor) { if (controllerSettings == null) { throw Error.ArgumentNull("controllerSettings"); } if (controllerDescriptor == null) { throw Error.ArgumentNull("controllerDescriptor"); } // If any OData formatters are registered globally, do nothing and use those instead MediaTypeFormatterCollection controllerFormatters = controllerSettings.Formatters; if (!controllerFormatters.Where(f => f != null && Decorator.GetInner(f) is ODataMediaTypeFormatter).Any()) { // Remove Xml and Json formatters to avoid media type conflicts. RemoveFormatters(controllerFormatters, controllerFormatters.Where(f => f is XmlMediaTypeFormatter || f is JsonMediaTypeFormatter)); // Then add our formatters. controllerFormatters.InsertRange(0, CreateODataFormatters()); } ServicesContainer services = controllerSettings.Services; Contract.Assert(services != null); // Replace the action value binder with one that attaches the request to the formatter. IActionValueBinder originalActionValueBinder = services.GetActionValueBinder(); IActionValueBinder actionValueBinder = new PerRequestActionValueBinder(originalActionValueBinder); controllerSettings.Services.Replace(typeof(IActionValueBinder), actionValueBinder); // Replace the content negotiator with one that uses the per-request formatters. // This negotiator is required to allow CanReadType to have access to the model. IContentNegotiator originalContentNegotiator = services.GetContentNegotiator(); IContentNegotiator contentNegotiator = new PerRequestContentNegotiator(originalContentNegotiator); controllerSettings.Services.Replace(typeof(IContentNegotiator), contentNegotiator); }
private void EnsureResolved() { if (_resolvedDependencies == null) { HttpRequestContext requestContext = _controller.RequestContext; Contract.Assert(requestContext != null); bool includeErrorDetail = requestContext.IncludeErrorDetail; HttpConfiguration configuration = _controller.Configuration; if (configuration == null) { throw new InvalidOperationException("HttpControllerContext.Configuration must not be null." /*SRResources.HttpControllerContext_ConfigurationMustNotBeNull*/); } ServicesContainer services = configuration.Services; Contract.Assert(services != null); IContentNegotiator contentNegotiator = services.GetContentNegotiator(); if (contentNegotiator == null) { throw new InvalidOperationException(Error.Format("The provided configuration does not have an instance of the '{0}' service registered." /*SRResources.HttpRequestMessageExtensions_NoContentNegotiator*/, typeof(IContentNegotiator))); } HttpRequestMessage request = _controller.Request; if (request == null) { throw new InvalidOperationException("ApiController.Request must not be null." /*SRResources.ApiController_RequestMustNotBeNull*/); } IEnumerable <MediaTypeFormatter> formatters = configuration.Formatters; Contract.Assert(formatters != null); _resolvedDependencies = new DirectDependencyProvider(includeErrorDetail, contentNegotiator, request, formatters); } }
private void EnsureResolved() { if (_resolvedDependencies == null) { HttpRequestContext requestContext = _controller.RequestContext; Contract.Assert(requestContext != null); bool includeErrorDetail = requestContext.IncludeErrorDetail; HttpConfiguration configuration = _controller.Configuration; if (configuration == null) { throw new InvalidOperationException("Configuration must not be null."); } ServicesContainer services = configuration.Services; Contract.Assert(services != null); IContentNegotiator contentNegotiator = services.GetContentNegotiator(); if (contentNegotiator == null) { throw new InvalidOperationException("ContentNegotiator must not be null."); } HttpRequestMessage request = _controller.Request; if (request == null) { throw new InvalidOperationException("Request must not be null."); } IEnumerable <MediaTypeFormatter> formatters = configuration.Formatters; Contract.Assert(formatters != null); _resolvedDependencies = new DirectDependencyProvider(includeErrorDetail, contentNegotiator, request, formatters); } }