public void Negotiate_CallGetPerRequestFormatterInstanceFirst()
        {
            HttpRequestMessage request = new HttpRequestMessage();
            MediaTypeFormatter perRequestFormatter = new ODataMediaTypeFormatter(Enumerable.Empty<ODataPayloadKind>()) { Request = request };
            Mock<MediaTypeFormatter> formatter = new Mock<MediaTypeFormatter>();
            formatter
                .Setup(f => f.GetPerRequestFormatterInstance(typeof(void), request, It.IsAny<MediaTypeHeaderValue>()))
                .Returns(perRequestFormatter);
            Mock<IContentNegotiator> innerContentNegotiator = new Mock<IContentNegotiator>();
            innerContentNegotiator
                .Setup(n => n.Negotiate(typeof(void), request, It.Is<IEnumerable<MediaTypeFormatter>>(f => f.First() == perRequestFormatter)))
                .Returns(new ContentNegotiationResult(perRequestFormatter, MediaTypeHeaderValue.Parse("application/json")));

            IContentNegotiator contentNegotiator = new PerRequestContentNegotiator(innerContentNegotiator.Object);
            var negotiationResult = contentNegotiator.Negotiate(typeof(void), request, new MediaTypeFormatter[] { formatter.Object });

            Assert.Same(perRequestFormatter, negotiationResult.Formatter);
        }
        /// <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);
        }
        public void Negotiate_CallGetPerRequestFormatterInstanceFirst()
        {
            HttpRequestMessage        request             = new HttpRequestMessage();
            MediaTypeFormatter        perRequestFormatter = new ODataMediaTypeFormatter(Enumerable.Empty <ODataPayloadKind>(), request);
            Mock <MediaTypeFormatter> formatter           = new Mock <MediaTypeFormatter>();

            formatter
            .Setup(f => f.GetPerRequestFormatterInstance(typeof(void), request, It.IsAny <MediaTypeHeaderValue>()))
            .Returns(perRequestFormatter);
            Mock <IContentNegotiator> innerContentNegotiator = new Mock <IContentNegotiator>();

            innerContentNegotiator
            .Setup(n => n.Negotiate(typeof(void), request, It.Is <IEnumerable <MediaTypeFormatter> >(f => f.First() == perRequestFormatter)))
            .Returns(new ContentNegotiationResult(perRequestFormatter, MediaTypeHeaderValue.Parse("application/json")));

            IContentNegotiator contentNegotiator = new PerRequestContentNegotiator(innerContentNegotiator.Object);
            var negotiationResult = contentNegotiator.Negotiate(typeof(void), request, new MediaTypeFormatter[] { formatter.Object });

            Assert.Same(perRequestFormatter, negotiationResult.Formatter);
        }
        /// <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);
        }