Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom WCF DataContractSerializer

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

like image 262
mkus Avatar asked Dec 28 '09 21:12

mkus


People also ask

How do you serialize an object in WCF?

This code constructs an instance of the DataContractSerializer that can be used only to serialize or deserialize instances of the Person class. DataContractSerializer dcs = new DataContractSerializer(typeof(Person)); // This can now be used to serialize/deserialize Person but not PurchaseOrder.

What is by serialization with respect to WCF?

The process forms a sequence of bytes into a logical object; this is called an encoding process. At runtime when WCF receives the logical message, it transforms them back into corresponding . Net objects. This process is called serialization.

Which namespace is used for serialization in WCF?

By default WCF uses the DataContractSerializer class to serialize data types.

Which class is required to configure to use the Datacontractjsonserializer?

JavaScriptSerializer Class (System. NET Framework 4.7.


3 Answers

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.

like image 106
Drew Marsh Avatar answered Nov 16 '22 07:11

Drew Marsh


The WCF team did this when they introduced DataContractJsonSerializer.

There are far more touchpoints than the ones mentioned here in the other two answers -- for example you may have to introduce and change the operation behaviors and operation formatters being used, add a new encoder, and add entirely new service and client behaviors as well. It depends on what the new serialization format is and what you're trying to do.

However, if your goal is to only add a new serializer for stand-alone serialization, it's simpler.

What I would recommend is -- look at all the different usage of DataContractJsonSerializer (via Reflector) in System.Runtime.Serialization.dll, System.ServiceModel.Web.dll, and System.Runtime.ServiceModel.dll.

If you're interested in only the standalone serialization aspect, just look at the DataContractJsonSerializer and the associated classes in the Serialization namespace. It should be fairly intuitive for you to grasp the various subtleties of custom serialization implementation. Ask away if you have specific questions, but I truly believe this is the quickest and most elegant way to learn this particular solution...

like image 34
krisragh MSFT Avatar answered Nov 16 '22 07:11

krisragh MSFT


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

like image 2
Neil Avatar answered Nov 16 '22 06:11

Neil