0

I am trying to customise the behaviour of the standard ODataMediaTypeFormatters and have done so by wrapping them in another type which extends MediaTypeFormatter. e.g.

var formatters = ODataMediaTypeFormatters
                   .Create(serialiserProvider, deserialiserProvider)
                   .Select(formatter => new Wrapper(formatter));

config.Formatters.InsertRange(0, formatters);

After the WebAPI configuration method has executed config.Formatters contains 12 items (7 of which are the wrapped formatters).

However, when a response is being serialised config.Formatters contains 17 items as the standard OData formatters have been re-added at some point. The relevant standard formatter is then used in preference to the wrapped version.

Does anyone know when this re-addition happens and if/how it can be prevented?

Mark Watts
  • 724
  • 7
  • 15

1 Answers1

0

I figured this out when I realised that the formatters are not re-added to GlobalConfiguration.Configuration.Formatters, but only ControllerContext.Configuration.Formatters.

The ODataController is annotated with the ODataFormattingAttribute. This checks to see if the controller's configuration contains any ODataMediaTypeFormatters and, if not, re-adds them.

When it performs this check it also looks for wrapped formatters using Decorator.GetInner, so having the wrappers implement IDecorator solved the problem.

Mark Watts
  • 724
  • 7
  • 15