[WCF 学习笔记] 5. 异常处理

本文介绍了Windows Communication Foundation (WCF)中如何处理服务异常,并将其转换为SOAP faults传递给客户端。通过示例展示了如何使用FaultException及FaultContractAttribute提供更友好的错误信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

WCF 将服务异常(Exception)转换成 SOAP faults,传递到客户端后再次转换成 Exception。只不过缺省情况下,我们很难从中获取有意义的信息。

[ServiceContract]
public interface ICalculate
{
  [OperationContract]
  int Add(int a, int b);
}

public class CalculateService : ICalculate
{
  public int Add(int a, int b)
  {
    throw new Exception("错误!");
  }
}


客户端调用 Add 方法触发异常,信息如下:

System.ServiceModel.FaultException: The server was unable to process the request due to an internal error. For more information about the error, either turn on IncludeExceptionDetailInFaults (either from ServiceBehaviorAttribute or from the <serviceDebug> configuration behavior) on the server in order to send the exception information back to the client, or turn on tracing as per the Microsoft .NET Framework 3.0 SDK documentation and inspect the server trace logs.

Server stack trace:
  在 System.ServiceModel.Channels.ServiceChannel.ThrowIfFaultUnderstood(Message reply, MessageFault fault, String action, MessageVersion version, FaultConverter faultConverter)
  ......


(我们可以使用 "(host as ServiceHost).Description.Behaviors.Find<ServiceDebugBehavior>().IncludeExceptionDetailInFaults = true;" 来启动调试行为,这样也能看到具体的出错信息! )

当然,WCF 会提供一个包装异常类 FaultException 来帮助我们处理这些问题。

[ServiceContract]
public interface ICalculate
{
  [OperationContract]
  int Add(int a, int b);
}

public class CalculateService : ICalculate
{
  public int Add(int a, int b)
  {
    throw new FaultException(new Exception("错误!").Message);
  }
}


这次输出的信息要友好得多。

System.ServiceModel.FaultException: 错误!

Server stack trace:
  在 System.ServiceModel.Channels.ServiceChannel.HandleReply(ProxyOperationRuntime operation, ProxyRpc& rpc)
  ......


另外,我们还可以通过 FaultContractAttribute 传递更详细的异常信息给客户端。

[DataContract]
public class FaultMessage
{
  [DataMember] public string Message;
  [DataMember] public int ErrorCode;
}

[ServiceContract]
public interface ICalculate
{
  [OperationContract]
  [FaultContract(typeof(FaultMessage))]
  int Add(int a, int b);
}

public class CalculateService : ICalculate
{
  public int Add(int a, int b)
  {
    FaultMessage fault = new FaultMessage();
    fault.Message = "错误信息!";
    fault.ErrorCode = 1234;

    throw new FaultException<FaultMessage>(fault, fault.Message);
  }
}


客户端代码

try
{
  CalculateClient client = new ConsoleApplication1.localhost.CalculateClient();
  client.Add(1, 2);
}
catch (FaultException<FaultMessage> e)
{
  Console.WriteLine("{0}; {1}", e.Detail.Message, e.Detail.ErrorCode);
}


-- 更详细的信息,请参考《WCF - FaultException》--

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值