Exception Handling in WCF using Fault Contract

Introduction

 
In all managed applications, processing errors are represented by Exception objects. In WCF applications, service methods communicate processing error information using SOAP fault messages. SOAP faults are message types that are included in the metadata for a service operation and, therefore, create a fault contract that clients can use to make their operation more interactive. In addition, because SOAP faults are expressed to clients in XML form, they are highly interoperable.
 

Exception Handling in WCF using Fault Contract 

 
To be more concrete, let us start with an example:
 
Create an empty solution in VS2005 called "IndigoException". Add one project called "IndigoService". Next add a reference to System.ServiceModel and then add an interface.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Text;  
  4. using System.ServiceModel;  
  5. using System.Runtime.Serialization;  
  6. namespace Indigo  
  7. {  
  8.     [ServiceContract()]  
  9.     public interface IService  
  10.     {  
  11.         [OperationContract]  
  12.         [FaultContract(typeof(MyFaultException))]  
  13.         string GetMessage();  
  14.     }  
  15. }  
Add one more class as follows:
  1. using System;  
  2. using System.Text;  
  3. using System.Runtime.Serialization;  
  4. namespace Indigo  
  5. {  
  6.     [DataContract]  
  7.     public class MyFaultException  
  8.     {  
  9.         private string _reason;  
  10.         [DataMember]  
  11.         public string Reason  
  12.         {  
  13.             get { return _reason; }  
  14.             set { _reason = value; }  
  15.         }  
  16.     }  
  17. }  
Add one class "Service" and a reference to System.ServiceModel
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Text;  
  4. using System.ServiceModel;  
  5. namespace Indigo {  
  6.  public class Service: IService {  
  7.   #  
  8.   region IService Members  
  9.   public string GetMessage() {  
  10.    try  
  11.    {  
  12.     int i, j, k;  
  13.     j = 1;  
  14.     k = 0;  
  15.     i = j / k;  
  16.    } catch (Exception exp)  
  17.    {  
  18.     MyFaultException theFault = new MyFaultException();  
  19.     theFault.Reason = "Some Error " + exp.Message.ToString();  
  20.     throw new FaultException < MyFaultException > (theFault);  
  21.    }  
  22.    return "No Error";  
  23.   }  
  24.   # endregion  
  25.  }  
  26. }  
Build the project.
 
Now add another project "Host" and add reference to "IndigoService" project and to System.ServiceModel
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Text;  
  4. using System.ServiceModel;  
  5. namespace Indigo  
  6. {  
  7.     class IndigoHost  
  8.     {  
  9.         internal static ServiceHost myServiceHost = null;  
  10.         static void Main(string[] args)  
  11.         {  
  12.             StartService();  
  13.             Console.WriteLine("Service at your service");  
  14.             Console.ReadKey();  
  15.             StopService();  
  16.         }  
  17.         internal static void StartService()  
  18.         {  
  19.             myServiceHost = new ServiceHost(typeof(Indigo.Service));  
  20.             myServiceHost.Open();  
  21.         }  
  22.         internal static void StopService()  
  23.         {  
  24.             //Call StopService from your shutdown logic (i.e. dispose method)  
  25.             if (myServiceHost.State != CommunicationState.Closed)  
  26.                 myServiceHost.Close();  
  27.         }  
  28.     }  
  29. }  
Add app.config with the following XML:
  1. <?xml version="1.0" encoding="utf-8" ?>  
  2. <configuration>  
  3.   <system.serviceModel>  
  4.     <services>  
  5.       <service behaviorConfiguration="MyServiceTypeBehaviors" name="Indigo.Service">  
  6.         <endpoint address="Service"  binding="wsHttpBinding" contract="Indigo.IService" />  
  7.         <host>  
  8.           <baseAddresses>  
  9.             <add baseAddress="http://localhost:1111/Indigo" />  
  10.           </baseAddresses>  
  11.         </host>  
  12.       </service>  
  13.     </services>  
  14.     <behaviors>  
  15.       <serviceBehaviors>  
  16.         <behavior name="MyServiceTypeBehaviors">  
  17.           <serviceMetadata httpGetEnabled="true" />  
  18.           <serviceDebug includeExceptionDetailInFaults="true" />  
  19.         </behavior>  
  20.       </serviceBehaviors>  
  21.     </behaviors>  
  22.   </system.serviceModel>  
  23. </configuration>  
Build the project.Run Host.exe from VS2005 command prompt. You should get the message at the command prompt as follows:
 
 
 
Add another project "IndigoClient" and add System.ServiceModel
 
Now add service refrence to the service URL (i.e. http://localhost:1111/Indigo)
 
It will create localhost.cs in service reference.
 
Add a class Program.cs as follows:
  1. using System;    
  2. using System.Collections.Generic;    
  3. using System.Text;    
  4. using System.ServiceModel;    
  5. namespace Indigo    
  6. {    
  7.     class Program    
  8.     {    
  9.         static void Main(string[] args)    
  10.         {    
  11.             string result = "";    
  12.             try    
  13.             {    
  14.                 IndigoClient.localhost.IService ww = new IndigoClient.localhost.ServiceClient();    
  15.                 result = ww.GetMessage();    
  16.             }    
  17.             catch (FaultException<IndigoClient.localhost.MyFaultException> ee)    
  18.             {    
  19.                result = "Exception: " + ee.Detail.Reason;    
  20.             }    
  21.             Console.WriteLine(result);    
  22.             Console.ReadLine();    
  23.         }    
  24.     }    
  25. }  
Add app.config with following XML
  1. <?xml version="1.0" encoding="utf-8" ?>  
  2. <configuration>  
  3.   <system.serviceModel>  
  4.     <bindings>  
  5.       <wsHttpBinding>  
  6.         <binding name="WSHttpBinding_IService" closeTimeout="00:01:00"  
  7.             openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"  
  8.             bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard"  
  9.             maxBufferPoolSize="524288" maxReceivedMessageSize="65536"  
  10.             messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true"  
  11.             allowCookies="false">  
  12.           <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"  
  13.               maxBytesPerRead="4096" maxNameTableCharCount="16384" />  
  14.           <reliableSession ordered="true" inactivityTimeout="00:10:00"  
  15.               enabled="false" />  
  16.           <security mode="Message">  
  17.             <transport clientCredentialType="Windows" proxyCredentialType="None"  
  18.                 realm="" />  
  19.             <message clientCredentialType="Windows" negotiateServiceCredential="true"  
  20.                 algorithmSuite="Default" establishSecurityContext="true" />  
  21.           </security>  
  22.         </binding>  
  23.       </wsHttpBinding>  
  24.     </bindings>  
  25.     <client>  
  26.       <endpoint address="http://localhost:1111/Indigo/Service" binding="wsHttpBinding"  
  27.           bindingConfiguration="WSHttpBinding_IService" contract="IndigoClient.localhost.IService"  
  28.           name="WSHttpBinding_IService">  
  29.         <identity>  
  30.           <userPrincipalName value="[email protected]" />  
  31.         </identity>  
  32.       </endpoint>  
  33.     </client>  
  34.   </system.serviceModel>  
  35. </configuration>  
Note: Please add your username and domain name under userPrincipalName
 
Now run the client project; you should get following error message:
 
 
 
You can now see the detailed error message returned from the service.


Similar Articles