<?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>chillijam.co.uk &#187; WCF</title>
	<atom:link href="http://chillijam.co.uk/tag/wcf/feed/" rel="self" type="application/rss+xml" />
	<link>http://chillijam.co.uk</link>
	<description></description>
	<lastBuildDate>Thu, 28 Jan 2021 10:32:11 +0000</lastBuildDate>
	<language>en-US</language>
		<sy:updatePeriod>hourly</sy:updatePeriod>
		<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=4.0.38</generator>
	<item>
		<title>Fault handling in WCF services</title>
		<link>http://chillijam.co.uk/2008/09/18/fault-handling-in-wcf-services/</link>
		<comments>http://chillijam.co.uk/2008/09/18/fault-handling-in-wcf-services/#comments</comments>
		<pubDate>Thu, 18 Sep 2008 11:08:42 +0000</pubDate>
		<dc:creator><![CDATA[Marc]]></dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Fault handling]]></category>
		<category><![CDATA[servicemodel]]></category>
		<category><![CDATA[WCF]]></category>

		<guid isPermaLink="false">http://chillijam.co.uk/?p=40</guid>
		<description><![CDATA[It is oprobably very well known to most WCF developers, but there is a methodology previously unknown to me for handling exceptions in a WCF service, and handling them gracefully.  Hopefully this post will give enough of an overview to the required steps to communicate fault conditions to the client to allow you to handle &#8230; <a href="http://chillijam.co.uk/2008/09/18/fault-handling-in-wcf-services/" class="more-link">Continue reading <span class="screen-reader-text">Fault handling in WCF services</span> <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>It is oprobably very well known to most WCF developers, but there is a methodology previously unknown to me for handling exceptions in a WCF service, and handling them gracefully.  Hopefully this post will give enough of an overview to the required steps to communicate fault conditions to the client to allow you to handle them in your code.</p>
<p>There are a few steps required in order to create the fault data, but they are all quite simple.</p>
<p>First, you need to create a DataContract object to hold the fault details.  Mine looks like this.</p>
<pre class="brush: csharp; title: ; notranslate">
using System;
using System.Runtime.Serialization;
 
namespace MyOrganisation.MyService
{
    [DataContract(Namespace = &quot;http://schemas.MyOrganisation.com/CentreService/2008/08&quot;)]
    public class UnexpectedServiceFault
    {
        private int errorCode = -1;
        private string errorMessage = &quot;UNDEFINED&quot;;
        private string additionalData = String.Empty;
 
        [DataMember]
        public int ErrorCode
        {
            get { return errorCode; }
            set { errorCode = value; }
        }
 
        [DataMember]
        public string ErrorMessage
        {
            get { return errorMessage; }
            set { errorMessage = value; }
        }
 
        [DataMember]
        public string AdditionalData
        {
            get { return additionalData; }
            set { additionalData = value; }
        }
    }
}
</pre>
<p>That&#8217;s the object that will hold the data for presentation to the client.  Now we&#8217;ll examine how to use it.</p>
<p>In the interface for your service class, you&#8217;ll need to decorate the methods for which you wish to provide fault handling.  This is a simple case of adding the following line to the code.</p>
<pre class="brush: csharp; title: ; notranslate">
[FaultContract(typeof(UnexpectedServiceFault))]
</pre>
<p>so, and example method signature would look like this.</p>
<pre class="brush: csharp; title: ; notranslate">
/// &lt;summary&gt;
/// A dummy method to demonstrate fault handling
/// &lt;/summary&gt;
/// &lt;param name=&quot;aParam&quot;&gt;A parameter to the method&lt;/param&gt;
[FaultContract(typeof(UnexpectedServiceFault))]
[OperationContract]
void MyMethod(string aParam);
</pre>
<p>This tells the compiler to generate the WSDDL required to support fault handling for this method.  Now, we need to implement this in the actual service code.</p>
<pre class="brush: csharp; title: ; notranslate">
/// &lt;summary&gt;
/// A dummy method to demonstrate fault handling
/// &lt;/summary&gt;
/// &lt;param name=&quot;aParam&quot;&gt;A parameter to the method&lt;/param&gt;
[FaultContract(typeof(UnexpectedServiceFault))]
[OperationContract]
void MyMethod(string aParam);
</pre>
<p>This tells the compiler to generate the WSDDL required to support fault handling for this method.  Now, we need to implement this in the actual service code.</p>
<pre class="brush: csharp; title: ; notranslate">
/// &lt;summary&gt;
/// A dummy method to demonstrate fault handling
/// &lt;/summary&gt;
/// &lt;param name=&quot;aParam&quot;&gt;A parameter to the method&lt;/param&gt;
[FaultContract(typeof(UnexpectedServiceFault))]
[OperationContract]
public void MyMethod(string aParam)
{
  try
  {
    
  }
  catch(Exception ex)
  {
    UnexpectedServiceFault fault = new UnexpectedServiceFault();
    fault.ErrorMessage = ex.Message;
    fault.ErrorCode = 101;  // an error code to identify the fault location
    fault.AdditionalData = ex.StackTrace;
    FaultException&lt;UnexpectedServiceFault&gt; faultException = new FaultException&lt;UnexpectedServiceFault&gt;(fault);
    throw faultException;
  }
}
</pre>
<p>So, in the catch block, we created a new object of type UnexpectedServiceFault (as we defined in the first step), populated it&#8217;s data, and then wrapped it in the FaultException<T> type provided by System.ServiceModel.  It is this FaultException<T> that we throw to the client.</p>
<p>In order to enable passing the fault data back to the client, we need to configure the host (in my case IIS) to pass exception details back to the caller.  In my web config, I created a new ServiceBehaviour element containing a serviceDebug element.  I then set the value of IncludeExceptionDetailsInFaults to true, and associated the behaviour with the service.</p>
<pre class="brush: xml; title: ; notranslate">
&lt;?xml version=&quot;1.0&quot;?&gt;

&lt;configuration&gt;
  &lt;system.serviceModel&gt;
    &lt;services&gt;
      &lt;service name=&quot;MyOrganisation.MyService&quot; 
           behaviorConfiguration=&quot;MyServiceBehaviour&quot;&gt;
        &lt;endpoint contract=&quot;MyOrganisation.MyService&quot; binding=&quot;wsHttpBinding&quot;/&gt;
      &lt;/service&gt;
    &lt;/services&gt;
    &lt;behaviors&gt;
      &lt;serviceBehaviors&gt;
        &lt;behavior name=&quot;MyServiceBehaviour&quot; &gt;
          &lt;serviceMetadata httpGetEnabled=&quot;true&quot; /&gt;
          &lt;serviceDebug httpHelpPageEnabled=&quot;true&quot; includeExceptionDetailInFaults=&quot;true&quot; /&gt;
        &lt;/behavior&gt;
      &lt;/serviceBehaviors&gt;
    &lt;/behaviors&gt;
  &lt;/system.serviceModel&gt;
</pre>
<p>Now all that remains is to implement the handling at the client.  Once you&#8217;ve updated the service reference information, you can catch excpetions of type System.ServiceModel.FaultException<UnexpectedServiceFault> and handle them as required.  In a simple case like so :</p>
<pre class="brush: csharp; title: ; notranslate">
catch(FaultException&lt;UnexpectedServiceFault&gt; ex)
{
  Console.WriteLine(&quot;An error occurred :: &quot; + ex.Detail.ErrorMessage);
}
</pre>
<p>Quite a simple process, but it can be a bit fiddly.  If you have problems, I&#8217;d suggest going back and re-checking each step of this guide.  I&#8217;m going to do the same in the next day or two, just to make sure I haven&#8217;t missed anything.  <img src="http://chillijam.co.uk/wp-includes/images/smilies/icon_smile.gif" alt=":)" class="wp-smiley" /></p>
]]></content:encoded>
			<wfw:commentRss>http://chillijam.co.uk/2008/09/18/fault-handling-in-wcf-services/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>WCF Services &#8211; a compatibility hack</title>
		<link>http://chillijam.co.uk/2008/08/20/wcf-services-a-compatibility-hack/</link>
		<comments>http://chillijam.co.uk/2008/08/20/wcf-services-a-compatibility-hack/#comments</comments>
		<pubDate>Wed, 20 Aug 2008 15:23:32 +0000</pubDate>
		<dc:creator><![CDATA[Marc]]></dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[AMSX]]></category>
		<category><![CDATA[Compatibility]]></category>
		<category><![CDATA[WCF]]></category>

		<guid isPermaLink="false">http://chillijam.co.uk/?p=27</guid>
		<description><![CDATA[I've recently had to start developing a set of WCF services for my company to use.  All good stuff, you might say - and tyou'd be right.  For the most part.

The area in which things get "interesting" is around the requirement for backwards-compatibility with some existing .Net 1.1 applications that consume traditional web services.]]></description>
				<content:encoded><![CDATA[<p>I&#8217;ve recently had to start developing a set of WCF services for my company to use.  All good stuff, you might say &#8211; and tyou&#8217;d be right.  For the most part.</p>
<p>The area in which things get &#8220;interesting&#8221; is around the requirement for backwards-compatibility with some existing .Net 1.1 applications that consume traditional web services.  These applications are horrendously complex, and the fewer changes to the code base we have to make, the better.  With this in mind, we created the WCF services to mirror the old web services as closely as possible and that&#8217;s where the fun began.</p>
<p>The issue that set me on a journey of discovery and hackery was around a method that returned a simple boolean type.</p>
<p>public bool NameIsInList(string name)</p>
<p>{</p>
<p>// implementation</p>
<p>return true;</p>
<p>}</p>
<p>Nothing fancy there.  However, exposing the service using basicHttpBinding had an unfortunate side effect.  The method signature was refactored to :</p>
<p>public void NameIsInList(string name, out bool NameIsInListResult, out bool NameIsInListResultSpecified);</p>
<p>Less than ideal.</p>
<p>No matter what I tried (including contacting <a title="Amit" href="http://www.desigeek.com" target="_blank">Amit</a>) I couldn&#8217;t make the down-level compatible web service return a simple boolean result.  Given how much effort we would have to go to to update the calling methods in the legacy systems, I had to find another way forward.</p>
<p>In the end, the solution was extremely simple, if a little hacky.  Create an ASMX web service that handled the calls to the WCF service, and presents the legacy apps with the data they expect.</p>
<p>[WebMethod]<br />
public bool NameIsInList(string name)<br />
{<br />
using (MyWcfService.ServiceClient WcfClient = new MyWcfService.ServiceClient(&#8220;WSHttpBinding_MyService&#8221;))<br />
{<br />
return WcfClient.NameIsInList(name);<br />
}<br />
}</p>
<p>Not the prettiest solution, but it works.</p>
<p>There are a few extra benefits that I wasn&#8217;t expecting from this approach, too.  I&#8217;ll post about those tomorrow.</p>
]]></content:encoded>
			<wfw:commentRss>http://chillijam.co.uk/2008/08/20/wcf-services-a-compatibility-hack/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
