2009-12-09 49 views
2

我在我的应用程序服务器端使用Enterprise应用程序块来处理异常。关于捕捉FaultException <T> ex

我能够成功地处理异常。我创建了一个自定义服务错误类来处理异常。

这里是我的web配置enteries ...

<exceptionPolicies> 
    <add name="WCF Exception Shielding"> 
    <exceptionTypes> 
     <add type="TEST.Infrastructure.ExceptionHandling.Exceptions.TESTBusinessException, Pluto.Infrastructure.ExceptionHandling, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" 
     postHandlingAction="ThrowNewException" name="TESTBusinessException"> 
     <exceptionHandlers> 
      <add logCategory="BusinessLoggingCategory" eventId="501" severity="Error" 
      title="Pluto Service Exception" formatterType="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.TextExceptionFormatter, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling" 
      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" /> 
      <add 
      type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.WCF.FaultContractExceptionHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.WCF, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" 
      name="DefaultFaultContract Handler" 
      faultContractType="TEST.Infrastructure.ExceptionHandling.Exceptions.TESTServiceException, Pluto.Infrastructure.ExceptionHandling" 
      exceptionMessage="Pluto.Infrastructure.ExceptionHandling.Exceptions.TESTBusinessException {handlingInstanceID}"> 
      <mappings> 
       <add name="Id" source="{Guid}"/> 
       <add name="MessageText" source="{Message}"/> 
      </mappings> 
      </add> 
     </exceptionHandlers> 
     </add> 

但在客户端上时,我试图抓住这个异常的

catch (FaultException<TESTServiceException> fex) 
     { 
     } 

这个异常没有被捕获。 我能够在客户端获取我在服务器端的app.config中写入的异常消息。

任何人都可以帮我找出问题。

在此先感谢

维克拉姆

+0

什么异常(包括'InnerException')**是你得到客户端? – 2009-12-09 10:14:09

+0

我正在异常类型 System.ServiceModel.FaultException的,而不是类型的 System.ServiceModel.FaultException 2009-12-09 10:29:52

+0

和内部异常为NULL。 – 2009-12-09 10:36:09

回答

2

尝试提取使用的XmlReader异常的详细信息:

catch (FaultException ex) 
{ 
    string msg = "FaultException: " + ex.Message; 
    MessageFault fault = ex.CreateMessageFault(); 
    if (fault.HasDetail) 
    { 
     System.Xml.XmlReader reader = fault.GetReaderAtDetailContents(); 
     if (reader.Name == "TESTServiceException") 
     { 
      TESTServiceException detail = fault.GetDetail<TESTServiceException>(); 
      msg += "\n\nStack Trace: " + detail.SomeTraceProperty; 
     } 
    } 
    MessageBox.Show(msg); 
} 

更多的解释上醒目故障异常herehere

0

你有没有定义的操作故障合同?

[FaultContract(typeof(TESTServiceException))] 

您是否定义了服务合同上的[ExceptionShielding]属性?

还有以下服务行为应设置

<behaviors> 
     <serviceBehaviors> 
     <behavior name="StandardBehaviour"> 
      <serviceDebug includeExceptionDetailInFaults="false" /> 
     </behavior> 
     </serviceBehaviors> 
</behaviors> 
+0

ServiceDebug值已设置为false ... – 2009-12-09 11:20:27

+0

[FaultContract(typeof(TESTServiceException))] 和[ExceptionShielding]属性已正确设置。 我能够得到我在app.config中设置的异常消息。但是我的异常类型是System.ServiceModel.FaultException,而不是System.ServiceModel.FaultException类型的异常。 2009-12-09 11:36:58

+0

只有TESTBusinessException将被转换为FaultException 。你确定你在操作中抛出正确的异常吗? 尝试在您的操作中只做一个新的TESTBusinessException(),看看会发生什么。 – softveda 2009-12-09 12:08:22