WCF and XmlSerialization and XmlWriterSettings
I have a REStful WCF web service (using a substantially modified WCF Rest Starter Kit) and the data contracts are simple POCOs marked with [Serializable] and [XmlType] (with members tagged with [XmlElement] or [XmlAttribute] where appropriate).
Somewhere inside WCF an instance of XmlSerializer is created which generates the ou开发者_StackOverflow中文版tput with no indentation or spacing between XML nodes, which is fine for automated processes, but makes debugging harder as I have to manually format the XML output myself.
I want to use XmlWriterSettings so it will automatically format the XML before it gets sent down the pipe, but I can't see where I could inject it.
I used Reflector to find where XmlSerializer is instantiated within WCF and it shows up inside a few nested internal classes isnide XmlSerializerOperationBehavior, but beyond that I'm stuck.
Ta!
The XmlWriterSettings
object isn't passed to the constructor to the XmlSerializer
, but to the XmlWriter
which will be then passed to the serializer when it's time to write the object out. The place where you can change that is a custom message encoder (responsible for converting between the XML Infoset in the message and the actual bytes in the wire). One good sample of a custom encoder which creates a XmlWriter
instance is the "Custom Text Encoder".
I think you can control the complete XMLSerializer output that WCF uses to create the message transcription. Hints and examples are given in http://msdn.microsoft.com/en-us/magazine/cc163569.aspx.
精彩评论