转载来的wcf学习笔记05
2008-05-28 16:51
375 查看
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);
}
[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学习笔记01
- 转载来的wcf学习笔记04
- wcf 学习笔记一 服务寄存到iis(个人写,非转载)
- Java学习笔记—运算符(转载)
- 【转载】自定义View学习笔记之详解onMeasure
- 转载了别人的cocos2d-x的学习笔记
- LINUX学习笔记——DNS服务器设置篇(转载)
- WCF学习笔记之 - 搭建WCF技术知识框架
- GTK+图形化应用程序开发学习笔记(二)—Glib库(1) 转载:http://blog.chinaunix.net/u/22935/article_56836.html
- 【转载】Lucene学习笔记(四)
- WCF学习笔记 -- 如何用C#开发一个WebService
- [转载]cmake 学习笔记(一)
- lua学习笔记05
- 一板一眼:wcf学习笔记开篇
- 【转载】MQTT学习笔记——MQTT协议体验 Mosquitto安装和使用
- WCF学习笔记之重载
- wcf学习笔记2 系统架构
- 转载:C++ list 类学习笔记
- WCF学习笔记一:WCF服务库和宿主
- linux鸟哥视频学习笔记05