<?xml version="1.0" encoding="utf-8"?><?xml-stylesheet type='text/xsl' href='http://wkfry.spaces.live.com/mmm2008-07-24_12.50/rsspretty.aspx?rssquery=en-US;http%3a%2f%2fwkfry.spaces.live.com%2fcategory%2fWCF%2ffeed.rss' version='1.0'?><rss version="2.0" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:msn="http://schemas.microsoft.com/msn/spaces/2005/rss" xmlns:live="http://schemas.microsoft.com/live/spaces/2006/rss" xmlns:dcterms="http://purl.org/dc/terms/" xmlns:cf="http://www.microsoft.com/schemas/rss/core/2005" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Bill's Space: WCF</title><description /><link>http://wkfry.spaces.live.com/?_c11_BlogPart_BlogPart=blogview&amp;_c=BlogPart&amp;partqs=catWCF</link><language>en-US</language><pubDate>Tue, 20 May 2008 16:21:24 GMT</pubDate><lastBuildDate>Tue, 20 May 2008 16:21:24 GMT</lastBuildDate><generator>Microsoft Spaces v1.1</generator><docs>http://www.rssboard.org/rss-specification</docs><ttl>60</ttl><cf:parentRSS>http://wkfry.spaces.live.com/blog/feed.rss</cf:parentRSS><live:type>blogcategory</live:type><live:identity><live:id>-4671077372406318224</live:id><live:alias>wkfry</live:alias></live:identity><cf:listinfo><cf:group ns="http://schemas.microsoft.com/live/spaces/2006/rss" element="typelabel" label="Type" /><cf:group ns="http://schemas.microsoft.com/live/spaces/2006/rss" element="tag" label="Tag" /><cf:group element="category" label="Category" /><cf:sort element="pubDate" label="Date" data-type="date" default="true" /><cf:sort element="title" label="Title" data-type="string" /><cf:sort ns="http://purl.org/rss/1.0/modules/slash/" element="comments" label="Comments" data-type="number" /></cf:listinfo><item><title>WCF: BodyWriter and Raw XML Problems</title><link>http://wkfry.spaces.live.com/Blog/cns!BF2CFFE0D35B3B70!235.entry</link><description>&lt;p&gt;I've been recently writing code with Windows Communication Foundation (WCF) using raw Message contracts. The purpose of raw Message contracts is to allow the developer to be in complete control of the format of the message that is received and sent by a service. This is a good choice for the project that I'm working on because I am dealing with legacy code that is not strongly typed and I need the flexibility to process messages with structures that may vary between calls. (This is of course less than ideal, but it allows legacy code to function in WCF while we refactor the code.) &lt;p&gt;WCF provides several options for writing XML into your message. The &lt;b&gt;Message.CreateMessage()&lt;/b&gt; method accepts &lt;b&gt;Object&lt;/b&gt; (which will automatically be serialized using the Xml Serializer), &lt;b&gt;XmlReader&lt;/b&gt;, &lt;b&gt;XmlDictionaryReader&lt;/b&gt; or &lt;b&gt;BodyWriter&lt;/b&gt;. The &lt;b&gt;BodyWriter&lt;/b&gt; object provides an event that you can override to implement your own serialization. &lt;p&gt;In the project that I was creating, the data for my Message was already in XML, so I thought my most efficient solution would be to implement a &lt;b&gt;BodyWriter&lt;/b&gt; that would simply write the raw XML into the stream. So I created the following class:&lt;pre&gt;&lt;code&gt;    public class SimpleMessageBody : BodyWriter
    {
        string xmlContent;

        public SimpleMessageBody(string content)
            : base(true)
        {
            this.xmlContent = content;
        }

        protected override void OnWriteBodyContents(XmlDictionaryWriter writer)
        {
            writer.WriteRaw(xmlContent);
        }
    }&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;I then used this class to be the input to the &lt;b&gt;CreateMessage&lt;/b&gt; method as follows:&lt;pre&gt;&lt;code&gt;    message = Message.CreateMessage(MessageVersion.Default, 
                &amp;quot;http://tempuri.org/SomeMethod&amp;quot;, new SimpleMessageBody(xml));&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Much to my surprise, my XML content was automatically encoded in my method! However, if I opened an XmlReader on my XML content and passed the reader into the method, everything worked fine.
&lt;p&gt;Eventually, I used the &lt;b&gt;.NET Reflector&lt;/b&gt; utility to look deep inside the &lt;b&gt;BodyWriter&lt;/b&gt; class. It turns out that the &lt;b&gt;BodyWriter&lt;/b&gt; uses it's own special implementation of XmlWriter and when you look at this implementation, whenever you call the &lt;b&gt;.WriteRaw()&lt;/b&gt; method, that method actually calls the &lt;b&gt;.WriteText()&lt;/b&gt; method under the hood which ultimately encodes your raw XML!
&lt;p&gt;It's possible that Microsoft wanted to prevent developers from writing raw XML into the message in order to prevent corruption, namespace conflicts or potential security violations. Unfortunately, this behavior does not appear to be clearly documented.
&lt;p&gt;The only option appears to be to open an &lt;b&gt;XmlReader&lt;/b&gt; and use the &lt;b&gt;.WriteNode()&lt;/b&gt; method.&lt;pre&gt;&lt;code&gt;    protected override void OnWriteBodyContents(XmlDictionaryWriter writer)
    {
        using (StringReader stringReader = new StringReader(xmlContent))
        {
            using (XmlReader xmlReader = XmlTextReader.Create(stringReader))
            {
                writer.WriteNode(xmlReader, true);
            }
        }
    }&lt;/code&gt;&lt;/pre&gt;&lt;img src="http://c.services.spaces.live.com/CollectionWebService/c.gif?cid=-4671077372406318224&amp;page=RSS%3a+WCF%3a+BodyWriter+and+Raw+XML+Problems&amp;referrer=" width="1px" height="1px" border="0" alt=""&gt;&lt;img style="position:absolute" alt="" width="0px" height="0px" src="http://c.live.com/c.gif?NC=31263&amp;amp;NA=1149&amp;amp;PI=73329&amp;amp;RF=&amp;amp;DI=3919&amp;amp;PS=85545&amp;amp;TP=wkfry.spaces.live.com&amp;amp;GT1=wkfry"&gt;</description><comments>http://wkfry.spaces.live.com/Blog/cns!BF2CFFE0D35B3B70!235.entry#comment</comments><guid isPermaLink="true">http://wkfry.spaces.live.com/Blog/cns!BF2CFFE0D35B3B70!235.entry</guid><pubDate>Tue, 15 May 2007 20:08:38 GMT</pubDate><slash:comments>0</slash:comments><msn:type>blogentry</msn:type><live:type>blogentry</live:type><live:typelabel>Blog entry</live:typelabel><wfw:commentRss>http://wkfry.spaces.live.com/blog/cns!BF2CFFE0D35B3B70!235/comments/feed.rss</wfw:commentRss><wfw:comment>http://wkfry.spaces.live.com/Blog/cns!BF2CFFE0D35B3B70!235.entry#comment</wfw:comment><dcterms:modified>2007-07-29T20:27:01Z</dcterms:modified></item></channel></rss>