By default WCF use DataContractSerialization so if w开发者_StackOverflow中文版e can change it then my question is how to change it and when should we need which serialization on wcf ?
You can use the XmlSerializerFormatAttribute
attribute on your service contract to force WCF to use the XmlSerializer
.
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.
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.
精彩评论