Soap Headers in WCF, where have they gone.
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.
Paul Kinlan said,
March 7, 2007 @ 3:55 pm
“I personally prefer the Code first approach and find it much easier to think of messages in terms of objects”
Messages are not objects, and they should not be thought of as objects. Messages are messages that contain data.
The good thing about contract first is that you define up-front a schema (noramlly wsdl/xsd) that everyone can conform towards.
I belive the Webservice Software Factory has some guidance and help around headers. It is on Codeplex under patterns & practices.
Paul Kinlan
Paul Pierce said,
March 7, 2007 @ 5:17 pm
Alright Paul,
Thanks for the comment, makes a change to get one thats not spam.
I can see the benefit of the contract first approach, in that the wsdl’s produced are more human readable and give you a more control over validation etc. The code first approach isn’t the right choice for all applications and I agree that an initial investment of time in writing the wsdl could save time in the long run.
But i disagree with “Messages are not objects, and they should not be thought of as objects”. Surely your wsdl will ultimately be used to generate a Data Transfer Object of some kind, so why not think in terms of the DTO to start with?
I will have a look at the webservice software factory.
ps. Enjoyed your posts on Devweek
http://www.kinlan.co.uk/2007/02/day-1-of-devweek-conference.html
http://www.kinlan.co.uk/2007/03/devweek-day-2.html
http://www.kinlan.co.uk/2007/03/devweek-day-3-review.html
Sounded like it was really interesting, Maybe next year
.