다음을 통해 공유


Using XML Serialization with WCF

How do I switch the serializer being used to process messages to the old XmlSerializer?

WCF supports both the newer data contract style of message description as well as the older XmlSerializer style of message description used by ASP.NET. The recommended serializer for most uses and all new web service development is the data contract serializer.

However, there are some good compatibility reasons why you may need to use the older XmlSerializer instead. Whenever you need to reuse existing types from an older ASMX application, from an existing XSD description, or other similar sources, the data contract serializer probably doesn't give you enough control to exactly match the existing type on the wire. The XmlSerializer on the other hand, gives you precise control over placement of the XML elements and attributes to craft the right message format.

To find the serializer, the engine first examines the operation contract to see if any serializer has been specified. If not, the engine next examines the service contract to see if a serializer globally applies to all of the operations. If a serializer specification still hasn't been found, then the default data contract serializer is used. The easiest way to specify that you want to use the XmlSerializer is to annotate the service or operation contract with an attribute. We have the [XmlSerializerFormat] attribute that does exactly this for both operation and service contracts.

Note that switching to the XmlSerializer is going to completely change how data contract types are interpreted. Data contracts only serialize type members that have been explicitly attributed as being members of the data contract. The XmlSerializer defaults to serializing every public member of the type. Therefore, you need to be careful when applying the XmlSerializerFormat attribute to make sure that the operations have been designed with that serialization method in mind.

Next time: Queue Scalability

Comments

  • Anonymous
    January 29, 2007
    I'm using the WSHttp binding for message security with a non-WCF system. The other system doesn't have

  • Anonymous
    January 30, 2007
    I have already said my piece on the Vista launch but also Office 2007 launches today which really rocks.

  • Anonymous
    February 19, 2007
    I haven't forgotten about the goal to put together a table of contents for all of these articles. The