3

There are a lof of different ways to replace the DataContractResolver if you are using WCF, I want to do the same thing with the Web Api. The only extension point I've found is this:

GlobalConfiguration.Configuration.Formatters.XmlFormatter.SetSerializer<Person>(new DataContractSerializer(typeof(Person), null, Int32.MaxValue, false, false, null, new TypeNameVersioning()));

I am looking for something in the line of (pseudo code):

GlobalConfiguration.Configuration.Formatters.XmlFormatter.SetDataContractResolver = new TypeNameVersioning();
Armel Larcier
  • 15,747
  • 7
  • 68
  • 89
Marius
  • 9,208
  • 8
  • 50
  • 73
  • DataContractResolver can only set in DataContractSerializer's constructor so you really need to create a new DataContractSerializer – LostInComputer Jan 15 '14 at 13:25
  • Not quite, you can also specify it in the Read/Write methods on the DataContractSerializer. But that is beside the point, WCF provides extension points I am surprised that the web api does not seem to provide something similar – Marius Jan 15 '14 at 13:30

1 Answers1

0

If you want to use the XmlSerializer then

GlobalConfiguration.Configuration.Formatters.XmlFormatter.UseXmlSerializer = true

If that doesn't meet your needs then do

GlobalConfinguration.Configuration.Formatters.Clear();
GlobalConfinguration.Configuration.Formatters.Add(new YourOwnFormatterCanGoHere());

Just one other point. I use Web API all the time and have for several years now and I rarely use formatters at all. I return HttpResponseMessage with derived versions of HttpContent. Not saying you should do the same, just saying there are many ways of doing things in Web API. Formatters are not required.

Darrel Miller
  • 139,164
  • 32
  • 194
  • 243
  • I'm sorry but I don't follow you. Formatters handle serialization based on content type right? If you return HttpResponseMessage someone still has to serialize the content onto the response stream? – Marius Jan 15 '14 at 14:24
  • @Marius No, classes that derive from HttpContent do the serialization. When you return a CLR type from a web api it gets put in an `ObjectContent` instance which then uses formatters to help it serialize. If you don't need to serialize arbitrary CLR types you can just return a derived HttpContent. – Darrel Miller Jan 15 '14 at 14:51
  • @Marius If you look at this project https://github.com/darrelmiller/ndc/tree/master/ConferenceWebApi/Controllers you can see I return `Json-home` content, `collection+json` content and `hal` content. – Darrel Miller Jan 15 '14 at 14:53
  • @Marius Web API has many of it's own content classes, but the only one that uses formatters is ObjectContent. – Darrel Miller Jan 15 '14 at 14:54
  • Thanks for the explanation. Just for curiosities sake, whats the benefit of abandoning content-negotation (the example code you sent)? – Marius Jan 16 '14 at 06:55
  • @Marius. I do content negotiation in controllers where I perceive it to be valuable. Where there is a real need to expose the same resource in different media types. I don't think conneg was intended to be a way for a client to express their preference between curly braces and angle brackets. – Darrel Miller Jan 16 '14 at 12:36