1

In WCF, if I have code like this and I'm returning it in XML format, the XML is auto generated. Is it possible for me to expicitly specify which XML will be used for the serialization of the CustomerEntity?

[OperationContract]    
[WebInvoke(Method = "GET"]
CustomerEntity GetCustomer(int customerPk);

The alternative that I'm currently using is to return an XElement, but the problem with this is that I can't also support JSon that way.

Update: My types are immutable and use raedonly properties, so IXmlSerializable won't work for me.

2 Answers2

2

Implement IXmlSerializable on CustomerEntity.

Darrel Miller
  • 139,164
  • 32
  • 194
  • 243
  • For a little more guidance on implementing IXmlSerializable, see http://stackoverflow.com/questions/279534/proper-way-to-implement-ixmlserializable – E.Z. Hart Mar 06 '11 at 01:07
  • It turns out this won't work because my type is immutable so I can't implement the from xml :( – wcf guru needed Mar 06 '11 at 01:51
  • @wcf guru It's going to make deserializing the type a bit tricky if it is immutable! – Darrel Miller Mar 06 '11 at 02:21
  • 1
    @wcf guru Can you return a CustomerEntityWrapper class that implements an IXmlSerializable interface and create the real CustomerEntity as a property of the wrapper? – Darrel Miller Mar 06 '11 at 02:22
  • The wrapper idea migiht work, if I don't get a better suggestion I'll re-accept this. I am wondering if I should just make 2 different functions with slightly different calling templates both calling a common function for the common functionality. – wcf guru needed Mar 06 '11 at 02:35
1

You can try using Raw messages i.e. using Contract types deriving from Message class and writing the messages the way you want by overriding OnWriteBodyContents and other overridable Message class members. Please refer "Inheriting from the Message Class" section gof following MSDN article to know more about overridable members of Message class, http://msdn.microsoft.com/en-us/library/ms734675.aspx

HTH, Amit

amit
  • 2,093
  • 16
  • 10