1.(服务端)抛出和(客户端)捕获SOAP Fault
当我们需要客户端获取到WCF服务端的抛出的异常的时候,使用FaultException类
WCF类库在System.ServiceModel命名空间下提供了FaultException类。如果WCF服务抛出FaultException对象,WCF运行时将生成SOAP fault消息并回传给客户端程序。
服务端抛出异常
catch (Exception ex)
{
if (ex.InnerException is System.Data.SqlClient.SqlException)
throw new FaultException(string.Format("Exception accessing database:{0}",
ex.InnerException.Message), new FaultCode("Connect to database"));
else
throw new FaultException(string.Format("Exception reading from numbers: {0}",
ex.Message), new FaultCode("Iterate through products"));
} 客户端捕获异常
try
{
...
}
catch (FaultException ex)
{
Console.WriteLine("{0}: {1}", ex.Code.Name, ex.Reason)
} 2.使用强类型SOAP faults
调用WCF服务时不知道会抛出什么样的异常,使用一个类来约定客户端和服务器端来约定服务器端抛出来的异常
服务器端代码:
[DataContract]
public class SystemFault
{
[DataMember]
public string SystemOperation { get; set; } [DataMember]
public string SystemReason { get; set; } [DataMember]
public string SystemMessage { get; set; }
} [FaultContract(typeof(SystemFault))]
[OperationContract]
string GetData(int value); public string GetData(int value)
{
SystemFault fault = new SystemFault
{
SystemOperation = "哪个操作",
SystemReason = "错误原因",
SystemMessage ="错误消息"
};
throw new FaultException(fault);
return string.Format("You entered: {0}", value);
} 客户端代码:
catch (FaultException sf)
{
Console.WriteLine("SystemFault {0}: {1}\n{2}",
sf.Detail.SystemOperation,
sf.Detail.SystemMessage,
sf.Detail.SystemReason);
} 3.报告意外预料之外的异常
在你开发WCF服务时,为了在客户端程序调试,将会把服务端发生的所有异常(包括预料之内的和预料之外的)转换成SOAP faults消息传送至客户端是非常有用的。
调试的时候将WCF服务的配置文件 设置为true,等正式上线的时候设置为false 4.在WCF服务宿主处理异常
(1)针对错误处理,ServiceHost则提供了Faulted事件,调用该事件后,ServiceHost对象进入Faulted状态。当发生错误的时候,你通过订阅该事件,然后使用其提供的方法确定发生错误的原因,最后抛弃现有的服务并重启一个新的服务。
示例代码:
try
{
ServiceHost productsServiceHost;
productsServiceHost = new ServiceHost(typeof(Products.ProductsService));
productsServiceHost.Open(); // subscribe the faulted event
productsServiceHost.Faulted += (eventSender, eventArgs) =>
{
productsServiceHost.Abort(); productsServiceHost = new ServiceHost(typeof(Products.ProductsService));
productsServiceHost.Open();
};
}
catch (Exception ex)
{
// handleException(ex);
} (2)在宿主程序中处理来自客户端的预料之外的消息
客户端发送消息
Message request = Message.CreateMessage(MessageVersion.Soap11, "http://tempuri.org/IProductsService/ListProducts");
服务端处理
productsServiceHost.UnknownMessageReceived +=(eventSendaer, eventArgs)
{
MessageBox.Show(string.Format("A client attempted to send the message {0}",
eventArgs.Message.Headers.Action));
};

重温WCF之WCF抛出异常的处理SOAP Fault(十二)的更多相关文章

  1. WCF服务创建与抛出强类型SOAP Fault

    原创地址:http://www.cnblogs.com/jfzhu/p/4060666.html 转载请注明出处 前面的文章<WCF服务的异常消息>中介绍过,如果WCF Service发生 ...

  2. WCF技术剖析之二十二: 深入剖析WCF底层异常处理框架实现原理[中篇]

    原文:WCF技术剖析之二十二: 深入剖析WCF底层异常处理框架实现原理[中篇] 在[上篇]中,我们分别站在消息交换和编程的角度介绍了SOAP Fault和FaultException异常.在服务执行过 ...

  3. 跟我一起学WCF(13)——WCF系列总结

    引言 WCF是微软为了实现SOA的框架,它是对微乳之前多种分布式技术的继承和扩展,这些技术包括Enterprise Service..NET Remoting.XML Web Service.MSMQ ...

  4. Learing WCF Chapter1 WCF Services

    WCF ServicesWCF services are the new distributed boundary in an enterprise application—with an empha ...

  5. WCF技术剖析之十二:数据契约(Data Contract)和数据契约序列化器(DataContractSerializer)

    原文:WCF技术剖析之十二:数据契约(Data Contract)和数据契约序列化器(DataContractSerializer) [爱心链接:拯救一个25岁身患急性白血病的女孩[内有苏州电视台经济 ...

  6. 扩展Wcf call security service, 手动添加 Soap Security Head.

    有次我们有个项目需要Call 一个 Java 的 web service, Soap包中需要一个 Security Head <soapenv:Header> <wsse:Secur ...

  7. 重温WCF之WCF传输安全(十三)(4)基于SSL的WCF对客户端采用证书验证(转)

    转载地址:http://www.cnblogs.com/lxblog/archive/2012/09/20/2695397.html 前一篇我们演示了基于SSL的WCF 对客户端进行用户名和密码方式的 ...

  8. 重温WCF之WCF中可靠性会话(十四)

    1.WCF中可靠性会话在绑定层保证消息只会被传输一次,并且保证消息之间的顺序.当使用TCP(Transmission Control Protocol,传输控制协议)通信时,协议本身保证了可靠性.然而 ...

  9. 重温WCF之WCF传输安全(十三)(3)基于SSL的WCF对客户端验证(转)

    转载地址:http://www.cnblogs.com/lxblog/archive/2012/09/18/2690719.html 上文我们演示了,客户端对服务器端身份的验证,这一篇来简单演示一下对 ...

随机推荐

  1. [转载]html中DTD使用小结

    原文链接:http://www.jb51.net/web/36856.html DTD 是一套关于标记符的语法规则.它是XML1.0版规格得一部分,是html文件的验证机制,属于html文件组成的一部 ...

  2. android中paint的setXfermode属性

    本文前半部分来自于:http://www.cnblogs.com/rayray/p/3670120.html 1.下面的Xfermode子类可以改变这种行为: AvoidXfermode  指定了一个 ...

  3. linux 编程环境搭建过程记录

    1, 安装centos 7 最小版  过程略 ...... 2, 安装桌面安装yum groupinstall "GNOME Desktop" 更新系统运行级别ln -sf /li ...

  4. Python自动化之IO多路复用

    单线程.多线程和异步对比图 灰色的是阻塞 IO多路复用 用户空间与内核空间 现在操作系统都是采用虚拟存储器,那么对32位操作系统而言,它的寻址空间(虚拟存储空间)为4G(2的32次方).操作系统的核心 ...

  5. sizeof和strlen的区别

    一.sizeof    sizeof(...)是运算符,而不是一个函数.    sizeof操作符的结果类型是size_t,在头文件中typedef为unsigned int,其值在编译时即计算好了, ...

  6. 【leetcode】Next Permutation

    Next Permutation Implement next permutation, which rearranges numbers into the lexicographically nex ...

  7. android一句话搞定图片加载

    http://square.github.io/picasso/ Picasso.with(context).load("http://i.imgur.com/DvpvklR.png&quo ...

  8. 游戏服java程序启动,显示内存溢出

    1.OutOfMemoryError:Java heap space 过程:服务器上面的mysql突然异常重启,导致了程序启动的时候报错 问题1:OutOfMemoryError:Java heap ...

  9. diff & pattch 命令

    基础知识 该命令的功能为逐行比较两个文本文件,列出其不同之处.它比comm命令完成更复杂的检查.它对给出的文件进行系统的检查,并显示出两个文件中所有不同的行,不要求事先对文件进行排序. 语法:diff ...

  10. JS里设定延时:js中SetInterval与setTimeout用法

     js中SetInterval与setTimeout用法 JS里设定延时: 使用SetInterval和设定延时函数setTimeout 很类似.setTimeout 运用在延迟一段时间,再进行某项操 ...