Exception handler with wcf is not working in enterprise library 5.0

Topics: Exception Handling Application Block
Sep 19, 2012 at 9:35 AM
WCF Service: 
[ServiceContract]
    public interface IService1
    {
 
        [OperationContract]
        [FaultContract(typeof(WeGoofedDetails))]
        string GetData(int value);
 
        
 
        // TODO: Add your service operations here
    }
 
 
    [DataContract]
    public class WeGoofedDetails
    {
        [DataMember]
        public string Message;
 
        [DataMember]
        public Guid ErrorID;
    }

 [ExceptionShielding("Policy")]
    public class Service1 : IService1
    {
        public string GetData(int value)
        {
            throw new Exception("I broke the code!");
        }
 
        
    }

<?xml version="1.0"?>
<configuration>
 
  <configSections>
    <section name="exceptionHandling" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Configuration.ExceptionHandlingSettings, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
  </configSections>
  <exceptionHandling>
    <exceptionPolicies>
      <add name="Policy">
        <exceptionTypes>
          <add name="All Exceptions" type="System.Exception, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
            postHandlingAction="ThrowNewException">
            <exceptionHandlers>
              <add type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.WCF.FaultContractExceptionHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.WCF, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                exceptionMessageResourceType="" exceptionMessage="Oops" faultContractType="WcfService1.WeGoofedDetails, WcfService1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
                name="Fault Contract Exception Handler">
                <mappings>
                  <add source="{Guid}" name="ErrorID" />
                </mappings>
              </add>
            </exceptionHandlers>
          </add>
        </exceptionTypes>
      </add>
    </exceptionPolicies>
  </exceptionHandling>
  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
          <serviceMetadata httpGetEnabled="true"/>
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
 <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>
  
</configuration>

Client:
class Program
    {
        static void Main(string[] args)
        {
            try
            {
                Service1Client proxy = new Service1Client();
                var output = proxy.GetData(7);
                Console.WriteLine(output);
                Console.ReadLine();
            }
            catch (FaultException<WeGoofedDetails> ex)
            {
                Console.WriteLine(ex.ToString());
            }
 
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
            
        }
    }

Its always getting caught in 

catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
instead of faultexception.

Any suggestion on this.
Sep 20, 2012 at 4:44 AM

Thanks for posting a full set of code and configuration for your issue.

I copied all of your code and configuration into a new project and it is working OK.  What exception are you getting instead of FaultException?

I would recommend Configuring Tracing for WCF and see if that gives you any additional information:

<configuration>
   <system.diagnostics>
      <sources>
            <source name="System.ServiceModel" 
                    switchValue="Information, ActivityTracing"
                    propagateActivity="true">
            <listeners>
               <add name="traceListener" 
                   type="System.Diagnostics.XmlWriterTraceListener" 
                   initializeData= "c:\log\Traces.svclog" />
            </listeners>
         </source>
      </sources>
   </system.diagnostics>
</configuration>

You could also download the WCF Exception Shielding With Logging Exception Handler Sample Project to compare.

--
Randy Levy
Enterprise Library support engineer
entlib.support@live.com