views:

329

answers:

2

Hi,

I have a question. Is it possible to replace dataContractSerializer in Windows Communication Foundation with my own serializer. If it is possible, how can I achieve this?

+1  A: 

Normally you place the attribute [DatacontractSerializer] or [XmlSerializer] above the Service Contract, im pretty sure you could create your own hence why you apply them as attributes, now if only reflector would start so that i could inspect the XmlSerializer and find out what makes it tick.

msdn insist that classes that want to be serialized via the XmlSerializer, must be decorated with [XmlAttribute] or [XmlElement] attributes (since it would give more shape to the xml file understandingly), but it does work if your classes are decorated with the [DataMember], which is compatible with DataContractSerializer, hence why you should be able to create your own serializer that will serialize anything with a [DataMember] tag, just like the XmlSerializer

Neil
+2  A: 

Yes, you can provide your own serializer implementation. By default WCF will use the DataContractSerializer. To provide your own serializer you must write your own IOperationBehavior which seeks out and removes the currently applied DataContractSerializerOperationBehavior from the OperationDescription::Behaviors collection and then applies a custom instance of an DataContractSerializerOperationBehavior. The DataContractSerializerOperationBehavior is then responsible for constructing an XmlObjectSerializer implementation in it's CreateSerializer factory methods. For some code samples for how to do this, check out this article by Dan Rigsby.

From there, it's all about implementing your own custom XmlObjectSerializer which will allow you to serialize the XML infoset to any representation you want.

Drew Marsh
Hi Drew,But is my serializer have to be derived from a base class or implement some interfaces. If the answer is yes, what are these? Is the serializer have to be a kind of xmlserializer? I wrote a serializer that serializes types as bytes.
mkus
You must inherit from XmlObjectSerializer and provide an implementation of your own for the various methods.
Drew Marsh