title: WCF Exception Handling #2
body: <p>I am having N number of WCF service method. I want to implement the exception handling. My service will be consumed by .net and non .net client. Is Fault contract is the only way to implement the exception handling? If it is means how can i avoid implementing to all the methods in my service. How can i implement Exception handling in one place to handle the exception of all the methods in my service. Kindly provider some set of codes to easily under stable. </p>
  
  <p>Here is my sample code.</p>
  
  <hr>
  
  <p>MyFaultContract</p>
  
  <hr>
  
  <pre><code>[DataContract()]  
  public class FleetCustomException  
  {  
      [DataMember()]  
      public string Title;  
      [DataMember()]  
      public string ExceptionMessage;  
      [DataMember()]  
      public string InnerException;  
      [DataMember()]  
      public string StackTrace;          
  }  
  </code></pre>
  
  <p>MyServiceContract  </p>
  
  <hr>
  
  <pre><code>[ServiceContract]  
  public interface IAddress  
  {  
      [OperationContract]  
  [FaultContract(typeof(FleetCustomException))]  
      bool SaveAddress(string address1, string address2, string address3, string city, string province, string postalCode,string country,bool isDeleted, int createdBy, DateTime createdDate, int lastModifiedBy, DateTime lastModifiedDate);  
  
      [OperationContract]  
  [FaultContract(typeof(FleetCustomException))]  
      bool DeleteAddress(Int64 addressID, int lastModifiedBy);  
  
      [OperationContract]  
  [FaultContract(typeof(FleetCustomException))]  
      List&lt;Address&gt; GetAllAddresses();  
  }  
  </code></pre>
  
  <p>Service  </p>
  
  <hr>
  
  <pre><code>public class SettingsService : IAddress  
  {  
  
      #region IAddress Members  
  
      public bool SaveAddress(string address1, string address2, string address3, string city, string province,string postalCode,string country, bool isDeleted,int createdBy, DateTime createdDate, int lastModifiedBy, DateTime lastModifiedDate)  
      {  
          try  
          {  
              Address address = new Address(0, address1, address2, address3, city, province, postalCode, country, isDeleted,createdBy, createdDate, lastModifiedBy, lastModifiedDate);  
              return address.Insert();  
  
          }  
          catch (FaultException ex)  
          {  
  
            FleetCustomException ex = new FleetCustomException();  
            ex.Title = "Error Funtion:SaveAddress()";  
        ex.ExceptionMessage = "Error occur while doing save address function.";  
            ex.InnerException = "Inner exception message from serice";  
            ex.StackTrace = "Stack Trace message from service.";  
            throw new FaultException(ex,"Reason: Testing the Fault contract") ;  
          }  
      }     
  
  
  
  
      public bool DeleteAddress(Int64 addressID, int lastModifiedBy)  
      {  
          Address address = new Address(addressID, lastModifiedBy);  
          return address.Delete();  
      }  
  
      public List&lt;Address&gt; GetAllAddresses()  
      {  
  
          Address address = new Address();  
          return address.GetAllAddresses();  
      }  
      #endregion  
  </code></pre>
  
  <p>}  </p>
  
  <p>config in Service layer includeExceptionDetailInFaults="true" in <code>&lt;serviceDebug&gt;</code> </p>
  
  <hr>
  
  <pre><code>&lt;serviceDebug includeExceptionDetailInFaults="true"/&gt;  
  </code></pre>
  
  <h2>Consume Method</h2>
  
  <pre><code>   try  
     {  
        MySettingsService.SettingsService  proxy = new MySettingsService.SettingsService();
        proxy .SaveAddress(Address1TextBox.Text, Address2TextBox.Text, "", "", "", "641025", "", false, 1, DateTime.Now, 0, DateTime.Now);  
      }  
      catch (FaultException&lt;MyCalculatorService.CustomException&gt; ex)  
       {  
          //Process the Exception
       }  
  </code></pre>
  
  <hr>
  
  <p>i have to implement to all my service methods i am having more than 100 method is there any way to achieve it quickly as well as effectively  </p>