2009-12-09 48 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