<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Infovark Underground &#187; WCF</title>
	<atom:link href="http://underground.infovark.com/category/wcf/feed/" rel="self" type="application/rss+xml" />
	<link>http://underground.infovark.com</link>
	<description>The Infovark technology blog</description>
	<lastBuildDate>Thu, 12 May 2011 16:32:40 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Looking for an IIS Alternative</title>
		<link>http://underground.infovark.com/2009/12/01/looking-for-an-iis-alternative/</link>
		<comments>http://underground.infovark.com/2009/12/01/looking-for-an-iis-alternative/#comments</comments>
		<pubDate>Tue, 01 Dec 2009 17:43:14 +0000</pubDate>
		<dc:creator>Dean</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[WCF]]></category>
		<category><![CDATA[Web Services]]></category>
		<category><![CDATA[REST]]></category>
		<category><![CDATA[RESTful]]></category>
		<category><![CDATA[web servers]]></category>

		<guid isPermaLink="false">http://underground.infovark.com/?p=531</guid>
		<description><![CDATA[One year ago it became clear that Infovark had outgrown the Windows Communication Foundation (WCF). We&#8217;d decided to use WCF because we wanted Infovark to provide web services, and we liked the fact that we could deploy WCF to client machines. Since WCF is built directly on top of HttpListener, a core part of the [...]


Related posts:<ol><li><a href='http://underground.infovark.com/2008/04/22/using-wcf-for-rest-part-1/' rel='bookmark' title='Using WCF for REST, Part 1'>Using WCF for REST, Part 1</a></li>
<li><a href='http://underground.infovark.com/2008/04/10/reviewrestful-webservices/' rel='bookmark' title='Review: Restful Web Services'>Review: Restful Web Services</a></li>
<li><a href='http://underground.infovark.com/2009/02/05/put-the-web-back-in-web-services/' rel='bookmark' title='Put the &#8220;Web&#8221; Back in Web Services'>Put the &#8220;Web&#8221; Back in Web Services</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>One year ago it became clear that Infovark had outgrown the Windows Communication Foundation (WCF). </p>
<p>We&#8217;d decided to use WCF because we wanted Infovark to provide web services, and we liked the fact that we could deploy WCF to client machines. Since WCF is built directly on top of HttpListener, a core part of the Microsoft .NET Framework, we wouldn&#8217;t need to use System.Net or Microsoft IIS.</p>
<p>But we&#8217;ve <a href="http://underground.infovark.com/category/wcf/">struggled with WCF</a> for a variety of reasons. First, we wanted to use a REST model for our web services, and WCF&#8217;s support for REST architectures lags behind its SOAP support.</p>
<p>Second, there&#8217;s no easy way to <a href="http://underground.infovark.com/2009/02/18/using-wcf-to-return-html/">return HTML from WCF</a>. We tried transforming our XML with XSLT and returning the XHTML results as a Stream. This works, but the programming experience is frustrating.</p>
<p>Last, because of the previous two reasons, we were left with a website that was way too rigid and programmer-like. It didn&#8217;t feel like an organic website. The tool we&#8217;d picked was forcing us to compromise on our website design goals. </p>
<p>Infovark&#8217;s primary mission is to help human beings, not other computers. That means that the look and feel of the web interface should be our number one priority. Awesome web services are nice to have, but happy users are more important.</p>
<h4>Web server alternatives</h4>
<p>So for the past few months, we&#8217;ve been hunting for an alternative web server. We can&#8217;t use IIS because its footprint is too heavy. Most IT departments won&#8217;t allow us to install IIS on client machines.</p>
<p>We could use Apache. It has a nice embeddable version, but interacting with it via C# is tricky. We&#8217;d prefer something a little more Microsoft-native.</p>
<p>That basically leaves us with one commercial option and two open source options.</p>
<ol>
<li><a href="http://www.ultidev.com/products/Cassini/">UltiDev Cassini</a> is a commercial product that&#8217;s been around for some time. We&#8217;re sure it could do the job, but the licensing model is cost prohibitive.</li>
<li><a href="http://www.codeplex.com/webserver">C# WebServer</a> is an open source project on CodePlex. It&#8217;s been around for two years, but the pace of development seems slow.</li>
<li><a href="http://trac.caffeine-it.com/openrasta">Open Rasta</a> is the brain child of Sebastien Lambda, an open source framework for the development RESTful development of web sites and services. It&#8217;s been getting a lot of attention recently.</li>
<li><a href="http://code.google.com/p/kayak/">Kayak</a> is a promising open source project, but it hasn&#8217;t reached its first public release yet.</li>
</ol>
<p>(If you know of other web servers worth investigating, please let us know in the comments!)</p>
<h4>Making the switch</h4>
<p>More important than picking an alternative web hosting framework for Infovark is the <em>timing</em> of the switch. We don&#8217;t want to impede future development. </p>
<p>As a stopgap, we might try plugging in the <a href="http://dev.dejardin.org/">Spark View Engine</a> to replace our current XML-XSLT-XHTML rendering path. Who knows? If it improves our web development flow, we might be able to keep our WCF base after all. </p>


<p>Related posts:<ol><li><a href='http://underground.infovark.com/2008/04/22/using-wcf-for-rest-part-1/' rel='bookmark' title='Using WCF for REST, Part 1'>Using WCF for REST, Part 1</a></li>
<li><a href='http://underground.infovark.com/2008/04/10/reviewrestful-webservices/' rel='bookmark' title='Review: Restful Web Services'>Review: Restful Web Services</a></li>
<li><a href='http://underground.infovark.com/2009/02/05/put-the-web-back-in-web-services/' rel='bookmark' title='Put the &#8220;Web&#8221; Back in Web Services'>Put the &#8220;Web&#8221; Back in Web Services</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://underground.infovark.com/2009/12/01/looking-for-an-iis-alternative/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Visual Studio 2008 and its CopyLocal setting</title>
		<link>http://underground.infovark.com/2009/03/31/visual-studio-2008-and-its-copylocal-setting/</link>
		<comments>http://underground.infovark.com/2009/03/31/visual-studio-2008-and-its-copylocal-setting/#comments</comments>
		<pubDate>Tue, 31 Mar 2009 01:08:38 +0000</pubDate>
		<dc:creator>Dean</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[WCF]]></category>
		<category><![CDATA[bugs]]></category>
		<category><![CDATA[Visual Studio]]></category>

		<guid isPermaLink="false">http://underground.infovark.com/?p=384</guid>
		<description><![CDATA[Our build server wasn&#8217;t producing the same results as our local development machines. The same C# code yielded two different sets of code libraries. After much head-banging and hand-wringing, we finally traced the problem to an obscure error in the way Visual Studio 2008 handles .csproj files. Basically, the Visual Studio IDE doesn&#8217;t handle the [...]


Related posts:<ol><li><a href='http://underground.infovark.com/2009/09/09/visual-studio-2008-and-the-copylocal-setting/' rel='bookmark' title='Visual Studio 2008 and the CopyLocal setting'>Visual Studio 2008 and the CopyLocal setting</a></li>
<li><a href='http://underground.infovark.com/2010/01/05/3-useful-visual-studio-tricks-for-spark-templates/' rel='bookmark' title='3 Useful Visual Studio Tricks for Spark Templates'>3 Useful Visual Studio Tricks for Spark Templates</a></li>
<li><a href='http://underground.infovark.com/2008/07/28/how-to-avoid-visual-studio-help/' rel='bookmark' title='How to avoid Visual Studio Help'>How to avoid Visual Studio Help</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>Our build server wasn&#8217;t producing the same results as our local development machines. The same C# code yielded two different sets of code libraries. After much head-banging and hand-wringing, we finally traced the problem to an obscure error in the way Visual Studio 2008 handles <code>.csproj</code> files. </p>
<p>Basically, the Visual Studio IDE doesn&#8217;t handle the CopyLocal setting properly. This <a href="http://www.paraesthesia.com/archive/2008/02/13/what-to-do-if-copy-local-works-in-vs-but.aspx">post from paraesthesia</a> set us on the right track.</p>
<p>I have no idea what caused the problem. We had the same versions of the .NET Framework and Visual Studio 2008 installed on all our machines. The code was exactly the same. Apart from the build machine being Windows Server 2003 and out development machines being Windows XP, I can&#8217;t think of any other differences. But somehow the code libraries weren&#8217;t getting copied in the same way.</p>
<p>Just another adventure on our way toward the Beta release&#8230;</p>


<p>Related posts:<ol><li><a href='http://underground.infovark.com/2009/09/09/visual-studio-2008-and-the-copylocal-setting/' rel='bookmark' title='Visual Studio 2008 and the CopyLocal setting'>Visual Studio 2008 and the CopyLocal setting</a></li>
<li><a href='http://underground.infovark.com/2010/01/05/3-useful-visual-studio-tricks-for-spark-templates/' rel='bookmark' title='3 Useful Visual Studio Tricks for Spark Templates'>3 Useful Visual Studio Tricks for Spark Templates</a></li>
<li><a href='http://underground.infovark.com/2008/07/28/how-to-avoid-visual-studio-help/' rel='bookmark' title='How to avoid Visual Studio Help'>How to avoid Visual Studio Help</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://underground.infovark.com/2009/03/31/visual-studio-2008-and-its-copylocal-setting/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>WCF WebInvoke Body Format Error</title>
		<link>http://underground.infovark.com/2009/03/08/wcf-webinvoke-body-format-error/</link>
		<comments>http://underground.infovark.com/2009/03/08/wcf-webinvoke-body-format-error/#comments</comments>
		<pubDate>Sun, 08 Mar 2009 20:24:48 +0000</pubDate>
		<dc:creator>Dean</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[WCF]]></category>

		<guid isPermaLink="false">http://underground.infovark.com/?p=359</guid>
		<description><![CDATA[I spent Sunday afternoon battling an odd WCF error. System.InvalidOperationException: Incoming message for operation [your operation here] contains an unrecognized http body format value &#8216;Xml&#8217;. The expected body format value is &#8216;Raw&#8217;. This can be because a WebContentTypeMapper has not been configured on the binding. See the documentation of WebContentTypeMapper for more details. Troubleshooting this [...]


Related posts:<ol><li><a href='http://underground.infovark.com/2010/12/30/how-to-format-the-xaml-hyperlink-navigateuri/' rel='bookmark' title='How to format the XAML Hyperlink NavigateUri'>How to format the XAML Hyperlink NavigateUri</a></li>
<li><a href='http://underground.infovark.com/2009/02/18/using-wcf-to-return-html/' rel='bookmark' title='Using WCF to return HTML'>Using WCF to return HTML</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>I spent Sunday afternoon battling an odd WCF error. </p>
<blockquote><p>
System.InvalidOperationException: Incoming message for operation <em>[your operation here]</em> contains an unrecognized http body format value &#8216;Xml&#8217;. The expected body format value is &#8216;Raw&#8217;. This can be because a WebContentTypeMapper has not been configured on the binding. See the documentation of WebContentTypeMapper for more details.
</p></blockquote>
<p>Troubleshooting this issue <em>sucked</em>. Here&#8217;s the deal: WCF helpfully attempts to parse any incoming &#8220;text/xml&#8221; requests automatically. If you&#8217;ve defined the input parameter on a WebInvoke operation to be a Stream, WCF can&#8217;t bind to the method and returns an HTTP 400.</p>
<p>There&#8217;s two ways to solve this problem: Change the input parameter from Stream to XmlElement, or configure WCF to treat this request as Raw. I picked the former method. Carlos Figueira <a href="http://blogs.msdn.com/carlosfigueira/archive/2008/04/17/wcf-raw-programming-model-receiving-arbitrary-data.aspx">explains the latter method</a>.</p>
<p>If folks are interested, I can post some more detail about the problem and the resolution. For now, I have to finish making things work.</p>
<p><em>Edit: So here&#8217;s the rest of the story, since Brad asked.</em></p>
<p>I wasn&#8217;t sure exactly what triggered the issue. I got this behavior with just one WebInvoke and one WebGet operation using the same URI template. What I&#8217;d done was to create a generic ObjectService that exposed the same RESTian operations for several different types of objects. The particular operation in question looked something like this:</p>
<div class="geshi no csharp">
<ol>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#91;</span>OperationContract<span class="br0">&#93;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#91;</span>WebGet<span class="br0">&#40;</span>UriTemplate <span class="sy0">=</span> ItemUris.<span class="me1">IndexXml</span>, RequestFormat <span class="sy0">=</span> WebMessageFormat.<span class="me1">Xml</span>, ResponseFormat <span class="sy0">=</span> WebMessageFormat.<span class="me1">Xml</span><span class="br0">&#41;</span><span class="br0">&#93;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; Stream GetListAsXml<span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#91;</span>OperationContract<span class="br0">&#93;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#91;</span>WebInvoke<span class="br0">&#40;</span>UriTemplate <span class="sy0">=</span> ItemUris.<span class="me1">IndexXml</span>, Method <span class="sy0">=</span> <span class="st0">&quot;POST&quot;</span>, BodyStyle <span class="sy0">=</span> WebMessageBodyStyle.<span class="me1">Bare</span>, RequestFormat <span class="sy0">=</span> WebMessageFormat.<span class="me1">Xml</span>, ResponseFormat <span class="sy0">=</span> WebMessageFormat.<span class="me1">Xml</span><span class="br0">&#41;</span><span class="br0">&#93;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; Stream PostListAsXml<span class="br0">&#40;</span>XmlElement input<span class="br0">&#41;</span>;</div>
</li>
</ol>
</div>
<p>Originally, <code>PostListAsXml</code> had accepted a Stream. That seemed to work in other places, but then I started noticing the <code>InvalidOperationException</code> messages.</p>
<p>I think the right way to solve this problem is to follow Carlos&#8217; advice, and create a new WebContentTypeMapper-derived class. But I didn&#8217;t have time to figure out exactly where to plug it in, and I was afraid that I might introduce other problems. I just didn&#8217;t know enough about the inner workings of WCF to know whether that was a safe operation.</p>
<p>Since we hadn&#8217;t shipped the interface yet, I was free to change the return type from Stream to XmlElement for the one or two WebInvoke operations that were returning errors. Fewer lines of code needed to change, and I knew I wouldn&#8217;t break anything else. </p>
<p>Of course, I&#8217;m probably just setting myself up for more pain down the road, but sometimes you just need to get things done, y&#8217;know? </p>
<p>Anyway, since I&#8217;ll likely revisit this decision in a later release, I&#8217;d love to hear what other folks did in this situation.</p>


<p>Related posts:<ol><li><a href='http://underground.infovark.com/2010/12/30/how-to-format-the-xaml-hyperlink-navigateuri/' rel='bookmark' title='How to format the XAML Hyperlink NavigateUri'>How to format the XAML Hyperlink NavigateUri</a></li>
<li><a href='http://underground.infovark.com/2009/02/18/using-wcf-to-return-html/' rel='bookmark' title='Using WCF to return HTML'>Using WCF to return HTML</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://underground.infovark.com/2009/03/08/wcf-webinvoke-body-format-error/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Using WCF to return HTML</title>
		<link>http://underground.infovark.com/2009/02/18/using-wcf-to-return-html/</link>
		<comments>http://underground.infovark.com/2009/02/18/using-wcf-to-return-html/#comments</comments>
		<pubDate>Wed, 18 Feb 2009 15:28:22 +0000</pubDate>
		<dc:creator>Dean</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[REST]]></category>
		<category><![CDATA[WCF]]></category>
		<category><![CDATA[XML]]></category>
		<category><![CDATA[xslt]]></category>

		<guid isPermaLink="false">http://underground.infovark.com/?p=332</guid>
		<description><![CDATA[I just answered a WCF question on StackOverflow, and decided it was worth cross-posting here as well. The question was: What is the best / most flexible way to have WCF output XHTML? Here&#8217;s how we do it at Infovark. While I&#8217;m not sure that our approach is the best way, it does the job. [...]


Related posts:<ol><li><a href='http://underground.infovark.com/2009/12/11/better-ways-to-encode-html-in-c/' rel='bookmark' title='Better ways to Encode HTML in C#'>Better ways to Encode HTML in C#</a></li>
<li><a href='http://underground.infovark.com/2009/12/01/looking-for-an-iis-alternative/' rel='bookmark' title='Looking for an IIS Alternative'>Looking for an IIS Alternative</a></li>
<li><a href='http://underground.infovark.com/2008/04/09/wcf-bad-request/' rel='bookmark' title='WCF Bad Request'>WCF Bad Request</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>I just answered a <a href="http://en.wikipedia.org/wiki/Windows_Communication_Foundation">WCF</a> question on <a href="http://www.stackoverflow.com">StackOverflow</a>, and decided it was worth cross-posting here as well. </p>
<p>The question was: <a href="http://stackoverflow.com/questions/558515/what-is-the-best-most-flexible-way-to-have-wcf-output-xhtml">What is the best / most flexible way to have WCF output XHTML?</a></p>
<p>Here&#8217;s how we do it at Infovark. While I&#8217;m not sure that our approach is the <em>best</em> way, it does the job.</p>
<p>Our approach is to use the DataContractSerilizer to generate XML, then apply a Complied XSLT transform and return the result stream, which should now contain XHTML. Here&#8217;s a simplified version of our code:</p>
<div class="geshi no csharp">
<ol>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">public</span> Stream GetItemAsHtml<span class="br0">&#40;</span><span class="kw4">string</span> id<span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; Item obj <span class="sy0">=</span> GetItem<span class="br0">&#40;</span>objectId<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; Stream xml <span class="sy0">=</span> GetXmlStream<span class="br0">&#40;</span>obj<span class="br0">&#41;</span>; &nbsp; &nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> TransformXmlStream<span class="br0">&#40;</span>xml, defaultTransform<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#125;</span> &nbsp; &nbsp; &nbsp; &nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">public</span> <span class="kw1">static</span> Stream GetXmlStream<span class="br0">&#40;</span>IXmlSerializable item<span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; MemoryStream stream <span class="sy0">=</span> <span class="kw3">new</span> MemoryStream<span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">using</span> <span class="br0">&#40;</span>XmlWriter writer <span class="sy0">=</span> XmlWriter.<span class="me1">Create</span><span class="br0">&#40;</span>stream, <span class="kw3">new</span> XmlWriterSettings <span class="br0">&#123;</span> Encoding <span class="sy0">=</span> Encoding.<span class="me1">UTF8</span> <span class="br0">&#125;</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>writer <span class="sy0">!=</span> <span class="kw1">null</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DataContractSerializer dcs <span class="sy0">=</span> <span class="kw3">new</span> DataContractSerializer<span class="br0">&#40;</span>item.<span class="me1">GetType</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dcs.<span class="me1">WriteObject</span><span class="br0">&#40;</span>writer, item<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; writer.<span class="me1">Flush</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; writer.<span class="me1">Close</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; stream.<span class="me1">Seek</span><span class="br0">&#40;</span><span class="nu0">0</span>, SeekOrigin.<span class="me1">Begin</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> stream;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">public</span> <span class="kw1">static</span> Stream TransformXmlStream<span class="br0">&#40;</span>Stream xml, <span class="kw4">string</span> xsltFile<span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; XmlReader reader <span class="sy0">=</span> XmlReader.<span class="me1">Create</span><span class="br0">&#40;</span>xml<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; XslCompiledTransform trans <span class="sy0">=</span> <span class="kw3">new</span> XslCompiledTransform<span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; trans.<span class="me1">Load</span><span class="br0">&#40;</span>xsltFile<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; MemoryStream stream <span class="sy0">=</span> <span class="kw3">new</span> MemoryStream<span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">using</span> <span class="br0">&#40;</span>XmlWriter writer <span class="sy0">=</span> XmlWriter.<span class="me1">Create</span><span class="br0">&#40;</span>stream, trans.<span class="me1">OutputSettings</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>writer <span class="sy0">!=</span> <span class="kw1">null</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; trans.<span class="me1">Transform</span><span class="br0">&#40;</span>reader, writer<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; writer.<span class="me1">Flush</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; writer.<span class="me1">Close</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; stream.<span class="me1">Seek</span><span class="br0">&#40;</span><span class="nu0">0</span>, SeekOrigin.<span class="me1">Begin</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> stream;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>It works for us, but if you&#8217;ve got other, better ideas, please let me know! </p>


<p>Related posts:<ol><li><a href='http://underground.infovark.com/2009/12/11/better-ways-to-encode-html-in-c/' rel='bookmark' title='Better ways to Encode HTML in C#'>Better ways to Encode HTML in C#</a></li>
<li><a href='http://underground.infovark.com/2009/12/01/looking-for-an-iis-alternative/' rel='bookmark' title='Looking for an IIS Alternative'>Looking for an IIS Alternative</a></li>
<li><a href='http://underground.infovark.com/2008/04/09/wcf-bad-request/' rel='bookmark' title='WCF Bad Request'>WCF Bad Request</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://underground.infovark.com/2009/02/18/using-wcf-to-return-html/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Put the &#8220;Web&#8221; Back in Web Services</title>
		<link>http://underground.infovark.com/2009/02/05/put-the-web-back-in-web-services/</link>
		<comments>http://underground.infovark.com/2009/02/05/put-the-web-back-in-web-services/#comments</comments>
		<pubDate>Thu, 05 Feb 2009 18:52:32 +0000</pubDate>
		<dc:creator>Dean</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[REST]]></category>
		<category><![CDATA[WCF]]></category>
		<category><![CDATA[Web Services]]></category>

		<guid isPermaLink="false">http://underground.infovark.com/?p=8</guid>
		<description><![CDATA[I know it&#8217;s possible to transmit Internet Protocol by carrier pigeon, but I&#8217;m not sure I could recommend it to our customers with a straight face. By the same token, I&#8217;m continually surprised to hear vendors and consultants insist that Web Services can be done without the web. Yet I&#8217;ve seen that claim repeated all [...]


Related posts:<ol><li><a href='http://underground.infovark.com/2008/04/10/reviewrestful-webservices/' rel='bookmark' title='Review: Restful Web Services'>Review: Restful Web Services</a></li>
<li><a href='http://underground.infovark.com/2009/03/24/rest-how-to-respond-to-an-http-post/' rel='bookmark' title='REST: How to respond to an HTTP POST'>REST: How to respond to an HTTP POST</a></li>
<li><a href='http://underground.infovark.com/2008/04/22/using-wcf-for-rest-part-1/' rel='bookmark' title='Using WCF for REST, Part 1'>Using WCF for REST, Part 1</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>I know it&#8217;s <em>possible</em> to transmit <a href="http://en.wikipedia.org/wiki/IP_over_Avian_Carriers">Internet Protocol by carrier pigeon</a>, but I&#8217;m not sure I could recommend it to our customers with a straight face. By the same token, I&#8217;m continually surprised to hear vendors and consultants insist that Web Services can be done <em>without the web</em>. </p>
<p>Yet I&#8217;ve seen that claim repeated all over the Internet. Sure, it&#8217;s true in the academic sense. Technically, there&#8217;s nothing wrong with &#8220;webless&#8221; web services, just like IP by avian carrier will eventually get the job done (though latency and packet loss are a challenge). But practically speaking, why would you use anything other than HTTP?</p>
<p>The &#8220;webless web services&#8221; meme has infected a number of good ideas. The SOAP standard is a case in point. At first, SOAP was a simple XML wrapper around an XML payload. (Remember when the S in SOAP stood for Simple?) Then a variety of industry heavyweights jumped on the bandwagon, and soon SOAP could be used with <em>any</em> protocol. </p>
<p>With, uh, a few changes. Like <a href="http://en.wikipedia.org/wiki/Web_services">five or six additional specifications</a>. And schema. Lots of schema. <a href="http://www.w3.org/2003/Talks/05-gartner-tbl/slide6-0.html">Maybe a diagram would help?</a> No?</p>
<h4>Why not use REST instead?</h4>
<p><a href="http://blogs.msdn.com/drnick/archive/2009/02/03/stefan-tilkov-on-rest.aspx">Nicholas Allen</a> recently shared a great presentation on <a href="http://www.infoq.com/presentations/qcon-tilkov-rest-intro">REST and SOA</a> given by Stefan Tilkov at QCon. Stefan makes some great points about how RESTful web service design aligns well with the goals of Service Oriented Architecture (SOA). </p>
<p>Sure, you <em>could</em> use the WS-* stack if you like, but RESTful web architecture is a proven, scalable and truly simple approach, as long as you don&#8217;t mind having to use HTTP.</p>
<p>Much of the added plumbing in the WS-* stack is there to help transition older computer-to-computer network communication technologies. It allows applications that depend on <a href="http://en.wikipedia.org/wiki/CORBA">CORBA</a> or <a href="http://en.wikipedia.org/wiki/Distributed_Component_Object_Model">DCOM</a> to tunnel through firewalls. But unless you&#8217;re trying to retrofit a system built before, say, <a href="http://www.wdvl.com/Authoring/Languages/XML/News/Archives/News1998.html">1998</a>, you can skip all that stuff.</p>
<p>Wasn&#8217;t using web protocol the whole point of web services anyway? As Stefan said in his closing comments: &#8220;Protocol independence is a bug, not a feature.&#8221;</p>
<p>Use a RESTful architecture for new development. Put the <strong>web</strong> back in <strong>web services</strong>.</p>


<p>Related posts:<ol><li><a href='http://underground.infovark.com/2008/04/10/reviewrestful-webservices/' rel='bookmark' title='Review: Restful Web Services'>Review: Restful Web Services</a></li>
<li><a href='http://underground.infovark.com/2009/03/24/rest-how-to-respond-to-an-http-post/' rel='bookmark' title='REST: How to respond to an HTTP POST'>REST: How to respond to an HTTP POST</a></li>
<li><a href='http://underground.infovark.com/2008/04/22/using-wcf-for-rest-part-1/' rel='bookmark' title='Using WCF for REST, Part 1'>Using WCF for REST, Part 1</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://underground.infovark.com/2009/02/05/put-the-web-back-in-web-services/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Using WCF for REST, Part 3</title>
		<link>http://underground.infovark.com/2008/05/27/using-wcf-for-rest-part-3/</link>
		<comments>http://underground.infovark.com/2008/05/27/using-wcf-for-rest-part-3/#comments</comments>
		<pubDate>Tue, 27 May 2008 15:44:52 +0000</pubDate>
		<dc:creator>Dean</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[REST]]></category>
		<category><![CDATA[WCF]]></category>

		<guid isPermaLink="false">http://underground.infovark.com/?p=9</guid>
		<description><![CDATA[The easiest way to explain the issues we encountered when implementing REST is to work through the design principles we followed. I think much of our trouble came from the fact that we come from a web applications background, not a SOAP services background. I&#8217;m hoping that by laying out our REST design, some of [...]


Related posts:<ol><li><a href='http://underground.infovark.com/2008/05/17/using-wcf-for-rest-part-2/' rel='bookmark' title='Using WCF for REST, Part 2'>Using WCF for REST, Part 2</a></li>
<li><a href='http://underground.infovark.com/2008/04/22/using-wcf-for-rest-part-1/' rel='bookmark' title='Using WCF for REST, Part 1'>Using WCF for REST, Part 1</a></li>
<li><a href='http://underground.infovark.com/2009/02/05/put-the-web-back-in-web-services/' rel='bookmark' title='Put the &#8220;Web&#8221; Back in Web Services'>Put the &#8220;Web&#8221; Back in Web Services</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>The easiest way to explain the <a href="http://underground.infovark.com/2008/05/17/using-wcf-for-rest-part-2/">issues we encountered when implementing REST</a> is to work through the design principles we followed. I think much of our trouble came from the fact that we come from a web applications background, not a SOAP services background. I&#8217;m hoping that by laying out our REST design, some of the Microsoft gurus can help us do things &#8220;the WCF way.&#8221; And perhaps we can help the WCF team out by highlighting a handful of places where we found WCF unintuitive.</p>
<h4>The Importance of Being Addressable</h4>
<p>Fundamentally, the REST pattern is about making resources available. This means that each item stored within your system can be accessed by someone with the correct permissions. Every one of these items has its own unique address, and its address should not change. This consistency is important, because it allows both people and computer programs to remember and reference items in your system.</p>
<p>Note that we&#8217;re talking about <em>resources </em>or <em>items</em>. In contrast with the SOAP model of web services, which allows programmers to invoke procedures on remote computers, REST is about providing data. SOAP is about <em>verbs</em>, while REST is about <em>nouns</em>. A SOAP service might <code>CalculateTotalSale()</code>; a REST service  provides  <code>CustomerRecieptNo_12345</code>. The kind of web services architecture you use will depend on the kind of application you&#8217;re building. The choice has major implications for the other components of your system.</p>
<p>REST imposes restrictions on what sort of things you can do, because it supports only a handful of actions: GET, POST, PUT, and DELETE. (There are a few other HTTP methods, but these four are the most important.) Fortunately, with these four actions, you can accomplish most basic programming tasks. There&#8217;s a close parallel to these actions and create/read/update/delete, or <a href="http://en.wikipedia.org/wiki/Create%2C_read%2C_update_and_delete">CRUD</a>, the building blocks of data storage systems.</p>
<h4>UriTemplate</h4>
<p>Since the address, or URI, is the primary way to access information in your system, it&#8217;s effectively part of your user interface. All the principles of good user interface design apply. So when designing a REST service, you need fine control over the structure of these identifiers.</p>
<p>SOAP, by contrast, typically has just one endpoint. The address itself conveys no information about what services are provided &#8212; that&#8217;s why SOAP services require a separate WSDL file to tell folks what&#8217;s possible. With REST, it should be easy to discover the extent of the system by looking at the URIs alone.</p>
<p>Coming up with good REST URI patterns can be tricky. Using short, descriptive naming conventions for your resources makes them easier to type. But URI patterns must also be distinct and unambiguous.</p>
<p>In the .NET framework, you use the UriTemplate class to define patterns. The UriTemplate implementation that shipped with .NET 3.5 allowed you to define variables that fit into slots in your URI. A typical UriTemplate might look like this: <code>http://restserver/{object}/{id}?view={viewname}</code>. </p>
<p>WCF looks for incoming URIs that match the patterns you define. The pattern defined above would match the following URIs:</p>
<ul>
<li><code>http://restserver/customer/5?view=profile</code></li>
<li><code>http://restserver/article/how_to_do_stuff?view=print</code></li>
<li><code>http://restserver/author/John-Smith?view=1</code></li>
</ul>
<p>Once you&#8217;ve defined a UriTemplate, you bind it to a method that has the same number of parameters.  (I won&#8217;t go into the ABCs of WCF here, but you can check out this <a href="http://msdn.microsoft.com/en-us/library/aa480190.aspx">MSDN Introduction to WCF</a> if you need a refresher.)</p>
<p>In WCF 3.5, you could only define a variable for a <em>whole segment</em>. A segment is basically the bit between the one forward slash and another, or one querystring parameter. A few bloggers <a href="http://www.aaronlerch.com/blog/2007/07/16/a-humble-wcf-feature-request-or-four/">requested more flexibility in UriTemplates</a>, and the WCF team answered with the soon-to-be-released 3.5 SP1. The ability to define variables for <em>partial</em> segments was crucial for our URI design.</p>
<h4>Representation Matters</h4>
<p>Most books about web services, including <a href="http://underground.infovark.com/2008/04/10/reviewrestful-webservices/">RESTful Web Services</a>, advocate leaving off file extensions from your URIs. This makes sense for SOAP, where you&#8217;re accessing methods and all responses are transmitted in XML. But in REST, you&#8217;re serving up <em>items</em>. </p>
<p>In our case, some of these items being served were files and some were records from a database. It seemed inconsistent to have some endpoints that had file extensions and others that didn&#8217;t. And we also wanted to be able to serve up different representations for our database records. Our REST service supports both JSON, XML, and HTML. It made sense to use a file extension to distinguish between the different representations.</p>
<p>One workaround would have been to create endpoints like <code>http://restserver/form/1040/xml</code> but that looked funny next to URIs like <code>http://resterver/file/documentation.pdf</code>. True RESTafarians would point out that neither the &#8220;/xml&#8221; or the &#8220;.pdf&#8221; are needed, since you can request an appropriate representation using the HTTP ACCEPT header. We decided against the header approach because not all browsers use the ACCEPT HTTP header. Besides, it might be useful for us humans to be able to reach alternate formats by simply changing the URL in the browser address bar. </p>
<p>In WCF 3.5, this required us to create <em>three times</em> the number of endpoints, with a separate method to handle each. We can&#8217;t wait for the official release of 3.5 SP1 to make UriTemplates like <code>http://restserver/user/{id}.{ext}</code> possible. </p>
<h4>The Final Slash</h4>
<p>Another source of endpoint duplication was the need to have <em>two</em> different endpoints for <code>http://restserver/folder</code> and <code>http://restserver/folder/</code>. Because the slash is used as a segment delimiter, the dispatcher in WCF 3.5 saw these two URLs as fundamentally different. </p>
<p>So handling what we thought were fairly trivial cases in URI patterns led us to create <strong>FIVE TIMES</strong> the number of endpoints we wanted. It&#8217;s a maintenance nightmare. SP1 can&#8217;t get here soon enough. </p>


<p>Related posts:<ol><li><a href='http://underground.infovark.com/2008/05/17/using-wcf-for-rest-part-2/' rel='bookmark' title='Using WCF for REST, Part 2'>Using WCF for REST, Part 2</a></li>
<li><a href='http://underground.infovark.com/2008/04/22/using-wcf-for-rest-part-1/' rel='bookmark' title='Using WCF for REST, Part 1'>Using WCF for REST, Part 1</a></li>
<li><a href='http://underground.infovark.com/2009/02/05/put-the-web-back-in-web-services/' rel='bookmark' title='Put the &#8220;Web&#8221; Back in Web Services'>Put the &#8220;Web&#8221; Back in Web Services</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://underground.infovark.com/2008/05/27/using-wcf-for-rest-part-3/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>WCF Instance Context</title>
		<link>http://underground.infovark.com/2008/05/21/wcf-instance-context/</link>
		<comments>http://underground.infovark.com/2008/05/21/wcf-instance-context/#comments</comments>
		<pubDate>Wed, 21 May 2008 22:31:19 +0000</pubDate>
		<dc:creator>Dean</dc:creator>
				<category><![CDATA[REST]]></category>
		<category><![CDATA[WCF]]></category>
		<category><![CDATA[Web Services]]></category>

		<guid isPermaLink="false">http://underground.infovark.com/?p=23</guid>
		<description><![CDATA[I finally figured out the source of my HTTP 400 problem. Apparently the Windows Communication Foundation deals with exceptions differently depending on your InstanceContextMode settings. I had been using the Single setting but I should have used the PerCall setting. In PerCall mode, the try/catch block works as expected. I think it has something to [...]


Related posts:<ol><li><a href='http://underground.infovark.com/2008/05/17/using-wcf-for-rest-part-2/' rel='bookmark' title='Using WCF for REST, Part 2'>Using WCF for REST, Part 2</a></li>
<li><a href='http://underground.infovark.com/2008/03/28/calling-a-static-method-from-an-object-instance-in-c/' rel='bookmark' title='Calling a Static Method from an Object Instance in C#'>Calling a Static Method from an Object Instance in C#</a></li>
<li><a href='http://underground.infovark.com/2008/04/22/using-wcf-for-rest-part-1/' rel='bookmark' title='Using WCF for REST, Part 1'>Using WCF for REST, Part 1</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>I finally figured out the source of my <a href="http://underground.infovark.com/2008/04/09/wcf-bad-request/">HTTP 400</a> problem. Apparently the Windows Communication Foundation deals with exceptions differently depending on your <a href="http://msdn.microsoft.com/en-us/library/system.servicemodel.instancecontextmode.aspx">InstanceContextMode</a> settings. I had been using the <code>Single</code> setting but I should have used the <code>PerCall</code> setting. In PerCall mode, the try/catch block works as expected.</p>
<p>I think it has something to do with the way that WCF distinguishes between channel exceptions and message faults.</p>
<p>Anyway, if you&#8217;re building a REST web service, you&#8217;ll want to make sure your class is decorated with the following <a href="http://msdn.microsoft.com/en-us/library/system.servicemodel.servicebehaviorattribute.instancecontextmode.aspx">ServiceBehavior</a> attribute.</p>
<div class="geshi no csharp">
<ol>
<li class="li1">
<div class="de1"><span class="br0">&#91;</span>ServiceBehavior<span class="br0">&#40;</span>InstanceContextMode <span class="sy0">=</span> InstanceContextMode.<span class="me1">PerCall</span><span class="br0">&#41;</span><span class="br0">&#93;</span></div>
</li>
</ol>
</div>


<p>Related posts:<ol><li><a href='http://underground.infovark.com/2008/05/17/using-wcf-for-rest-part-2/' rel='bookmark' title='Using WCF for REST, Part 2'>Using WCF for REST, Part 2</a></li>
<li><a href='http://underground.infovark.com/2008/03/28/calling-a-static-method-from-an-object-instance-in-c/' rel='bookmark' title='Calling a Static Method from an Object Instance in C#'>Calling a Static Method from an Object Instance in C#</a></li>
<li><a href='http://underground.infovark.com/2008/04/22/using-wcf-for-rest-part-1/' rel='bookmark' title='Using WCF for REST, Part 1'>Using WCF for REST, Part 1</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://underground.infovark.com/2008/05/21/wcf-instance-context/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Using WCF for REST, Part 2</title>
		<link>http://underground.infovark.com/2008/05/17/using-wcf-for-rest-part-2/</link>
		<comments>http://underground.infovark.com/2008/05/17/using-wcf-for-rest-part-2/#comments</comments>
		<pubDate>Sat, 17 May 2008 18:16:53 +0000</pubDate>
		<dc:creator>Dean</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[REST]]></category>
		<category><![CDATA[WCF]]></category>
		<category><![CDATA[Web Services]]></category>

		<guid isPermaLink="false">http://underground.infovark.com/?p=21</guid>
		<description><![CDATA[In part one of this series, I listed several websites and blogs that had useful information on the Windows Communication Foundation (WCF) and REST. I also mentioned that if I was stating again, I&#8217;d probably use something other than WCF. Perhaps deriving my own REST server from System.Net.HTTPListener, for example. Vish asked for some additional [...]


Related posts:<ol><li><a href='http://underground.infovark.com/2008/05/27/using-wcf-for-rest-part-3/' rel='bookmark' title='Using WCF for REST, Part 3'>Using WCF for REST, Part 3</a></li>
<li><a href='http://underground.infovark.com/2008/04/22/using-wcf-for-rest-part-1/' rel='bookmark' title='Using WCF for REST, Part 1'>Using WCF for REST, Part 1</a></li>
<li><a href='http://underground.infovark.com/2009/03/24/rest-how-to-respond-to-an-http-post/' rel='bookmark' title='REST: How to respond to an HTTP POST'>REST: How to respond to an HTTP POST</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>In <a href="http://underground.infovark.com/2008/04/22/using-wcf-for-rest-part-1/">part one</a> of this series, I listed several websites and blogs that had useful information on the Windows Communication Foundation (WCF) and REST. I also mentioned that if I was stating again, I&#8217;d probably use something other than WCF. Perhaps deriving my own REST server from <a href="http://msdn.microsoft.com/en-us/library/system.net.httplistener.aspx">System.Net.HTTPListener</a>, for example.</p>
<p><a href="http://underground.infovark.com/2008/04/22/using-wcf-for-rest-part-1/#comment-18">Vish asked for some additional details</a> in his comment to that post. He works on the Microsoft WCF development team and was curious about our experience.</p>
<p>I had just begun putting together my response when I noticed Scott Guthrie&#8217;s post about <a href="http://weblogs.asp.net/scottgu/archive/2008/05/12/visual-studio-2008-and-net-framework-3-5-service-pack-1-beta.aspx">Service Pack 1 for the .NET Framework 3.5 beta release</a>. Steve Maine also posted specifics about the <a href="http://hyperthink.net/blog/2008/05/12/We+Are+Pleased+To+Bring+You+New+Features+In+NET+35+SP1.aspx">ADO.Net Data Services and WCF changes</a>.</p>
<p>So, Vish, it seems your team&#8217;s beaten me to the punch on some of these issues! Many of the difficulties I was having with WCF and REST were addressed by the service pack. Here&#8217;s an overview of our key stumbling blocks:</p>
<ol>
<li>REST requires much greater control over the URI than SOAP does, and the URITemplate class just wasn&#8217;t up to the task. We had to hardcode most of our endpoints to compensate. (Fixed in SP1. Hooray!)</li>
<li>Supporting multiple formats, such as serving both XML and JSON, either require you to program against Stream or require twice the number of endpoints.</li>
<li>Existing serializers had trouble with complicated object graphs, forcing us to perform serialization/deserialization ourselves. (This seems greatly improved in SP1.)</li>
<li>WCF allows only one contract/interface per endpoint. This makes it tricky to factor out common contract patterns.</li>
<li>Good REST practice would have you return many kinds of errors as HTTP status messages. SOAP embeds all error information in the returned XML. WCF is closely aligned with the SOAP approach, which means that you&#8217;ve got to be very careful distinguishing exceptions from faults when implementing REST in WCF. It was an unpleasant surprise, and we had to do quite a bit of work to deal with that.</li>
</ol>
<p>I&#8217;ll talk about all five of these areas in more detail in upcoming posts in this series. And I&#8217;ll be sure check out the SP1 beta once we get our Infovark Alpha release out the door.</p>


<p>Related posts:<ol><li><a href='http://underground.infovark.com/2008/05/27/using-wcf-for-rest-part-3/' rel='bookmark' title='Using WCF for REST, Part 3'>Using WCF for REST, Part 3</a></li>
<li><a href='http://underground.infovark.com/2008/04/22/using-wcf-for-rest-part-1/' rel='bookmark' title='Using WCF for REST, Part 1'>Using WCF for REST, Part 1</a></li>
<li><a href='http://underground.infovark.com/2009/03/24/rest-how-to-respond-to-an-http-post/' rel='bookmark' title='REST: How to respond to an HTTP POST'>REST: How to respond to an HTTP POST</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://underground.infovark.com/2008/05/17/using-wcf-for-rest-part-2/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Using WCF for REST, Part 1</title>
		<link>http://underground.infovark.com/2008/04/22/using-wcf-for-rest-part-1/</link>
		<comments>http://underground.infovark.com/2008/04/22/using-wcf-for-rest-part-1/#comments</comments>
		<pubDate>Tue, 22 Apr 2008 15:51:56 +0000</pubDate>
		<dc:creator>Dean</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[REST]]></category>
		<category><![CDATA[WCF]]></category>
		<category><![CDATA[Web Services]]></category>

		<guid isPermaLink="false">http://underground.infovark.com/?p=19</guid>
		<description><![CDATA[Just because you can do something doesn&#8217;t mean it&#8217;s a good idea. We decided to use the Windows Communication Foundation to drive our REST-based web service. In hindsight, it was a poor choice. REST support in WCF seems like it was a last-minute addition to .NET 3.5. You can certainly hack something together, but I&#8217;ve [...]


Related posts:<ol><li><a href='http://underground.infovark.com/2008/05/17/using-wcf-for-rest-part-2/' rel='bookmark' title='Using WCF for REST, Part 2'>Using WCF for REST, Part 2</a></li>
<li><a href='http://underground.infovark.com/2008/05/27/using-wcf-for-rest-part-3/' rel='bookmark' title='Using WCF for REST, Part 3'>Using WCF for REST, Part 3</a></li>
<li><a href='http://underground.infovark.com/2009/02/05/put-the-web-back-in-web-services/' rel='bookmark' title='Put the &#8220;Web&#8221; Back in Web Services'>Put the &#8220;Web&#8221; Back in Web Services</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>Just because you <em>can </em>do something doesn&#8217;t mean it&#8217;s a good idea.</p>
<p>We decided to use the Windows Communication Foundation to drive our REST-based web service. In hindsight, it was a poor choice. REST support in WCF seems like it was a last-minute addition to .NET 3.5. You can certainly hack something together, but I&#8217;ve found few real-world examples on the Internet, and most of those sidestep the tricky issues.</p>
<p>Here&#8217;s the short of it: WCF was designed for RPC-SOAP. More importantly, it was designed to SOA-enable legacy services that used older communications channels like DCOM. If you&#8217;re starting from scratch, and have full control over the output of your web service and the design of your object model, I&#8217;d recommend using a different (and simpler) framework.</p>
<p>We&#8217;ve gotten a good bit of blog traffic from people looking for help with Windows Communication Foundation and the REST architectural pattern. (It&#8217;s good to know that we&#8217;re not the only ones needing advice.) Here are the better sources we&#8217;ve found so far.</p>
<p><a title="WCF" href="http://msdn2.microsoft.com/en-us/library/ms735119.aspx">Windows Communication Foundation</a> documentation on MSDN</p>
<p>Good <a href="http://qcon.infoq.com/sanfrancisco/presentation/REST,+JSON+and+RSS+with+Windows+Communication+Foundation+3.5">overview presentation</a> on REST and Syndication using WCF</p>
<p>Microsoft&#8217;s <a title="WCF Picture Services Sample" href="http://samples.netfx3.com/pictureservices/">Picture Services Sample</a></p>
<p>Justin Smith&#8217;s <a href="http://blogs.msdn.com/justinjsmith/archive/tags/Web+Programming+with+WCF/default.aspx">WCF articles on Cybertopian Chronicles</a></p>
<p><a href="http://blogs.msdn.com/drnick/">Nicholas Allen&#8217;s Indigo blog</a></p>
<p><a href="http://hyperthink.net/blog/">Steve Maine&#8217;s blog</a></p>
<p>Assorted posts on <a href="http://www.west-wind.com/WebLog/default.aspx">Rick Strahl&#8217;s blog</a></p>
<p><a href="http://www.thatindigogirl.com/">That Indigo Girl</a></p>
<p>If you find other useful places to look, let us know!</p>


<p>Related posts:<ol><li><a href='http://underground.infovark.com/2008/05/17/using-wcf-for-rest-part-2/' rel='bookmark' title='Using WCF for REST, Part 2'>Using WCF for REST, Part 2</a></li>
<li><a href='http://underground.infovark.com/2008/05/27/using-wcf-for-rest-part-3/' rel='bookmark' title='Using WCF for REST, Part 3'>Using WCF for REST, Part 3</a></li>
<li><a href='http://underground.infovark.com/2009/02/05/put-the-web-back-in-web-services/' rel='bookmark' title='Put the &#8220;Web&#8221; Back in Web Services'>Put the &#8220;Web&#8221; Back in Web Services</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://underground.infovark.com/2008/04/22/using-wcf-for-rest-part-1/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>WCF Bad Request</title>
		<link>http://underground.infovark.com/2008/04/09/wcf-bad-request/</link>
		<comments>http://underground.infovark.com/2008/04/09/wcf-bad-request/#comments</comments>
		<pubDate>Wed, 09 Apr 2008 23:27:32 +0000</pubDate>
		<dc:creator>Dean</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[REST]]></category>
		<category><![CDATA[WCF]]></category>
		<category><![CDATA[Web Services]]></category>

		<guid isPermaLink="false">http://underground.infovark.com/?p=14</guid>
		<description><![CDATA[I&#8217;ve just identified a horrible bug in WCF for the .NET Framework 3.5. A caught exception in a WebInvoke operation will cause WCF to return an HTTP 400 Bad Request status code to the client. Any caught exception. Every time. Regardless of whatever error code you might want to send back. I found the error [...]


Related posts:<ol><li><a href='http://underground.infovark.com/2009/03/24/rest-how-to-respond-to-an-http-post/' rel='bookmark' title='REST: How to respond to an HTTP POST'>REST: How to respond to an HTTP POST</a></li>
<li><a href='http://underground.infovark.com/2008/05/21/wcf-instance-context/' rel='bookmark' title='WCF Instance Context'>WCF Instance Context</a></li>
<li><a href='http://underground.infovark.com/2008/05/17/using-wcf-for-rest-part-2/' rel='bookmark' title='Using WCF for REST, Part 2'>Using WCF for REST, Part 2</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve just identified a horrible bug in WCF for the .NET Framework 3.5.</p>
<p>A caught exception in a WebInvoke operation will cause WCF to return an HTTP 400 Bad Request status code to the client. <em>Any </em>caught exception. <em>Every </em>time. Regardless of whatever error code you might <em>want </em>to send back.</p>
<p>I found the error by mistake. I&#8217;d used &#8220;BadGateway&#8221; instead of &#8220;BadRequest&#8221; in my code. If it weren&#8217;t for other odd WCF behavior, I wouldn&#8217;t have noticed that my status code was being ignored.</p>
<p>Consider the following example:</p>
<div class="geshi no csharp">
<ol>
<li class="li1">
<div class="de1">&nbsp;<span class="co1">// Read the Xml into our object and save.</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="kw1">try</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="co1">// The following line triggered the error.</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; obj.<span class="me1">ReadXml</span><span class="br0">&#40;</span>reader<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; obj.<span class="me1">Save</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="co1">// Set HTTP Cache Options and MIME Type.</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; Utilities.<span class="me1">SetCaching</span><span class="br0">&#40;</span>WebOperationContext.<span class="me1">Current</span>, obj.<span class="me1">DateModified</span>, <span class="nu0">60</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; Utilities.<span class="me1">SetMimeType</span><span class="br0">&#40;</span>Format.<span class="me1">Xml</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">return</span> Utilities.<span class="me1">GetXmlStream</span><span class="br0">&#40;</span>obj<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="kw1">catch</span> <span class="br0">&#40;</span>Exception e<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="co1">// Was it a schema validation error? If so, provide detail.</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span><span class="sy0">!</span><span class="kw4">string</span>.<span class="me1">IsNullOrEmpty</span><span class="br0">&#40;</span>_XmlValidationErrors<span class="br0">&#41;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;<span class="co1">// I slipped here, using BadGateway 502 instead of Bad Request 400.</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;<span class="co1">// But WCF doesn&#39;t care. If you enter the catch block it&#39;s _always_ 400.</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;WebOperationContext.<span class="me1">Current</span>.<span class="me1">OutgoingResponse</span>.<span class="me1">StatusCode</span> <span class="sy0">=</span> HttpStatusCode.<span class="me1">BadGateway</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;WebOperationContext.<span class="me1">Current</span>.<span class="me1">OutgoingResponse</span>.<span class="me1">StatusDescription</span> <span class="sy0">=</span> _XmlValidationErrors;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;WebOperationContext.<span class="me1">Current</span>.<span class="me1">OutgoingResponse</span>.<span class="me1">SuppressEntityBody</span> <span class="sy0">=</span> <span class="kw1">false</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;<span class="kw1">return</span> <span class="kw1">null</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>If no error occurs, WCF will return the status code you specify. A try/finally block will work just fine; WCF returns whatever status code you specify. Enter a catch block, though, and it&#8217;s nothing but <strong>400 Bad Request</strong>. </p>
<p>Hey, if there&#8217;s an error, it <em>must </em>be the client&#8217;s fault, right?</p>


<p>Related posts:<ol><li><a href='http://underground.infovark.com/2009/03/24/rest-how-to-respond-to-an-http-post/' rel='bookmark' title='REST: How to respond to an HTTP POST'>REST: How to respond to an HTTP POST</a></li>
<li><a href='http://underground.infovark.com/2008/05/21/wcf-instance-context/' rel='bookmark' title='WCF Instance Context'>WCF Instance Context</a></li>
<li><a href='http://underground.infovark.com/2008/05/17/using-wcf-for-rest-part-2/' rel='bookmark' title='Using WCF for REST, Part 2'>Using WCF for REST, Part 2</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://underground.infovark.com/2008/04/09/wcf-bad-request/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>

