【问题标题】:Regarding Catching FaultException<T> ex关于捕获 FaultException<T> ex
【发布时间】:2010-12-24 18:43:23
【问题描述】:

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

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

这是我的网络配置输入...

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

谁能帮我解决问题。

提前致谢

维克拉姆

【问题讨论】:

  • 什么异常(包括InnerException你得到客户端?
  • 我收到 System.ServiceModel.FaultException 类型的异常,而不是 System.ServiceModel.FaultException 类型的异常

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


【解决方案1】:

尝试使用 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的解释。

【讨论】:

    【解决方案2】:

    您是否在操作上定义了故障契约?

    [FaultContract(typeof(TESTServiceException))]
    

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

    还应设置以下服务行为

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

    【讨论】:

    • [FaultContract(typeof(TESTServiceException))] 和 [ExceptionShielding] 属性已正确设置。我能够获得我在 app.config 中设置的异常消息。但我的异常类型是 System.ServiceModel.FaultException 而不是 System.ServiceModel.FaultException 类型
    • 只有 TESTBusinessException 会被转换为 FaultException。你确定你在操作中抛出了正确的异常吗?尝试从你的操作中抛出一个新的 TESTBusinessException() 看看会发生什么。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-11-01
    • 2013-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多