Jeevan Bhatt
Jeevan Bhatt

Reputation: 6101

How to change Wcf to use a different serializer?

By default WCF use DataContractSerialization so if we can change it then my question is how to change it and when should we need which serialization on wcf ?

Upvotes: 9

Views: 13062

Answers (3)

Oleg
Oleg

Reputation: 222007

WCF has a nice feature that a method can return Message or a Stream (see Returning raw json (string) in wcf and How to set Json.Net as the default serializer for WCF REST service as examples). The corresponding code which you need to write can be more easy as if you will use more advance techniques Extending Encoders and Serializers. So it is very easy to implement Streaming Message Transfer for example or just returning JPG or Excel file as a result of some WCF method.

Upvotes: 4

softveda
softveda

Reputation: 11074

The default choice of DataContractSerializer is good for most purpose. You can also use the DataContractJsonSerializer specially for REST type services and if the client expects Json content type. The other option is XmlSerializer for interoperability purpose if you need more control over the generated XML. DataContractSerializer is more efficient than XmlSerializer.

In 3rd party options you can use protobuf-net from Google which is more efficient than DataContract Serializer.

Upvotes: 1

Graham Clark
Graham Clark

Reputation: 12966

You can use the XmlSerializerFormatAttribute attribute on your service contract to force WCF to use the XmlSerializer.

Upvotes: 7

Related Questions