<?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/"
	xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Dave Jansen's Blog &#187; WCF</title>
	<atom:link href="http://dansen.wordpress.com/category/wcf/feed/" rel="self" type="application/rss+xml" />
	<link>http://dansen.wordpress.com</link>
	<description>Thoughts... Words... Stories... Experiences...</description>
	<lastBuildDate>Wed, 25 Jun 2008 23:32:53 +0000</lastBuildDate>
	<generator>http://wordpress.com/</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<cloud domain='dansen.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://www.gravatar.com/blavatar/e960aec7a4a664225f78efe5b222056e?s=96&#038;d=http://s.wordpress.com/i/buttonw-com.png</url>
		<title>Dave Jansen's Blog &#187; WCF</title>
		<link>http://dansen.wordpress.com</link>
	</image>
			<item>
		<title>Error when Behaviour Extension types are not fully qualified</title>
		<link>http://dansen.wordpress.com/2008/04/23/exception-when-not-fully-qualifying-behaviour-extensions/</link>
		<comments>http://dansen.wordpress.com/2008/04/23/exception-when-not-fully-qualifying-behaviour-extensions/#comments</comments>
		<pubDate>Wed, 23 Apr 2008 12:58:13 +0000</pubDate>
		<dc:creator>dansen</dc:creator>
				<category><![CDATA[WCF]]></category>

		<guid isPermaLink="false">http://dansen.wordpress.com/2008/04/23/exception-when-not-fully-qualifying-behaviour-extensions/</guid>
		<description><![CDATA[Custom behaviour extensions are a great way to customise service behaviour such as implementing exception shielding as described here in Rory&#8217;s post&#160; or message validation using the Microsoft Enterprise Library Validation Application block.
I recently implemented a custom WCF behaviour extension for shielding exceptions when I came across the following cryptic exception.
&#8220;An error occurred during the [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dansen.wordpress.com&blog=1210400&post=45&subd=dansen&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>Custom behaviour extensions are a great way to customise service behaviour such as implementing exception shielding as described here in <a href="http://neovolve.com/archive/2008/04/07/implementing-ierrorhandler.aspx">Rory&#8217;s post</a>&nbsp; or message validation using the <a href="http://www.codeproject.com/KB/WCF/WCFVAB.asp">Microsoft Enterprise Library Validation Application block.</a></p>
<p>I recently implemented a custom WCF behaviour extension for shielding exceptions when I came across the following cryptic exception.</p>
<blockquote><p><font face="Arial" color="#ff0000">&#8220;An error occurred during the processing of a configuration file required to service this request. Please review the specific error details below and modify your configuration file appropriately.&#8221;</font></p>
</blockquote>
<p><a href="http://dansen.files.wordpress.com/2008/04/image5.png"><img style="border-width:0;" height="445" alt="image" src="http://dansen.files.wordpress.com/2008/04/image-thumb5.png?w=556&#038;h=445" width="556" border="0"></a></p>
<p>While the message was somewhat cryptic, it pointed me to the service behaviour were I realised I had specified the incorrect version on the type attribute.</p>
<h2>Steps</h2>
<p>1. Create new behaviour extension. My extension shielded and traced certain exceptions using the <a href="http://msdn2.microsoft.com/en-us/library/system.servicemodel.dispatcher.ierrorhandler.aspx">IErrorHandler</a>.<br />2. Use <a href="http://msdn2.microsoft.com/en-us/library/ms732009.aspx">SVC Configuration Editor</a> to plug the extension in. It added the following configuration as expected.</p>
<div style="font-size:10pt;background:white;color:black;font-family:courier new;">
<p style="margin:0;"><span style="color:blue;">&lt;</span><span style="color:#a31515;">system.serviceModel</span><span style="color:blue;">&gt;</span></p>
<p style="margin:0;"><span style="color:blue;">&nbsp; &lt;</span><span style="color:#a31515;">extensions</span><span style="color:blue;">&gt;</span></p>
<p style="margin:0;"><span style="color:blue;">&nbsp;&nbsp;&nbsp; &lt;</span><span style="color:#a31515;">behaviorExtensions</span><span style="color:blue;">&gt;</span></p>
<p style="margin:0;"><span style="color:blue;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;</span><span style="color:#a31515;">add</span><span style="color:blue;"> </span><span style="color:red;">name</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">ErrorHandlerBehavior</span>&#8220;</p>
<p style="margin:0;"><span style="color:blue;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color:red;">type</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">Wcf.Demo.Service.ErrorHandlerElement, Wcf.Demo.Service, </span></p>
<p style="margin:0;"><span style="color:blue;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Version=1.0.0.0, Culture=neutral, PublicKeyToken=null</span>&#8220;<span style="color:blue;"> /&gt;</span></p>
<p style="margin:0;"><span style="color:blue;">&nbsp;&nbsp;&nbsp; &lt;/</span><span style="color:#a31515;">behaviorExtensions</span><span style="color:blue;">&gt;</span></p>
<p style="margin:0;"><span style="color:blue;">&nbsp; &lt;/</span><span style="color:#a31515;">extensions</span><span style="color:blue;">&gt;</span></p>
<p style="margin:0;"><span style="color:blue;">&lt;/</span><span style="color:#a31515;">system.serviceModel</span><span style="color:blue;">&gt;</span></p>
</div>
<p>3. Ran integration unit tests that passed.<br />4. The service build was integrated into TeamBuild which happen to automatically update my AssemblyInfo version files.<br />5. As soon as a TeamBuild kicked off and the assembly version changed, I received the cryptic error.<br />6. I realised I had an incorrect version configured in the web.config so I replaced the fully qualified type with:</p>
<div style="font-size:10pt;background:white;color:black;font-family:courier new;">
<p style="margin:0;"><span style="color:blue;">&lt;</span><span style="color:#a31515;">behaviorExtensions</span><span style="color:blue;">&gt;</span></p>
<p style="margin:0;"><span style="color:blue;">&nbsp; &lt;</span><span style="color:#a31515;">add</span><span style="color:blue;"> </span><span style="color:red;">name</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">ErrorHandlerBehavior</span>&#8220;</p>
<p style="margin:0;"><span style="color:blue;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color:red;">type</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">Wcf.Demo.Service.ErrorHandlerElement, Wcf.Demo.Service</span>&#8220;<span style="color:blue;"> /&gt;</span></p>
<p style="margin:0;"><span style="color:blue;">&lt;/</span><span style="color:#a31515;">behaviorExtensions</span><span style="color:blue;">&gt;</span></p>
</div>
<p><!--EndFragment-->7. Ran tests and failed.</p>
<p>After attempting to diagnose the issue using tools such as Fusion Log Viewer, I checked Google (which I should have done first) and came across several posts also identifying the issue &#8211; including a closed and unresolved issue on Microsoft Connect. This is the <a href="http://dansen.wordpress.com/2008/04/09/code-analysis-ca1304-must-call-faultreasonfaultreasonstring-cultureinfo/">second time</a> round I have been to Microsoft Connect regarding a bug and it does not look like they will resolve this anytime soon.</p>
<h2>A Way Forward</h2>
<p>At this stage since there is no fix, a tedious workaround will have to be to get TeamBuild to modify the web.config and update the version number of the behaviour extension when applying a new version &#8211; which in my case is for every CI build triggered by a code checkin.</p>
<h2>Links</h2>
<ul>
<li>Mitch Denny has a good blog post/rant on this <a href="http://notgartner.wordpress.com/2006/12/19/rant-an-error-occured-creating-the-configuration-section-handler-for-systemservicemodelbehaviors/">here</a>.
<li>Microsoft Connect <a href="https://connect.microsoft.com/wcf/feedback/ViewFeedback.aspx?FeedbackID=216431&amp;wa=wsignin1.0">issue was raised but it was interestingly closed.</a>
<li>Keyvan also reports the problem <a href="http://nayyeri.net/blog/configuration-error-for-custom-behavior-extensions-in-wcf/">here</a>. </li>
</ul>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/dansen.wordpress.com/45/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/dansen.wordpress.com/45/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/dansen.wordpress.com/45/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/dansen.wordpress.com/45/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/dansen.wordpress.com/45/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/dansen.wordpress.com/45/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/dansen.wordpress.com/45/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/dansen.wordpress.com/45/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/dansen.wordpress.com/45/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/dansen.wordpress.com/45/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/dansen.wordpress.com/45/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/dansen.wordpress.com/45/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dansen.wordpress.com&blog=1210400&post=45&subd=dansen&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://dansen.wordpress.com/2008/04/23/exception-when-not-fully-qualifying-behaviour-extensions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/a2613ce2237e8235db55cafe47bd9ce1?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Dave</media:title>
		</media:content>

		<media:content url="http://dansen.files.wordpress.com/2008/04/image-thumb5.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>
	</item>
		<item>
		<title>Debugging WCF Clients and Services</title>
		<link>http://dansen.wordpress.com/2008/04/15/debugging-wcf-clients-and-services/</link>
		<comments>http://dansen.wordpress.com/2008/04/15/debugging-wcf-clients-and-services/#comments</comments>
		<pubDate>Tue, 15 Apr 2008 14:09:20 +0000</pubDate>
		<dc:creator>dansen</dc:creator>
				<category><![CDATA[Visual Studio]]></category>
		<category><![CDATA[WCF]]></category>

		<guid isPermaLink="false">http://dansen.wordpress.com/2008/04/15/debugging-wcf-clients-and-services/</guid>
		<description><![CDATA[Debugging WCF services from the client through to the service is much easier when attaching the debugger to either the service or both the client and the service.
In the past, I have debugged services by firing up the client application and manually attaching to the WCF Service Host &#8211; which in this case is the [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dansen.wordpress.com&blog=1210400&post=38&subd=dansen&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>Debugging WCF services from the client through to the service is much easier when attaching the debugger to either the service or both the client and the service.</p>
<p>In the past, I have debugged services by firing up the client application and manually attaching to the WCF Service Host &#8211; which in this case is the ASP.NET worker process. This process is quite cumbersome.</p>
<p>Visual Studio has feature that supports attaching to multiple projects when starting up.&nbsp; </p>
<p>In this example, I have a WCF service hosted in IIS which lives in the Wcf.Demo.IisServiceHost project. I also have a Wcf.Demo.TestHarness project which calls the service. I want to be able to attach the debugger to both the client and service by simply pressing F5.</p>
<h3>Steps</h3>
<p>1. Right click on the solution and select &#8220;<em><strong>Set StartUp Projects&#8230;</strong></em>&#8220;.</p>
<p><a href="http://dansen.files.wordpress.com/2008/04/image.png"><img style="border-width:0;" border="0" alt="image" src="http://dansen.files.wordpress.com/2008/04/image-thumb.png?w=296&#038;h=176" width="296" height="176"></a></p>
<p>2. In the StartUp tab, select &#8220;<strong><em>Multiple startup projects</em></strong>&#8221; and select the projects you want to start up. </p>
<p>3. On the Action of the selected projects, select &#8220;Start&#8221; to start with the debugger attached. </p>
<p><a href="http://dansen.files.wordpress.com/2008/04/image1.png"><img style="border-width:0;" border="0" alt="image" src="http://dansen.files.wordpress.com/2008/04/image-thumb1.png?w=455&#038;h=313" width="455" height="313"></a></p>
<p>If you now run (F5) the application, it will fire up both the Test Harness and a Internet browser to the service virtual directory. You can disable this.</p>
<p>4. Go to the Web project properties for the WCF web project and select &#8220;<strong><em>Don&#8217;t open a page</em></strong>&#8220;.</p>
<p>&nbsp;<a href="http://dansen.files.wordpress.com/2008/04/image2.png"><img style="border-width:0;" border="0" alt="image" src="http://dansen.files.wordpress.com/2008/04/image-thumb2.png?w=556&#038;h=312" width="556" height="312"></a></p>
<p>Now when you hit F5, the test harness will launch and the debugger will be attached to both processes.</p>
<p><a href="http://dansen.files.wordpress.com/2008/04/image3.png"><img style="border-width:0;" border="0" alt="image" src="http://dansen.files.wordpress.com/2008/04/image-thumb3.png?w=695&#038;h=344" width="695" height="344"></a></p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/dansen.wordpress.com/38/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/dansen.wordpress.com/38/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/dansen.wordpress.com/38/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/dansen.wordpress.com/38/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/dansen.wordpress.com/38/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/dansen.wordpress.com/38/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/dansen.wordpress.com/38/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/dansen.wordpress.com/38/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/dansen.wordpress.com/38/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/dansen.wordpress.com/38/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/dansen.wordpress.com/38/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/dansen.wordpress.com/38/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dansen.wordpress.com&blog=1210400&post=38&subd=dansen&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://dansen.wordpress.com/2008/04/15/debugging-wcf-clients-and-services/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/a2613ce2237e8235db55cafe47bd9ce1?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Dave</media:title>
		</media:content>

		<media:content url="http://dansen.files.wordpress.com/2008/04/image-thumb.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://dansen.files.wordpress.com/2008/04/image-thumb1.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://dansen.files.wordpress.com/2008/04/image-thumb2.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://dansen.files.wordpress.com/2008/04/image-thumb3.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>
	</item>
		<item>
		<title>Handling Fault Contracts</title>
		<link>http://dansen.wordpress.com/2008/04/15/handling-fault-contracts/</link>
		<comments>http://dansen.wordpress.com/2008/04/15/handling-fault-contracts/#comments</comments>
		<pubDate>Tue, 15 Apr 2008 13:35:59 +0000</pubDate>
		<dc:creator>dansen</dc:creator>
				<category><![CDATA[WCF]]></category>

		<guid isPermaLink="false">http://dansen.wordpress.com/2008/04/15/handling-fault-contracts/</guid>
		<description><![CDATA[Today I had an issue with catching Fault Contracts thrown from my WCF service. Every FaultException type raised by the service (excluding Communication Exceptions) were caught by the client as a FaultException instead of the expected FaultException&#60;T&#62;.
The issue was that I defined the FaultContractAttribute(type) on the operations in the Service Implementation class instead of on [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dansen.wordpress.com&blog=1210400&post=29&subd=dansen&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>Today I had an issue with catching Fault Contracts thrown from my WCF service. Every FaultException type raised by the service (excluding Communication Exceptions) were caught by the client as a <strong>FaultException</strong> instead of the expected <strong>FaultException&lt;T&gt;</strong>.</p>
<p>The issue was that I defined the <strong>FaultContractAttribute</strong>(type) on the operations in the Service Implementation class instead of on the Service Contract&#8217;s (Interface class) operations.</p>
<p>A service should always handle/shield exceptions and throw them as either a <strong>FaultException</strong> or a <strong>FaultException&lt;T&gt;</strong>. These types will not cause the channel to be faulted. I recommend defining Fault Contracts to convey information about the problem to client applications, and DO NOT provide the internal exception such as service stack traces. I&#8217;ll blog a post in the future on my recommendations service exception shielding patterns.</p>
<p>To catch Fault Contract types on the client, make sure that each Fault Contract type you require for that service operation, ensure that the FaultContractAttribute(Type) is specified.</p>
<h2>Service code</h2>
<h3>Error Handler </h3>
<p>The following code is implemented in the IErrorHandler class and is based on Rory&#8217;s post on <a href="http://neovolve.com/archive/2008/04/07/implementing-ierrorhandler.aspx">Implementing IErrorHandler</a> . In this example, all exceptions in my WCF service are changed to <strong>FaultException&lt;ServiceFault&gt;</strong>.</p>
<pre><span style="color:blue;">public void </span>ProvideFault(<span style="color:#2b91af;">Exception </span>error,
                         <span style="color:#2b91af;">MessageVersion </span>version,
                         <span style="color:blue;">ref </span><span style="color:#2b91af;">Message </span>fault)
{
    <span style="color:green;">// Return a 'ServiceFault' Fault Contract
    </span><span style="color:#2b91af;">ServiceFault </span>faultDetail = <span style="color:blue;">new </span><span style="color:#2b91af;">ServiceFault</span>(<span style="color:#a31515;">"An error occured"</span>);

    <span style="color:green;">// Construct FaultException with Fault Contract and FaultReason
    </span><span style="color:#2b91af;">FaultException</span>&lt;<span style="color:#2b91af;">ServiceFault</span>&gt; faultException =
        <span style="color:blue;">new </span><span style="color:#2b91af;">FaultException</span>&lt;<span style="color:#2b91af;">ServiceFault</span>&gt;(faultDetail,
        <span style="color:blue;">new </span><span style="color:#2b91af;">FaultReason</span>(<span style="color:#a31515;">"FaultReasonText"</span>));

    <span style="color:green;">// Construct MessageFault to return.
    </span><span style="color:#2b91af;">MessageFault </span>messageFault = faultException.CreateMessageFault();

    fault = <span style="color:#2b91af;">Message</span>.CreateMessage(version,
        messageFault,
        faultException.Action);
}</pre>
<p><a href="http://11011.net/software/vspaste"></a><a href="http://11011.net/software/vspaste"></a></p>
<h3>Service Contract</h3>
<p>Now this is where I went wrong. I did not decorate my service contract with the <strong>FaultContractAttribute</strong>. This attribute accepts a Type parameter which should be the type of the Fault Contract.</p>
<div style="font-size:10pt;background:white;color:black;font-family:courier new;">
<p style="margin:0;"><span style="color:blue;">namespace</span> DJ.Wcf.Demo.ServiceContracts</p>
<p style="margin:0;">{</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp; [<span style="color:#2b91af;">ServiceContract</span>(Name = <span style="color:#a31515;">"DJ.Wcf.Demo.DemoService"</span>, Namespace = <span style="color:#a31515;">"DJ.Wcf.Demo"</span>)]</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp; <span style="color:blue;">public</span> <span style="color:blue;">interface</span> <span style="color:#2b91af;">IDemoService</span></p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp; {</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [<span style="color:#2b91af;">OperationContract</span>]</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong>[<span style="color:#2b91af;">FaultContract</span>(<span style="color:blue;">typeof</span>(<span style="color:#2b91af;">ServiceFault</span>))]</strong></p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:#2b91af;">DemoData</span> GetDemoData(<span style="color:#2b91af;">Int32</span> demoId);</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp; }</p>
<p style="margin:0;">} </p>
</div>
<h2>Service Fault Contract code</h2>
<p>Here is a defined Fault Contract used by the service and client. Being a Data Contract, it could contain a collection of error messages e.g. a collection of validation error messages for a set of fields that were incorrectly set on the service operation.</p>
<pre><span style="color:blue;">using </span>System;
<span style="color:blue;">using </span>System.Runtime.Serialization;

<span style="color:blue;">namespace </span>Wcf.Demo.ServiceContracts
{
    [<span style="color:#2b91af;">DataContract</span>]
    <span style="color:blue;">public class </span><span style="color:#2b91af;">ServiceFault
    </span>{
        <span style="color:blue;">public </span>ServiceFault(<span style="color:#2b91af;">String </span>message)
        {
            Message = message;
        }

        [<span style="color:#2b91af;">DataMember</span>]
        <span style="color:blue;">public </span><span style="color:#2b91af;">String </span>Message
        {
            <span style="color:blue;">get</span>;
            <span style="color:blue;">set</span>;
        }
    }
}</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<h2>Client code</h2>
<p>The service calling code below shows several catch statements. When the service passes back a ServiceFault, it will be caught in: <font face="Courier New"><span style="color:blue;">catch</span> (<span style="color:#2b91af;">FaultException</span>&lt;<span style="color:#2b91af;">ServiceFault</span>&gt;</font>. All other <strong>FaultException</strong> and <strong>FaultException&lt;T&gt;</strong> types will be handled by: <font face="Courier New"><span style="color:blue;">catch</span> (<span style="color:#2b91af;">FaultException</span> fault)</font> block.</p>
<p>Unhandled service exceptions will be raised and caught in:&nbsp; <font face="Courier New"><span style="color:blue;">catch</span> (<span style="color:#2b91af;">CommunicationException</span> cex)</font> which will cause the WCF channel to fault, thus forcing the client to create a new channel.</p>
<pre><span style="color:blue;">try
</span>{
    <span style="color:blue;">using </span>(<span style="color:#2b91af;">ChannelFactory</span>&lt;<span style="color:#2b91af;">IDemoService</span>&gt; channel =
        <span style="color:blue;">new </span><span style="color:#2b91af;">ChannelFactory</span>&lt;<span style="color:#2b91af;">IDemoService</span>&gt;(<span style="color:#a31515;">"DemoService"</span>))
    {
        <span style="color:#2b91af;">IDemoService </span>service = channel.CreateChannel();
        channel.Open();

        <span style="color:blue;">return </span>service.GetPerson(42);
    }
}
<span style="color:blue;">catch </span>(<span style="color:#2b91af;">FaultException</span>&lt;<span style="color:#2b91af;">ServiceFault</span>&gt; serviceFault)
{
    <span style="color:#2b91af;">MessageBox</span>.Show(serviceFault.Message);
}
<span style="color:blue;">catch </span>(<span style="color:#2b91af;">FaultException </span>fault)
{
    <span style="color:#2b91af;">MessageBox</span>.Show(<span style="color:#a31515;">"Unknown Service Fault"</span>);
}
<span style="color:blue;">catch </span>(<span style="color:#2b91af;">TimeoutException </span>tex)
{
    <span style="color:#2b91af;">MessageBox</span>.Show(<span style="color:#a31515;">"Service call timed out. Please retry."</span>);
}
<span style="color:blue;">catch </span>(<span style="color:#2b91af;">CommunicationException </span>cex)
{
    <span style="color:#2b91af;">MessageBox</span>.Show(<span style="color:#a31515;">"Unknown Communications exception occured."</span>);
}</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/dansen.wordpress.com/29/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/dansen.wordpress.com/29/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/dansen.wordpress.com/29/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/dansen.wordpress.com/29/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/dansen.wordpress.com/29/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/dansen.wordpress.com/29/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/dansen.wordpress.com/29/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/dansen.wordpress.com/29/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/dansen.wordpress.com/29/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/dansen.wordpress.com/29/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/dansen.wordpress.com/29/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/dansen.wordpress.com/29/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dansen.wordpress.com&blog=1210400&post=29&subd=dansen&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://dansen.wordpress.com/2008/04/15/handling-fault-contracts/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/a2613ce2237e8235db55cafe47bd9ce1?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Dave</media:title>
		</media:content>
	</item>
		<item>
		<title>Controlling the serialized order of WCF Data Contract Members</title>
		<link>http://dansen.wordpress.com/2008/04/12/controlling-the-serialized-order-of-wcf-data-contract-members/</link>
		<comments>http://dansen.wordpress.com/2008/04/12/controlling-the-serialized-order-of-wcf-data-contract-members/#comments</comments>
		<pubDate>Sat, 12 Apr 2008 01:08:26 +0000</pubDate>
		<dc:creator>dansen</dc:creator>
				<category><![CDATA[WCF]]></category>

		<guid isPermaLink="false">http://dansen.wordpress.com/2008/04/12/controlling-the-serialized-order-of-wcf-data-contract-members/</guid>
		<description><![CDATA[There are situations where you wish to specify the order of fields when they are serialized. I believe that a good designed service should not specify the ordering of fields nor should a service consuming application expect it..
In some applications, it is useful to know the order in which data from the various data members [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dansen.wordpress.com&blog=1210400&post=25&subd=dansen&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>There are situations where you wish to specify the order of fields when they are serialized. I believe that a good designed service should not specify the ordering of fields nor should a service consuming application expect it..</p>
<blockquote><p>In some applications, it is useful to know the order in which data from the various data members is sent or is expected to be received (such as the order in which data appears in the serialized XML). Sometimes it may be necessary to change this order&#8230;..</p>
</blockquote>
<p>Recently, I started using ReSharper (now I&#8217;m hooked). I used it to reformat my code based on a style which alphabetically reordered all the properties in the data contracts. The changes were checked in, unit tested, integrated united tested and deployed to Test. The changes I made broke the client application as the application was relying on the serialised Xml to be in a certain order. In my opinion this is definately <strong>not </strong>service design.</p>
<p>I was told about the <a href="http://msdn2.microsoft.com/en-us/library/ms729813.aspx">DataMemberAttribute.Order</a> property by <a href="http://neovolve.com/default.aspx">Rory</a> and reluctantly added the <a href="http://msdn2.microsoft.com/en-us/library/ms729813.aspx">Order</a> property to every data contract member. This change still broke the client application. The reason was that the client application was using the XmlSerializer which does not change its behvaior with the new <a href="http://msdn2.microsoft.com/en-us/library/system.runtime.serialization.datamemberattribute.aspx">DataMember</a> attributes.</p>
<p>The solution was to serialise a Data Contract using the <a href="http://msdn2.microsoft.com/en-us/library/system.runtime.serialization.datacontractserializer.aspx">DataContractSerializer</a>. I still consider this to be a dirty solution because the ordering of the properties must always be the same, otherwise the consumer may break.</p>
<h3>Data Contract</h3>
<p>The following data contract lists fields in alphabetical order. The <a href="http://msdn2.microsoft.com/en-us/library/ms729813.aspx">Order</a> attribute forces the <a href="http://msdn2.microsoft.com/en-us/library/system.runtime.serialization.datacontractserializer.aspx">DataContractSerializer</a> to output elements in the requested order.</p>
<div style="font-size:10pt;background:white;color:black;font-family:courier new;">
<p style="margin:0;">&nbsp;&nbsp;&nbsp; [<span style="color:#2b91af;">DataContract</span>]</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp; <span style="color:blue;">public</span> <span style="color:blue;">class</span> <span style="color:#2b91af;">Person</span></p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp; {</p>
<p style="margin:0;"><strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [<span style="color:#2b91af;">DataMember</span>(Order = 2)]</strong></p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">public</span> <span style="color:#2b91af;">Int32</span> Age { <span style="color:blue;">get</span>; <span style="color:blue;">set</span>; }</p>
<p style="margin:0;">&nbsp;</p>
<p><strong>
<p style="margin:0;"><strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [<span style="color:#2b91af;">DataMember</span>(Order = 0)]</strong></p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">public</span> <span style="color:#2b91af;">String</span> FirstName { <span style="color:blue;">get</span>; <span style="color:blue;">set</span>; }</p>
<p style="margin:0;">&nbsp;</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [<span style="color:#2b91af;">DataMember</span>(Order = 1)]</p>
<p></strong>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">public</span> <span style="color:#2b91af;">String</span> LastName { <span style="color:blue;">get</span>; <span style="color:blue;">set</span>; }</p>
<p style="margin:0;">&nbsp;</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp; }</p>
<p style="margin:0;">&nbsp;</p>
</div>
<h3>Serializing using the XmlSerializer</h3>
<div style="font-size:8pt;background:white;color:black;font-family:courier new;">
<p style="margin:0;"><font size="2">&nbsp;&nbsp;&nbsp; <span style="color:#2b91af;">Person</span> person = <span style="color:blue;">new</span> <span style="color:#2b91af;">Person</span> { Age = 25, FirstName = <span style="color:#a31515;">&#8220;Bob&#8221;</span>, LastName = <span style="color:#a31515;">&#8220;Jane&#8221;</span> };</font></p>
<p style="margin:0;"><font size="2">&nbsp;&nbsp;&nbsp; <span style="color:blue;">using</span> (<span style="color:#2b91af;">FileStream</span> writer = <span style="color:blue;">new</span> <span style="color:#2b91af;">FileStream</span>(<span style="color:#a31515;">&#8220;Person.xml&#8221;</span>, <span style="color:#2b91af;">FileMode</span>.Create))</font></p>
<p style="margin:0;"><font size="2">&nbsp;&nbsp;&nbsp; {</font></p>
<p style="margin:0;"><font size="2">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:#2b91af;">XmlSerializer</span> xmlSerializer = <span style="color:blue;">new</span> <span style="color:#2b91af;">XmlSerializer</span>(<span style="color:blue;">typeof</span>(<span style="color:#2b91af;">Person</span>));</font></p>
<p style="margin:0;"><font size="2">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; xmlSerializer.Serialize(writer, person);</font></p>
<p style="margin:0;"><font size="2">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; writer.Close();</font></p>
<p style="margin:0;"><font size="2">&nbsp;&nbsp;&nbsp; }</font></p>
</div>
<p>The XML is written in the order the fields are defined in the class.</p>
<div style="font-size:8pt;background:white;color:black;font-family:courier new;"><font size="2"><span style="color:blue;">
<div style="font-size:10pt;background:white;color:black;font-family:courier new;">
<p style="margin:0;"><span style="color:blue;">&nbsp; &lt;?</span><span style="color:#a31515;">xml</span><span style="color:blue;"> </span><span style="color:red;">version</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">1.0</span>&#8220;<span style="color:blue;">?&gt;</span></p>
<p style="margin:0;"><span style="color:blue;">&nbsp; &lt;</span><span style="color:#a31515;">Person</span><span style="color:blue;"> </span><span style="color:red;">xmlns:xsi</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">http://www.w3.org/2001/XMLSchema-instance</span>&#8220;<span style="color:blue;"> </span></p>
<p style="margin:0;"><span style="color:blue;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color:red;">xmlns:xsd</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">http://www.w3.org/2001/XMLSchema</span>&#8220;<span style="color:blue;">&gt;</span></p>
<p style="margin:0;"><span style="color:blue;">&nbsp;&nbsp;&nbsp; &lt;</span><span style="color:#a31515;">Age</span><span style="color:blue;">&gt;</span>25<span style="color:blue;">&lt;/</span><span style="color:#a31515;">Age</span><span style="color:blue;">&gt;</span></p>
<p style="margin:0;"><span style="color:blue;">&nbsp;&nbsp;&nbsp; &lt;</span><span style="color:#a31515;">LastName</span><span style="color:blue;">&gt;</span>Jane<span style="color:blue;">&lt;/</span><span style="color:#a31515;">LastName</span><span style="color:blue;">&gt;</span></p>
<p style="margin:0;"><span style="color:blue;">&nbsp;&nbsp;&nbsp; &lt;</span><span style="color:#a31515;">FirstName</span><span style="color:blue;">&gt;</span>Bob<span style="color:blue;">&lt;/</span><span style="color:#a31515;">FirstName</span><span style="color:blue;">&gt;</span></p>
<p style="margin:0;"><span style="color:blue;">&nbsp; &lt;/</span><span style="color:#a31515;">Person</span><span style="color:blue;">&gt;</span></span></font>&nbsp;</p>
</div>
</div>
<h3><font size="2"><font size="2"><br />
<h3>Serializing using the DataContractSerializer</h3>
<div style="font-size:8pt;background:white;color:black;font-family:courier new;">
<p style="margin:0;"><font size="2">&nbsp;&nbsp;&nbsp; <span style="color:#2b91af;">Person</span> person = <span style="color:blue;">new</span> <span style="color:#2b91af;">Person</span> { Age = 25, FirstName = <span style="color:#a31515;">&#8220;Bob&#8221;</span>, LastName = <span style="color:#a31515;">&#8220;Jane&#8221;</span> };</font></p>
<p style="margin:0;"><font size="2">&nbsp;&nbsp;&nbsp; <span style="color:blue;">using</span> (<span style="color:#2b91af;">FileStream</span> writer = <span style="color:blue;">new</span> <span style="color:#2b91af;">FileStream</span>(<span style="color:#a31515;">&#8220;Person.xml&#8221;</span>, <span style="color:#2b91af;">FileMode</span>.Create))</font></p>
<p style="margin:0;"><font size="2">&nbsp;&nbsp;&nbsp; {</font></p>
<p style="margin:0;"><font size="2">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:#2b91af;">DataContractSerializer</span> contractSerializer = <span style="color:blue;">new</span> <span style="color:#2b91af;">DataContractSerializer</span>(<span style="color:blue;">typeof</span> (<span style="color:#2b91af;">Person</span>));</font></p>
<p style="margin:0;"><font size="2">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; contractSerializer.WriteObject(writer, person);</font></p>
<p style="margin:0;"><font size="2">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; writer.Close();</font></p>
<p style="margin:0;"><font size="2">&nbsp;&nbsp;&nbsp; }</font></p>
<p style="margin:0;"></font></font>&nbsp;</p>
</div>
</h3>
<h4>
<p><font size="2">The XML is written in the expected order defined by the Order property, even though the data contract defines in another order.</font></p>
</h4>
<div style="font-size:8pt;background:white;color:black;font-family:courier new;"><font size="2"><span style="color:blue;">
<div style="font-size:10pt;background:white;color:black;font-family:courier new;">
<p style="margin:0;"><span style="color:blue;">&nbsp; &lt;</span><span style="color:#a31515;">Person</span><span style="color:blue;"> </span><span style="color:red;">xmlns</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">http://schemas.datacontract.org/2004/07/Wcf.Demo.ServiceContracts</span>&#8220;<span style="color:blue;"> </span></p>
<p style="margin:0;"><span style="color:blue;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color:red;">xmlns:i</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">http://www.w3.org/2001/XMLSchema-instance</span>&#8220;<span style="color:blue;">&gt;</span></p>
<p style="margin:0;"><span style="color:blue;">&nbsp;&nbsp;&nbsp; &lt;</span><span style="color:#a31515;">FirstName</span><span style="color:blue;">&gt;</span>Bob<span style="color:blue;">&lt;/</span><span style="color:#a31515;">FirstName</span><span style="color:blue;">&gt;</span></p>
<p style="margin:0;"><span style="color:blue;">&nbsp;&nbsp;&nbsp; &lt;</span><span style="color:#a31515;">LastName</span><span style="color:blue;">&gt;</span>Jane<span style="color:blue;">&lt;/</span><span style="color:#a31515;">LastName</span><span style="color:blue;">&gt;</span></p>
<p style="margin:0;"><span style="color:blue;">&nbsp;&nbsp;&nbsp; &lt;</span><span style="color:#a31515;">Age</span><span style="color:blue;">&gt;</span>25<span style="color:blue;">&lt;/</span><span style="color:#a31515;">Age</span><span style="color:blue;">&gt;</span></p>
<p style="margin:0;"><span style="color:blue;">&nbsp; &lt;/</span><span style="color:#a31515;">Person</span><span style="color:blue;">&gt;</span></span></font></p>
</div>
</div>
<p>&nbsp;</p>
<p>So if you require a specific order to be xml serialized, make sure you use the DataContractSerializer.</p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/dansen.wordpress.com/25/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/dansen.wordpress.com/25/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/dansen.wordpress.com/25/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/dansen.wordpress.com/25/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/dansen.wordpress.com/25/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/dansen.wordpress.com/25/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/dansen.wordpress.com/25/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/dansen.wordpress.com/25/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/dansen.wordpress.com/25/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/dansen.wordpress.com/25/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/dansen.wordpress.com/25/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/dansen.wordpress.com/25/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dansen.wordpress.com&blog=1210400&post=25&subd=dansen&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://dansen.wordpress.com/2008/04/12/controlling-the-serialized-order-of-wcf-data-contract-members/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/a2613ce2237e8235db55cafe47bd9ce1?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Dave</media:title>
		</media:content>
	</item>
		<item>
		<title>CA1304 &#8211; Must specify CultureInfo when using FaultReason</title>
		<link>http://dansen.wordpress.com/2008/04/09/code-analysis-ca1304-must-specify-cultureinfo-when-using-faultreason/</link>
		<comments>http://dansen.wordpress.com/2008/04/09/code-analysis-ca1304-must-specify-cultureinfo-when-using-faultreason/#comments</comments>
		<pubDate>Wed, 09 Apr 2008 12:36:31 +0000</pubDate>
		<dc:creator>dansen</dc:creator>
				<category><![CDATA[Code Analysis]]></category>
		<category><![CDATA[Visual Studio]]></category>
		<category><![CDATA[WCF]]></category>

		<guid isPermaLink="false">http://dansen.wordpress.com/2008/04/09/code-analysis-ca1304-must-call-faultreasonfaultreasonstring-cultureinfo/</guid>
		<description><![CDATA[Today I came across the following issue when running Code Analysis over my WCF Error Shielding solution.

Because I have &#8220;Treat Warnings as Errors&#8221; enabled for all projects, Code Analysis fails.
Rory Primrose has recently blogged a very nice Error Handling solution which have implemented in a WCF service. As soon as I implemented the solution and [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dansen.wordpress.com&blog=1210400&post=24&subd=dansen&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>Today I came across the following issue when running Code Analysis over my WCF Error Shielding solution.</p>
<p><a href="http://dansen.files.wordpress.com/2008/04/caerror.jpg"><img style="border-width:0;" src="http://dansen.files.wordpress.com/2008/04/caerror-thumb.jpg?w=822&#038;h=100" border="0" alt="CAError" width="822" height="100" /></a></p>
<p>Because I have &#8220;Treat Warnings as Errors&#8221; enabled for all projects, Code Analysis fails.</p>
<p><a href="http://neovolve.com/archive/2008/04/07/implementing-ierrorhandler.aspx">Rory Primrose</a> has recently blogged a very nice <a href="http://neovolve.com/archive/2008/04/07/implementing-ierrorhandler.aspx">Error Handling</a> solution which have implemented in a WCF service. As soon as I implemented the solution and specified to return a custom Fault Contract, WCF returned an exception indicating that the &#8220;<a href="http://msdn2.microsoft.com/en-us/library/system.servicemodel.faultreason.aspx">FaultReason</a>&#8221; was not provided. So I constructed a new <a href="http://msdn2.microsoft.com/en-us/library/system.servicemodel.faultreason.aspx">FaultReason</a>, and passed it in to the FaultException(Fault, FaultReason).</p>
<blockquote><p>The <span class="selflink">FaultReason</span> class contains a set of <a id="ctl00_rs1_mainContentContainer_ctl22" href="http://msdn2.microsoft.com/en-us/library/system.servicemodel.faultreasontext.aspx"><span style="color:#0066cc;">System.ServiceModel<span class="cs">.</span><span class="vb">.</span><span class="cpp">::</span><span class="nu">.</span>FaultReasonText</span></a> objects, each of which contains a description of the fault in a specific language.</p></blockquote>
<p><strong>Sample</strong></p>
<p><!-- {\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue255;\red255\green255\blue255;\red0\green0\blue0;\red43\green145\blue175;\red0\green128\blue0;\red163\green21\blue21;}??\fs20 \cf1 public\cf0  \cf1 void\cf0  ProvideFault(\cf4 Exception\cf0  error, \cf4 MessageVersion\cf0  version, \cf1 ref\cf0  \cf4 Message\cf0  fault)\par ??\{\par ??    \cf5 // Return a 'ServiceFault' Fault Contract\par ??\cf0     \cf4 ServiceFault\cf0  faultDetail = \cf1 new\cf0  \cf4 ServiceFault\cf0 (\cf6 "An error occured"\cf0 );\par ??\par ??    \cf5 // Construct FaultException with FaultContract and FaultReason\par ??\cf0     \cf4 FaultException\cf0 &amp;lt;\cf4 ServiceFault\cf0 &amp;gt; faultException = \par ??        \cf1 new\cf0  \cf4 FaultException\cf0 &amp;lt;\cf4 ServiceFault\cf0 &amp;gt;(faultDetail, \cf1 new\cf0  \cf4 FaultReason\cf0 (\cf6 "FaultReasonText"\cf0 ));\par ??\par ??    \cf4 MessageFault\cf0  messageFault = faultException.CreateMessageFault();\par ??\par ??    fault = \cf4 Message\cf0 .CreateMessage(version, messageFault, faultException.Action);\par ??\}} --></p>
<div style="background:white none repeat scroll 0;font-family:Courier New;font-size:8pt;color:black;">
<p style="margin:0;"><span style="color:blue;">public</span> <span style="color:blue;">void</span> ProvideFault(<span style="color:#2b91af;">Exception</span> error, <span style="color:#2b91af;">MessageVersion</span> version, <span style="color:blue;">ref</span> <span style="color:#2b91af;">Message</span> fault)</p>
<p style="margin:0;">{</p>
<p style="margin:0;"><span style="color:green;"> // Return a &#8216;ServiceFault&#8217; Fault Contract</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> ServiceFault</span> faultDetail = <span style="color:blue;">new</span> <span style="color:#2b91af;">ServiceFault</span>(<span style="color:#a31515;">&#8220;An error occured&#8221;</span>);</p>
<p style="margin:0;">
<p style="margin:0;"><span style="color:green;"><br />
// Construct FaultException with FaultContract and FaultReason</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> FaultException</span>&lt;<span style="color:#2b91af;">ServiceFault</span>&gt; faultException =</p>
<p style="margin:0;"><span style="color:blue;"> new</span> <span style="color:#2b91af;">FaultException</span>&lt;<span style="color:#2b91af;">ServiceFault</span>&gt;(faultDetail, <span style="color:blue;">new</span> <span style="color:#2b91af;">FaultReason</span>(<span style="color:#a31515;">&#8220;FaultReasonText&#8221;</span>));</p>
<p style="margin:0;">
<p style="margin:0;"><span style="color:#2b91af;"><br />
MessageFault</span> messageFault = faultException.CreateMessageFault();</p>
<p style="margin:0;">
<p>fault = <span style="color:#2b91af;">Message</span>.CreateMessage(version, messageFault, faultException.Action);</p>
<p style="margin:0;">}</p>
</div>
<p><!--EndFragment--></p>
<p>I compiled the code in Debug which was successful. I then switched on Code Analysis and it raised CA1304 : Microsoft.Globalization. The Code Analysis rule made sense to ensure I was using CultureInfo for the reason text.</p>
<p>However, the FaultReason does not provide any <strong>&#8216;public</strong>&#8216; constructor overrides to accept the CultureInfo. It does however provide an internal FaultReason(string text, CultureInfo cultureInfo) constructor.</p>
<p><a href="http://dansen.files.wordpress.com/2008/04/faultreason.jpg"><img style="border-width:0;" src="http://dansen.files.wordpress.com/2008/04/faultreason-thumb.jpg?w=678&#038;h=307" border="0" alt="FaultReason" width="678" height="307" /></a></p>
<p>After the help of <a href="http://neovolve.com/default.aspx">Rory</a> who confirmed with <a href="http://blogs.msdn.com/fxcop/">David Kean</a> from the <a href="http://blogs.msdn.com/fxcop/">Code Analysis Team</a> that this was a bug, I submitted feedback to <span style="font-size:11pt;font-family:'Calibri','sans-serif';color:#1f497d;"><a href="http://connect.microsoft.com/VisualStudio"><span style="color:#0000ff;">Microsoft Connect</span></a>.</span></p>
<p>You can keep track of this bug here: https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=337431&amp;wa=wsignin1.0</p>
<p>In the meantime, I have suppressed this CA rule in my GlobalSupressions.cs for the project.</p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/dansen.wordpress.com/24/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/dansen.wordpress.com/24/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/dansen.wordpress.com/24/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/dansen.wordpress.com/24/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/dansen.wordpress.com/24/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/dansen.wordpress.com/24/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/dansen.wordpress.com/24/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/dansen.wordpress.com/24/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/dansen.wordpress.com/24/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/dansen.wordpress.com/24/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/dansen.wordpress.com/24/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/dansen.wordpress.com/24/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dansen.wordpress.com&blog=1210400&post=24&subd=dansen&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://dansen.wordpress.com/2008/04/09/code-analysis-ca1304-must-specify-cultureinfo-when-using-faultreason/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/a2613ce2237e8235db55cafe47bd9ce1?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Dave</media:title>
		</media:content>

		<media:content url="http://dansen.files.wordpress.com/2008/04/caerror-thumb.jpg" medium="image">
			<media:title type="html">CAError</media:title>
		</media:content>

		<media:content url="http://dansen.files.wordpress.com/2008/04/faultreason-thumb.jpg" medium="image">
			<media:title type="html">FaultReason</media:title>
		</media:content>
	</item>
	</channel>
</rss>