WCF中的异常
一、考虑到安全因素,为了避免将服务端的异常发送给客户端。默认情况下,服务端出现异常会对异常屏蔽处理后,再发送到客户端。所以客户端捕捉到的异常都是同一个FaultException异常。

例如在服务端直接产生一个空引用异常,客户端捕获到的是上述异常。
服务端:
class Program
{
static void Main(string[] args)
{
ServiceHost host = new ServiceHost(typeof(SayHello));
host.AddServiceEndpoint(typeof(ISayHello), new WSHttpBinding(), "http://localhost:4216");
host.Opened += delegate { Console.WriteLine("Service Start!"); };
host.Open();
Console.ReadLine();
}
}
[ServiceContract]
public interface ISayHello
{
[OperationContract]
void Say();
} public class SayHello : ISayHello
{
public void Say()
{
string name = null;
Console.Write("Hello {0}", name.Length);
}
}
客户端:
class Program
{
static void Main(string[] args)
{
ISayHello ClientChannel = ChannelFactory<ISayHello>.CreateChannel(new WSHttpBinding(), new EndpointAddress("http://localhost:4216"));
try
{
ClientChannel.Say();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.WriteLine(ex.StackTrace);
}
}
}
二、可通过配置ServiceDebugBehavior将IncludeExceptionDetailInFaults,将其置为true。则服务端会将异常原封不动的传递到客户端,该配置默认为false。
[ServiceBehavior(IncludeExceptionDetailInFaults=true)]
public class SayHello : ISayHello
{
public void Say()
{
string name = null;
Console.Write("Hello {0}", name.Length);
}
}
客户端捕捉到空引用异常。

三、自定义异常信息
1.直接通过FaultException构造函数,构造异常。
[ServiceBehavior(IncludeExceptionDetailInFaults=true)]
public class SayHello : ISayHello
{
public void Say()
{
throw new FaultException("自定义异常");
}
}
2.通过FaultException<TDetail>构造异常。
TDetail是一个可序列化的数据结构,如下面定义的myException。需要注意的是在操作契约上需要加一个错误契约, [FaultContract(typeof(myException))]。
[ServiceContract]
public interface ISayHello
{
[OperationContract]
[FaultContract(typeof(myException))]
void Say();
}
[ServiceBehavior(IncludeExceptionDetailInFaults=true)]
public class SayHello : ISayHello
{
public void Say()
{
myException ex = new myException
{
Message = "自定义异常",
OperatorMethodName = "SayHello:Say()"
};
throw new FaultException<myException>(ex, ex.Message);
}
} [DataContract]
public class myException
{
[DataMember]
public string Message;
[DataMember]
public string OperatorMethodName;
}
客户端:
class Program
{
static void Main(string[] args)
{
ISayHello ClientChannel = ChannelFactory<ISayHello>.CreateChannel(new WSHttpBinding(), new EndpointAddress("http://localhost:4216"));
try
{
ClientChannel.Say();
}
catch (Exception ex)
{
myException myex = (ex as FaultException<myException>).Detail;
Console.WriteLine(myex.Message);
Console.WriteLine(myex.OperatorMethodName);
}
}
}

使用错误契约需要注意的地方:
(1)不能再同一个操作方法上声明相同的细节类型。如下所示:
[ServiceContract]
public interface ISayHello
{
[OperationContract]
[FaultContract(typeof(myException))]
[FaultContract(typeof(myException))]
void Say();
}
(2)错误契约上的细节类型不能等效。
[ServiceContract]
public interface ISayHello
{
[OperationContract]
[FaultContract(typeof(myException),Name="PersonalException",Namespace="www.cnblogs.cn/lh218")]
[FaultContract(typeof(myError), Name = "PersonalException", Namespace = "www.cnblogs.cn/lh218")]
void Say();
}
四、可以指定异常的序列化方式
默认的情况下异常消息还DataContractSerializer序列化的。可以通过XmlSerializerFormat的SupportFaults,指定使用XmlSerializer序列化异常消息。
[ServiceContract]
public interface ISayHello
{
[OperationContract]
[FaultContract(typeof(myException))]
[XmlSerializerFormat(SupportFaults=true)]
void Say();
}
五、错误消息的结构
由5部分组成
(1)FaultCode,可以看出是对异常消息的分类。结点内部的Value表示错误类型,SubCode表示错误子代码。
(2)FaultReason表示错误原因,内部有一个FaultReasonText集合,FaultReasonText支持全球化语言描述。
(3)FaultNode元素,表示在SOAP消息路由过程中产生异常的结点。
(4)FaultRole表示处理消息时所担当的角色。
(5)FaultDetail 即之前描述的错误细节。
下面代码讲创建一个错误消息:
using System.Xml;
using System.Xml.Serialization;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.Runtime.Serialization;
using System.ServiceModel.Channels;
namespace ConsoleApplication4
{
class Program
{
static void Main(string[] args)
{
FaultCode subCode = new FaultCode("lh218", "www.cnblogs.com/lh218");
FaultCode code = new FaultCode("myFault", subCode);
var text1=new FaultReasonText("异常消息","zh-CN");
var text2=new FaultReasonText("Exception Message","en-US");
FaultReason reason=new FaultReason(new FaultReasonText[]{text1,text2});
myException exDetail = new myException
{
Message = "自定义异常细节",
OperatorMethodName = "myException"
};
MessageFault fault = MessageFault.CreateFault(code, reason, exDetail, new DataContractSerializer(typeof(myException)), "lhActor","lhNode");
Message msg = Message.CreateMessage(MessageVersion.Soap12WSAddressing10, fault, "http://myaction");
Write(msg, @"D://1.txt");
Console.ReadLine();
} public static void Write(Message msg,string path)
{
using (XmlTextWriter writer = new XmlTextWriter(path,Encoding.UTF8))
{
writer.Formatting = Formatting.Indented;
msg.WriteMessage(writer);
}
}
}
}
消息为:
<s:Envelope xmlns:a="http://www.w3.org/2005/08/addressing" xmlns:s="http://www.w3.org/2003/05/soap-envelope">
<s:Header>
<a:Action s:mustUnderstand="1">http://myaction</a:Action>
</s:Header>
<s:Body>
<s:Fault>
<s:Code>
<s:Value>s:myFault</s:Value>
<s:Subcode>
<s:Value xmlns:a="www.cnblogs.com/lh218">a:lh218</s:Value>
</s:Subcode>
</s:Code>
<s:Reason>
<s:Text xml:lang="zh-CN">异常消息</s:Text>
<s:Text xml:lang="en-US">Exception Message</s:Text>
</s:Reason>
<s:Node>lhNode</s:Node>
<s:Role>lhActor</s:Role>
<s:Detail>
<PersonalException xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="www.cnblogs.com/lh218">
<Message>自定义异常细节</Message>
<OperatorMethodName>myException</OperatorMethodName>
</PersonalException>
</s:Detail>
</s:Fault>
</s:Body>
</s:Envelope>
六、异常与消息之间的转换关系

如上图所示,MessageFault是FaultException和Message直接的中转对象。
先看Server端:
FaultException ==》MessageFault转换方法:FaultException对象可以调用CreateMessageFault创建MessageFault对象。
public virtual MessageFault CreateMessageFault();
MessageFault ==》 Message:Message调用CreateMessage创建,传入MessageFault参数。
public static Message CreateMessage(MessageVersion version, MessageFault fault, string action);
Client端:
Message==>MessageFault:MessageFault类型内部的CreateFault方法可以提取异常消息中的MessageFault
public static MessageFault CreateFault(Message message, int maxBufferSize);
MessageFault ==》FaultException:FaultException类型提供两个静态方法创建FaultException。
public static FaultException CreateFault(MessageFault messageFault, params Type[] faultDetailTypes);
public static FaultException CreateFault(MessageFault messageFault, string action, params Type[] faultDetailTypes);
WCF中的异常的更多相关文章
- WCF学习之旅—WCF中传统的异常处理(十六)
WCF中的异常处理 在软件开发过程中,不可能没有异常的出现,所以在开发过程中,对不可预知的异常进行解决时,异常处理显得尤为重要.对于一般的.NET系统来说,我们简单地借助try/catch可以很容易地 ...
- WCF初探-26:WCF中的会话
理解WCF中的会话机制 在WCF应用程序中,会话将一组消息相互关联,从而形成对话.会话”是在两个终结点之间发送的所有消息的一种相互关系.当某个服务协定指定它需要会话时,该协定会指定所有调用(即,支持调 ...
- <转>WCF中出现死锁或者超时
WCF回调中的死锁 一.服务器端死锁 对于如下服务: [ServiceContract(CallbackContract = typeof(INotify))] public class Downlo ...
- 跟我一起学WCF(11)——WCF中队列服务详解
一.引言 在前面的WCF服务中,它都要求服务与客户端两端都必须启动并且运行,从而实现彼此间的交互.然而,还有相当多的情况希望一个面向服务的应用中拥有离线交互的能力.WCF通过服务队列的方法来支持客户端 ...
- 跟我一起学WCF(8)——WCF中Session、实例管理详解
一.引言 由前面几篇博文我们知道,WCF是微软基于SOA建立的一套在分布式环境中各个相对独立的应用进行交流(Communication)的框架,它实现了最新的基于WS-*规范.按照SOA的原则,相对独 ...
- 我的WCF之旅(3):在WCF中实现双工通信
双工(Duplex)模式的消息交换方式体现在消息交换过程中,参与的双方均可以向对方发送消息.基于双工MEP消息交换可以看成是多个基本模式下(比如请求-回复模式和单项模式)消息交换的组合.双工MEP又具 ...
- WCF技术剖析之十一:异步操作在WCF中的应用(上篇)
原文:WCF技术剖析之十一:异步操作在WCF中的应用(上篇) 按照操作执行所需的资源类型,我们可以将操作分为CPU绑定型(CPU Bound)操作和I/O绑定型(I/O Bound)操作.对于前者,操 ...
- WCF中队列服务详解
WCF中队列服务详解 一.引言 在前面的WCF服务中,它都要求服务与客户端两端都必须启动并且运行,从而实现彼此间的交互.然而,还有相当多的情况希望一个面向服务的应用中拥有离线交互的能力.WCF通过服务 ...
- WCF 服务端异常封装
通常WCF服务端异常的详细信息只有在调试环境下才暴露出来,但我目前有需求需要将一部分异常的详细信息传递到客户端,又需要保证一定的安全性. 最简单的办法当然是在服务端将异常捕获后,序列化传给客户端,但这 ...
随机推荐
- 设计模式--Singleton_(1)(C#版)
今天我们来探索一下Singleton设计模式的实现及应用场景. Singleton模式属于Creational Type(创建型)设计模式的一种.该模式一般用于确保在应用中仅创建一个某类的instan ...
- 2D Circular Geometry Kernel ( Geometry Kernels) CGAL 4.13 -User Manual
1 Introduction The goal of the circular kernel is to offer to the user a large set of functionalitie ...
- 在Asp.Net MVC中实现上传图片并显示
实现思路大概分为两步: 1. 通过上传接口,将图片上传到服务器,返回文件路径给客户端: 2. 点击保存上传,将文件路径保存到数据库,如果是多张图片,路径用逗号分隔. 核心上传代码: /// <s ...
- NGUI图集字体
UIFont里使用Symbols来指定字体时用Sprite前缀和名字自动分配的工具,前段时间工作需要时写的,具体用法有空时再写. using UnityEngine; using UnityEdito ...
- oracle 游标简单案例
oracle 游标简单案例 一.案例: DECLARE IDO NUMBER; DABH CHAR); t_count ); CURSOR TJ_CURSOR IS SELECT IDO,DABH ...
- Meteor in Action(一)起步
杜撰的名字,这个系列文章旨在记录工作中开发APP所要学习meteor的过程. 最简单的例子,运行Meteor自带的缺省的Hello world例子. 安装好Meteor后,建立一个空白目录. 然后: ...
- day72 Ajax 第一天
第一个示例:(i1+i2 ) 前端数据 <!DOCTYPE html> <html lang="en"> <head> <meta cha ...
- TOJ2470
#include <stdio.h> struct node{ int x; int y; int step; }first; int zx[4]={-1,0,1,0}; int zy[4 ...
- 638. Shopping Offers
In LeetCode Store, there are some kinds of items to sell. Each item has a price. However, there are ...
- 关于finecms v5 会员头像 任意文件上传漏洞分析
看到我私藏的一个洞被别人提交到补天拿奖金,所以我干脆在社区这里分享,给大家学习下 本文原创作者:常威,本文属i春秋原创奖励计划,未经许可禁止转载! 1.定位功能 下载源码在本地搭建起来后,正常登陆了用 ...