WCF offers several different approaches for writing Web Services; Contract first, Code first, with slight variations on either approach.
I personally prefer the Code first approach and find it much easier to think of messages in terms of objects. With the Code First approach in WCF, you define a contract which will later be used to generate your WSDL. WCF provides Data Contracts as the preferred approach to abstracting out your message.
To use Data Contracts you mark your class with the DataContract attribute and the properties you wish to expose for serialisation with the DataMember attribute, the object would then serialised using the DataContractSerializer; a limitation with this approach is that you don’t have full control over serialisation, so it would be difficult to work with classes that may have been generated using xsd.exe for example.
WCF offers another Code First solution that gives you full control over serialisation; you can mark your classes with the Serializable attribute and the Service Contract Interface with XmlSerializerFormat attribute to leverage the XmlSerializer.
The XmlSerializerFormat approach seemed like an ideal solution to a recent project, giving me full control over the serialization of the soap body. I next needed to do a similar thing to the soap header. WCF uses Message Contracts to give you control over the soap message, you basically decorate the class that represents your message with the MessageContract attribute then properties in that class with MessageHeader and MessageBody attributes, represent the SOAP header and SOAP body respectively.
The abstraction the SOAP envelope using the Message Contract is not compatible with the XmlSerializerFormat approach, you can only use it with DataContract approach and there doesn’t appear to be a similar abstraction to use with the XmlSerializerFormat approach. This has left me a bit puzzled as to the best way to harness the XmlSerializer for SOAP headers. I’m sure the answer must be to dip into the WCF pipeline at another point to process the header, but nowhere feel’s natural to do so.
Any thoughts or ideas on this would be welcome.