开发者

Enterprise Library Exception Handling for WCF Fault Contracts - CLIENT SIDE

I have a Windows Service which communicates with WCF services. The WCF services are all fault shielded and generate custom UserFaultContracts and ServiceFaultContracts. No problems there.

In the Windows Service I am using EntLib for exception handling and logging.

I do not want to try catch for faults

try
{
}
catch (FaultException<UserFaultContract>)
{
}

I want to use EntLib

try
{
}
catch (Exception ex)
{
var rethrow = ExceptionPolicy.HandleException(ex, "Transaction Policy");
if (rethrow) throw;
}

This also works, however, in my Tranasaction Policy I want to Log the details of the UserFaultContract. This is where I am unglued. And I hate becoming unglued. The fault is captured and logged...but I can't get the details of the fault.

My exception policy is

  <add name="Transaction Policy">
    <exceptionTypes>
      <add type="System.Exception, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
        postHandlingAction="None" name="Exception">
        <exceptionHandlers>
          <add logCategory="General" eventId="200" severity="Error" title="Transaction Error"
            formatterType="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.TextExceptionFormatter, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
            priority="2" useDefaultLogger="true" 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="None" name="FaultException">
        <exceptionHandlers>
          <add logCategory="General" eventId="200" severity="Error" title="Service Fault"
            formatterType="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.TextExceptionFormatter, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
            priority="2" useDefaultLogger="true" 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>
    </exceptionTypes>
  </add>

The exception logged is:

Timestamp: 5/13/2010 14:53:40 Message: HandlingInstanceID: e9038634-e16e-4d87-ab1e-92379431838b

An exception of type 'System.ServiceModel.FaultException`1[[LCI.DispatchMaster.FaultContracts.ServiceFaultContract, LCI.DispatchMaster.FaultContracts, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]' occurred and was caught.

05/13/2010 10:53:40 Type : System.ServiceModel.FaultException`1[[LCI.DispatchMaster.FaultContracts.ServiceFaultContract, LCI.DispatchMaster.FaultContracts, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]], System.ServiceModel, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 Message : There was an internal fault at the DispatchMaster service. Source : mscorlib Help link : Detail : LCI.DispatchMaster.FaultContracts.ServiceFaultContract Action : http://LCI.DispatchMaster.LogicalChoices.com/ITruckMasterService/MergeScenarioServiceFaultContractFault Code : System.ServiceModel.FaultCode Reason : There was an internal fault at the DispatchMaster service. Data : System.Collections.ListDictionaryInternal TargetSite : Void HandleReturnMessage(System.Runtime.Remoting.Messaging.IMessage, System.Runtime.Remoting.Messaging.IMessage) Stack Trace :

In the fault contact there is an ID and a Message. I would, as you can see, like the ID and Message to be logged by EntLib.

I am assuming that I'm going to have to write a custom handler to exctract the fault details - but thought I'd ask if I'm missing something in EntLib which might help me avoid that task.

Thanks to anyone who is wi开发者_JS百科lling to help.


I'm actually doing very similar research right now as to how to properly deal with WCF Faults and the EAB. I think this article might prove to be extremely useful to you. I haven't tried it, but it doesn't look too duanting.


If you are sure that everything is configured correctly in WCF service then you should be able to get the message you populated on service side.

Following code should display the full exception detail on client side.

private void CalculateDivision(decimal numerator, decimal denominator)
{
    try
    {
        // Create an instance of service client.
        ICalculatorService svc = new CalculatorServiceClient();
        // Call service method.
        Console.WriteLine("Result is: {0}", svc.Divide(numerator, denominator));
    }
    catch (Exception ex)
    {
        // Show details of the exception returned to the calling code.  
        ShowExceptionDetails(ex);
        // Show details of the fault contract returned from the WCF service.  
        ShowFaultContract(ex);
    }
}


public void ShowExceptionDetails(Exception ex)
{
    Console.WriteLine("Exception type {0} was thrown.", ex.GetType().ToString());
    Console.WriteLine("Message: '{0}'", ex.Message);
    Console.WriteLine("Source: '{0}'", ex.Source);
    if (null == ex.InnerException)
    {
        Console.WriteLine("No Inner Exception");
    }
    else
    {
        Console.WriteLine();
        Console.WriteLine("Inner Exception: {0}", ex.InnerException.ToString());
    }
    Console.WriteLine();
}

private static void ShowFaultContract(Exception ex)
{
    var faultContract = ex as FaultException<CalculatorFault>;
    if (faultContract != null)
    {
        CalculatorFault calculatorFault = faultContract.Detail;
        Console.WriteLine("Fault contract detail: ");
        Console.WriteLine("Fault ID: {0}", calculatorFault.FaultID);
        Console.WriteLine("Message: {0}", calculatorFault.FaultMessage);
    }
}

If it still doesn't work, then there is something wrong on the service side for sure. Let's see what the service code should look like for the particular client defined above.

[ServiceContract]
public interface ICalculatorService
{
    [OperationContract]
    [FaultContract(typeof(CalculatorFault))]
    decimal Divide(decimal num1, decimal num2);
}

[DataContract]
public class CalculatorFault
{
    [DataMember]
    public Guid FaultID { get; set; }

    [DataMember]
    public string FaultMessage { get; set; }
}

[ExceptionShielding("CalculatorServicePolicy")]
public class CalculatorService : ICalculatorService
{
    public decimal Divide(decimal num1, decimal num2)
    {
      Calculator calc = new Calculator();
      return calc.Divide(num1, num2);
    }
}

//Call the following function in Application_Start function of your Global.asax file
private void ConfigureExceptionHandlingBlock()
{
    var policies = new List<ExceptionPolicyDefinition>();

    var mappings = new NameValueCollection();
    mappings.Add("FaultID", "{Guid}");
    mappings.Add("FaultMessage", "{Message}");

    var calculatorServicePolicy = new List<ExceptionPolicyEntry>
        {
            new ExceptionPolicyEntry(typeof(Exception),
                PostHandlingAction.ThrowNewException,
                new IExceptionHandler[]
                {
                    new FaultContractExceptionHandler(typeof(CalculatorFault), "Some internal service Error. Check service logs.", mappings)
                })
        };
    policies.Add(new ExceptionPolicyDefinition("CalculatorServicePolicy", calculatorServicePolicy));
    ExceptionManager exManager = new ExceptionManager(policies);
    ExceptionPolicy.SetExceptionManager(exManager);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜