关于捕获FaultException <t> ex </t>

时间:2009-12-09 10:11:17

标签: .net wcf exception-handling enterprise-library enterprise

我在我的应用程序'服务器端使用Enterprise应用程序块来处理异常。

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

以下是我的网络配置输入...

<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中编写的异常消息。

任何人都可以帮我解决问题。

提前致谢

维克拉姆

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

您是否在操作中定义了错误合同?

[FaultContract(typeof(TESTServiceException))]

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

还应设置以下服务行为

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