0

Let me describe my problem a bit first.

I got to work on an Xamarin app that had to utilize our API. The API is exposed via WCF service on two endpoints and it is either tcp or basic http. Because of the limitations of Xamarin platform I ended up using HttpWebClient for which I had to turn my WCF service restful. This is where problems start to emerge.

I ahve defined another endpoint that I named "rest" and configured it to return JSON through property named DefaultOutgoingResponseFormat. This property takes no effect because I will always get response in form of a XML.

If I remove the XmlSerializerFormatAttribute my service will return me JSON but now I dont have XML. This is easily fixable by making another endpoint whose default return value will be XML but the problems now get even more interesting since some of my classes will not get serialized properly and I will explain this now.

There is an attribute called XmlIgnore that is placed on a property. If you have a propert called Name and another property called NameSpecified and if you place this attribute on NameSpecified than property Name will not be serialized in case property NameSpecified holds value of false. This is extremely useful because the XML that is sent is not crowded with useless information. We want to keep this which effectively means we want to keep XmlSerializerFormatAttribute but also be able to return JSON.

Is this possible?

Robert
  • 2,407
  • 1
  • 24
  • 35

1 Answers1

0

Once you use XmlSerializerFormat you are telling WCF to use the XmlSerializer class over the DataContractSerializer. There is no mechanism built into WCF that can make it now return JSON, because you've explicitly told it to use an XmlSerializer.

If you want to return XML or JSON content, you can take full charge of your message formatting by creating your own IDispatchMessageFormatter implementation. It will then be entirely up to you to format your messages with whatever rules you wish.

Paul Turner
  • 38,949
  • 15
  • 102
  • 166
  • I took a look at the IDispatchMessageFormatter and it is not something trivial to implement. The examples are rare and they all use Json tfor showcase. Damn WCF! – Robert Jun 19 '17 at 12:25