【问题标题】:Best practice to handle and throw FaultException处理和抛出 FaultException 的最佳实践
【发布时间】:2014-07-24 11:26:19
【问题描述】:

我需要找到最好的方法来做到这一点,

try
{
  // Service call.
}
 catch (FaultException exception)
      {
        service.Abort();
        throw new FaultException(Resources.UnexpectedErrorOccurredAtServer, exception);
      }

或者

catch (FaultException exception)
      {
        service.Abort();
        throw new Exception(Resources.UnexpectedErrorOccurredAtServer, exception);
      }

// 调用者。

Main()
{
try
{
 serviceCaller()
}
catch(FaultException ex)
{
  // Should we have this catch???
}
catch( Exception ex)
{
  // Handle unexpected errors.
}

向调用者抛出预期异常的最佳方法是什么。 如果我们抛出 FaultException,调用者的 main 方法应该显式处理它,否则一般的异常都会起作用。

【问题讨论】:

    标签: c# wcf exception-handling


    【解决方案1】:

    最好的方法是

    try
    {
      // ...
    }
    catch (FaultException ex)
    {
        service.Abort();
        throw;
    }
    

    这会保留堆栈跟踪,而您建议的方法不会。见Throwing Exceptions best practices

    【讨论】:

      【解决方案2】:

      您可能有兴趣查看WCF error handling and some best practices

      最好的方法是这样的:-

      try
      {
        proxy.SomeOperation();
      }
      catch (FaultException<MyFaultInfo> ex)
      {
        // only if a fault contract was specified
      }
      catch (FaultException ex)
      {
        // any other faults
      }
      catch (CommunicationException ex)
      {
        // any communication errors?
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-08-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-04-12
        • 1970-01-01
        • 2018-01-03
        相关资源
        最近更新 更多