Use XmlSerializer on request and DataContractSerializer when responding?

Is it possible to get a request with attributes and use the XmlSerializer to deserialize and send a response using only elements using a DataContractSerializer?

Also, if you get a request with attributes, should you use the XmlSerializer to deserialize the content?

+4
source share
1 answer

For the second question: if you have attributes, you need to use XmlSerializer - DataContractSerializer does not support them.

For the first question: yes, it is possible. No, this is not easy. The choice of serializer is done at the operation formatter level. WCF allows you to change the serializer to an operation using [XmlSerializerFormat] or [DataContractFormat] (by default), but this will associate the serializer with both requests and responses.

If you really want to do this, you can create your own formatter (replace it with some operation behavior), and then in this formatting you can choose how you serialize / deserialize the inputs / outputs (you can even use different serializers for each parameter ), but you need to create one formatter, which is not too easy to do.

I wrote a few posts about message formatters and replaced serializers in Silverlight , which can give you a hint on how to get started. But if you can only live with the XmlSerializer, it will be much easier.

+4
source

All Articles