[WCF 学习笔记] 5. 异常处理
来源:互联网 发布:2016淘宝最新刷单平台 编辑:程序博客网 时间:2024/05/29 17:15
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("错误!");
}
}
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 onIncludeExceptionDetailInFaults (either from ServiceBehaviorAttribute orfrom the <serviceDebug> configuration behavior) on the server inorder to send the exception information back to the client, or turn ontracing as per the Microsoft .NET Framework 3.0 SDK documentation andinspect the server trace logs.
Server stack trace:
在System.ServiceModel.Channels.ServiceChannel.ThrowIfFaultUnderstood(Messagereply, MessageFault fault, String action, MessageVersion version,FaultConverter faultConverter)
......
Server stack trace:
在System.ServiceModel.Channels.ServiceChannel.ThrowIfFaultUnderstood(Messagereply, MessageFault fault, String action, MessageVersion version,FaultConverter faultConverter)
......
(我们可以使用 "(host asServiceHost).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);
}
}
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)
......
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);
}
}
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);
}
{
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》--
- [WCF 学习笔记] 5. 异常处理
- WCF 学习笔记之异常处理
- 异常处理学习笔记
- WCF异常处理
- WCF - 异常处理
- WCF中的异常处理
- silverlight wcf 异常处理
- WCF HTTP异常处理
- WCF 异常处理
- Silverlight处理WCF异常
- WCF处理异常
- java 异常处理学习笔记
- java学习笔记 异常处理
- Java学习笔记(异常处理)
- iOS异常处理学习笔记
- 学习java异常处理笔记
- Python学习笔记--异常处理
- java异常处理学习笔记
- 深入理解CLDC和MIDP
- 【转】让VB6.0集成环境支持鼠标滑轮
- 深入理解MIDlet类
- 意外发现老版本cyberlink decoder存在的问题
- 深入理解Display类的使用
- [WCF 学习笔记] 5. 异常处理
- 无线路由设置
- 活力无限的J2ME技术
- TestDriven.NET测试开发工具
- J2ME中索引值(index)使用总结
- [WCF 学习笔记] 6. 会话状态
- 中国最“毒”的地方(来自美国电视台节目截图)
- 一个生成无重复数字的代码
- 手机程序开发技术介绍