WCF FaultExceptions with EntLib 4.1

Feb 25, 2009 at 9:47 AM
Edited Feb 25, 2009 at 9:51 AM
Hi,

I'm using Enterprise Library 4.1 and want to log exceptions in the EventLog as well as sending them back to the client.
FaultExceptions are getting passed back to the client, but I cannot specify a custom type of FaultException. Every time the exception message is also the same.

My Service app.config:

<exceptionHandling>
    <exceptionPolicies>
      <add name="WCF Exception Shielding">
        <exceptionTypes>
          <add type="System.Exception, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
            postHandlingAction="ThrowNewException" name="Exception">
            <exceptionHandlers>
              <add exceptionMessage="Ein Fehler ist aufgetreten" faultContractType="DataService.Wcf.FaultContracts.EmptyTimeSeriesScheduleFault, DataService.Wcf.FaultContracts.FaultContracts, Version=1.0.0.0, Culture=neutral, PublicKeyToken=0c7b338c6202a078"
                type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.WCF.FaultContractExceptionHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.WCF, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                name="Fault Contract Exception Handler">
                <mappings>
                  <add source="{Guid}" name="Id" />
                  <add source="{Message}" name="MessageText" />
                </mappings>
              </add>
              <add logCategory="General" eventId="100" severity="Error" title="Enterprise Library Exception Handling"
                formatterType="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.TextExceptionFormatter, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                priority="0" useDefaultLogger="false" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging.LoggingExceptionHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                name="Logging Handler" />
            </exceptionHandlers>
          </add>
          <add type="System.ServiceModel.FaultException, System.ServiceModel, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
            postHandlingAction="NotifyRethrow" name="FaultException" />
        </exceptionTypes>
      </add>
    </exceptionPolicies>
  </exceptionHandling>




ServiceImplementation:

public override ResponseMessage GetData(RequestMessage request)
{
    ResponseMessage response = new ResponseMessage();

    if (somecondition)
    {
        // calling BL and translator
    }
    else
    {
        EmptyTimeSeriesScheduleFault fault = new EmptyTimeSeriesScheduleFault();
        throw new FaultException<EmptyTimeSeriesScheduleFault>(fault, new FaultReason("An error has occured."));
    }
   
    return response;
}


Client:

try
{
    Data d = proxy.GetData(new RequestMessage());
}
catch (FaultException<EmptyTimeSeriesScheduleFault> customEx)
{
    MessageBox.Show(customEx.ToString());
}
catch (FaultException e)
{
    MessageBox.Show(e.ToString());
}


Every time my service throws an Exception, the client ends up in the second catch block with a FaultException, and not FaultException<EmptyTimeSeriesScheduleFault> what I was expecting.

The exception e seems to be very generic:
base {System.ServiceModel.CommunicationException} = {"An error has occurred while consuming this service. Please contact your administrator for more information. Error ID: c082eac7-2d6f-47c5-b2c8-cbb0b018232c"}

Do I have a configuration issue, or what am I doing wrong?

Thanks a lot
Marc





Developer
Feb 26, 2009 at 2:42 PM
Marc,

I hope this thread might show you some hint into the right direction.
http://www.codeplex.com/servicefactory/Thread/View.aspx?ThreadId=25234

Regards,
Hernan