WCF 学习笔记之异常处理

1:WCF异常在配置文件

<configuration>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="serviceDebuBehavior">
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors> <services>
<service name="Artech.WcfServices.Service.CalculatorService" behaviorConfiguration="serviceDebuBehavior">
<endpoint address="http://127.0.0.1:3721/calculatorservice"
binding="ws2007HttpBinding"
contract="Artech.WcfServices.Service.Interface.ICalculator" />
</service>
</services>
</system.serviceModel>
</configuration>

2:也可以直接在服务上直接用特性进行设定

[ServiceBehavior(IncludeExceptionDetailInFaults=true)]

public class CalculatorService:ICalculator

{

}

上面两种方式实现的效果是一样的;

3:自定义异常信息

(1)直接通过FaultException直接指定错误的信息

using System.ServiceModel;
namespace Artech.WcfServices.Service
{
public class CalculatorService : ICalculator
{
public int Divide(int x, int y)
{
if (0 == y)
{
throw new FaultException("被除数y不能为零!");
}
return x / y; }
}
}

相应的配置文件内容为:

<configuration>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="serviceDebuBehavior">
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors> <services>
<service name="Artech.WcfServices.Service.CalculatorService" behaviorConfiguration="serviceDebuBehavior">
<endpoint address="http://127.0.0.1:3721/calculatorservice"
binding="ws2007HttpBinding"
contract="Artech.WcfServices.Service.Interface.ICalculator" />
</service>
</services>
</system.serviceModel>
</configuration>

(2)通过FaultException<TDetail>采用自定义类型封装错误

首先我们看一下一个实例;注意Interface层里的CalculaltionError.cs这个是自定义封装的错误类;

CalculaltionError.cs类代码如下:它是一个数据契约

using System.Text;
using System.Runtime.Serialization; namespace Artech.WcfServices.Service.Interface
{
[DataContract]
public class CalculationError
{
public CalculationError(string operation, string message)
{
this.Operation = operation;
this.Message = message;
}
[DataMember]
public string Operation { get; set; }
[DataMember]
public string Message { get; set; }
} }

契约里的定义如下:为了确保错误细节对象能够被正常序列化和反序列化,要按照如下的方式通过FaultContractAttribute特性为操作定义基于CalculationError类型的错误契约(错误契约的一些注意点在下面会讲到);

namespace Artech.WcfServices.Service.Interface
{
[ServiceContract(Namespace = "http://www.artech.com/")]
public interface ICalculator
{
[OperationContract]
[FaultContract(typeof(CalculationError))]
int Divide(int x, int y);
}
}

服务实现里直接抛出一个FaultException<CalculationError>,并创建一个CalculationError对象作为该异常对象的细节;

using System.ServiceModel;
namespace Artech.WcfServices.Service
{
public class CalculatorService : ICalculator
{
public int Divide(int x, int y)
{
if (0 == y)
{
var error = new CalculationError("Divide", "被除数y不能为零!");
throw new FaultException<CalculationError>(error, error.Message);
}
return x / y;
}
}
}

服务实现的配置文件内容如下:

<configuration>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="serviceDebuBehavior">
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors> <services>
<service name="Artech.WcfServices.Service.CalculatorService" behaviorConfiguration="serviceDebuBehavior">
<endpoint address="http://127.0.0.1:3721/calculatorservice"
binding="ws2007HttpBinding"
contract="Artech.WcfServices.Service.Interface.ICalculator" />
</service>
</services>
</system.serviceModel>
</configuration>

客户端调用相关的异常处理;获得FaultException<CalculationError>类型的异常

using System.ServiceModel;
using Artech.WcfServices.Service.Interface;
namespace Artech.WcfServices.Clients
{
class Program
{
static void Main(string[] args)
{
using (ChannelFactory<ICalculator> channelFactory = new ChannelFactory<ICalculator>("calculatorservice"))
{
ICalculator calculator = channelFactory.CreateChannel();
using (calculator as IDisposable)
{
try
{
int result = calculator.Divide(1, 0);
}
catch (FaultException<CalculationError> ex)
{
Console.WriteLine("运算错误");
Console.WriteLine("运算操作:{0}", ex.Detail.Operation);
Console.WriteLine("错误消息: {0}", ex.Detail.Message);
(calculator as ICommunicationObject).Abort();
}
}
}
Console.Read(); }
} }

客户端配置信息如下:

<configuration>
<system.serviceModel>
<client>
<endpoint name="calculatorservice"
address= "http://127.0.0.1:3721/calculatorservice"
binding="ws2007HttpBinding"
contract="Artech.WcfServices.Service.Interface.ICalculator"/>
</client>
</system.serviceModel>
</configuration>

*接下来讲解关于错误契约的注意点:

对于错误契约的运用不仅仅在将自定义的错误细节类型应用到服务契约相应操作上时才要显式地在操作方法上应用FaultContracAttribute特性;对于一些基元类型(比如Int32, String等)也要这么做;

    public class CalculatorService : ICalculator
{
public int Divide(int x, int y)
{
if (0 == y)
{
throw new FaultException<String>("不能为0");
}
return x / y;
}
}

契约如下:

namespace Artech.WcfServices.Service.Interface
{
[ServiceContract]
public interface ICalculator
{
[OperationContract]
[FaultContract(typeof(string))]
int Divide(int x, int y);
}
}

当然它是可以多次声明针对多个异常的处理

namespace Artech.WcfServices.Service.Interface
{
[ServiceContract]
public interface ICalculator
{
[OperationContract]
[FaultContract(typeof(CalculationError))]
[FaultContract(typeof(string))]
int Divide(int x, int y);
}
}

若是两个是相同类型的则要增加Name 或者Namespace来区别开;若是Name一样类型不一样同样会报错;

    [ServiceContract]
public interface ICalculator
{
[OperationContract]
[FaultContract(typeof(CalculationError),Name="CalualationError")]
[FaultContract(typeof(CalculationError),Name="CalualationException")]
int Divide(int x, int y);
}

(4)通过XmlSerializer对错误细节对象进行序列化([XmlSerializerFormat(SupportFaults=true)])因为WCF默认是采用序列化器是DataContractSerializer(WCF提供的两种序列化器DataContractSerializer 和 XmlSerializer)

    [ServiceContract]
public interface ICalculator
{
[OperationContract]
[FaultContract(typeof(CalculationError))]
[XmlSerializerFormat(SupportFaults=true)]
int Divide(int x, int y);
}

封装的错误类如下:

    [Serializable]
public class CalculationError
{
[XmlAttributeAttribute("op")]
public string Operation { get; set; }
[XmlElement("Error")]
public string Message { get; set; }
}
 
 
 
标签: WCF

WCF 学习笔记之异常处理的更多相关文章

  1. WCF学习笔记之事务编程

    WCF学习笔记之事务编程 一:WCF事务设置 事务提供一种机制将一个活动涉及的所有操作纳入到一个不可分割的执行单元: WCF通过System.ServiceModel.TransactionFlowA ...

  2. WCF学习笔记之传输安全

    WCF学习笔记之传输安全 最近学习[WCF全面解析]下册的知识,针对传输安全的内容做一个简单的记录,这边只是简单的记录一些要点:本文的内容均来自[WCF全面解析]下册: WCF的传输安全主要涉及认证. ...

  3. WCF 学习笔记之双工实现

    WCF 学习笔记之双工实现 其中 Client 和Service为控制台程序 Service.Interface为类库 首先了解契约Interface两个接口 using System.Service ...

  4. Python学习笔记之异常处理

    1.概念 Python 使用异常对象来表示异常状态,并在遇到错误时引发异常.异常对象未被捕获时,程序将终止并显示一条错误信息 >>> 1/0 # Traceback (most re ...

  5. WCF学习笔记(2)——使用IIS承载WCF服务

    通过前面的笔记我们知道WCF服务是不能独立存在,必须“寄宿”于其他的应用程序中,承载WCF服务的应用程序我们称之为“宿主”.WCF的多种可选宿主,其中比较常见的就是承载于IIS服务中,在这里我们来学习 ...

  6. WCF学习笔记1--发布使用配置文件的服务

    关于WCF的入门网上资料很多,可以参考蒋金楠老师的博客http://www.cnblogs.com/artech/archive/2007/02/26/656901.html,我是从这篇博客开始学习的 ...

  7. WCF学习笔记(1)——Hello WCF

    1.什么是WCF Windows Communication Foundation(WCF)是一个面向服务(SOA)的通讯框架,作为.NET Framework 3.0的重要组成部分于2006年正式发 ...

  8. WCF学习笔记(基于REST规则方式)

    一.WCF的定义 WCF是.NET 3.0后开始引入的新技术,意为基于windows平台的通讯服务. 首先在学习WCF之前,我们也知道他其实是加强版的一个面向服务(SOA)的框架技术. 如果熟悉Web ...

  9. [WCF学习笔记] 我的WCF之旅(1):创建一个简单的WCF程序

    近日学习WCF,找了很多资料,终于找到了Artech这个不错的系列.希望能从中有所收获. 本文用于记录在学习和实践WCF过程中遇到的各种基础问题以及解决方法,以供日后回顾翻阅.可能这些问题都很基础,可 ...

随机推荐

  1. 将字符串“abc”全排列成:abc、acb、bac、bca、cab、cba

     [STAThread]         static void Main()         {             string s = "abcd";           ...

  2. Asp.Net MVC5入门学习系列①

    原文:Asp.Net MVC5入门学习系列① 现在直接开始MVC5的学习系列,学习资源来自Micrsoft. 开始使用Asp.Net MVC 5 打开Visual Studio 2013,然后新建一个 ...

  3. Hibernate 之强大的HQL查询

    Hibernate  配备了一种非常强大的查询语言,这种语言看上去很像  SQL.但是不要被语法结构上的相似所迷惑,HQL  是非常有意识的被设计为完全面向对象的查询,它可以理解如继承.多态和关联之类 ...

  4. js页码生成库,一个适合前后端分离的页码生成器

    原文:js页码生成库,一个适合前后端分离的页码生成器 前言 上星期写的任务里面有需要进行分页的处理,git搜索了一番,没有觉得合适的,于是自己临时写了个分页的算法. 然后等闲下来的时候,决定把分页进行 ...

  5. 且看三星刚发布的Smart TV如何窃听你的枕边细语

    三星最新的SmartTV有一个很酷的新的声控功能,网络连接设备可以通过它来录下你说过的所有内容并把它上传到一个第三方的地方进行存储. 该公司的语音识别软件允许用户跟他们的电视通过声音来进行沟通.一旦电 ...

  6. Android设备连接Unity Profiler性能分析器

    Unity提供两种方式让Developer的Android设备连接Profiler进行性能分析: 1.通过wifi,Android设备和计算机处于同一个Wlan中. 2.通过USB ADB 普通情况我 ...

  7. 探秘IntelliJ IDEA v13的应用服务器

    原文:探秘IntelliJ IDEA v13的应用服务器 IntelliJ IDEA v13应用out-of-the-box支持众多企业级和开源的服务器,包括:GlassFish.WebLogic. ...

  8. SQL Server相似度比较函数

    原文:SQL Server相似度比较函数 相似度函数 概述    最近有人问到关于两个字段求相似度的函数,所以就写了一篇关于相似度的函数,分别是“简单的模糊匹配”,“顺序匹配”,“一对一位置匹配”.在 ...

  9. [探索]点点轻博客搬家到WordPress(一)

    摘要:点点博客备份XML通过DiandianToWordpress-beta.sh(文末给出)搬家到Wordpress博客 本人曾使用过点点轻博客,也深知像点点博客,Lofter博客导出的XML文件不 ...

  10. 1cocos2dx扩展UI控制,CCControlSlider,CCScale9Sprite(九妹图。),CCControlSwitch,CCControlButton

     UI控件来自cocos2dx的扩展库.完好了UI方面的元素,使cocos2dx更加丰富多彩.使用扩展库需包括: #include "cocos-ext.h" USING_NS ...