Learning WCF:Fault Handling
There are two types of Execptions which can be throwed from the WCF service. They are Application excepiton and Infrastructure exception.
Handle Application Exception
If we do nothing, a FaultException always be throwed when your WCF service appear any error. For example:
Service:
using Service.Interface;
using Entities;
using System;
namespace Service
{
public class PeopleService : IPeopleOperator
{
public void DeletePerson(Person person)
{
Console.WriteLine("Have deleted a person whoes name is:" + person.Name);
}
}
}
Client:
using Entities;
using Service.Interface;
using System;
using System.ServiceModel;
namespace Client
{
class Program
{
static void Main(string[] args)
{
using (ChannelFactory<IPeopleOperator> channelFactory = new ChannelFactory<IPeopleOperator>(new WSHttpBinding(),
"http://localhost:9999/PeopleService"))
{
IPeopleOperator proxy = channelFactory.CreateChannel();
Person person = null;
proxy.DeletePerson(person);
Console.Read();
}
}
}
}
When you run the Client code:

There is no useful information. We should use Fault Contract. Fault Contract allow developer define error information entity flexible.
Using Fault Contract
Create an Error message container entity
using System.Runtime.Serialization;
namespace Entities
{
[DataContract]
public class ErrorInformation
{
public ErrorInformation(string messages,
string serviceName,
string methodName)
{
Messages = messages;
ServiceName = serviceName;
MethodName = methodName;
}
[DataMember]
public string Messages { get; private set; }
[DataMember]
public string ServiceName { get; private set; }
[DataMember]
public string MethodName { get; private set; }
}
}
Apply FaultContract attribute on the operation of the contract interface
using Entities;
using System.ServiceModel;
namespace Service.Interface
{
[ServiceContract(Name = "PeopleOperatorService", Namespace ="http://zzy0471.cnblogs.com")]
public interface IPeopleOperator
{
[FaultContract(typeof(ErrorInformation))]
[OperationContract]
void DeletePerson(Person person);
}
}
Mondify the Service Code
using Service.Interface;
using Entities;
using System;
using System.ServiceModel;
namespace Service
{
public class PeopleService : IPeopleOperator
{
public void DeletePerson(Person person)
{
if (person == null)
{
var error = new ErrorInformation("参数person为null",
"PeopleService",
"DeletePerson");
throw new FaultException<ErrorInformation>(error);
}
else
{
Console.WriteLine("Have deleted a person whoes name is:" + person.Name);
}
}
}
}
Mondify the Clinet Code:
using Entities;
using Service.Interface;
using System;
using System.ServiceModel;
namespace Client
{
class Program
{
static void Main(string[] args)
{
using (ChannelFactory<IPeopleOperator> channelFactory = new ChannelFactory<IPeopleOperator>(new WSHttpBinding(),
"http://localhost:9999/PeopleService"))
{
IPeopleOperator proxy = channelFactory.CreateChannel();
Person person = null;
try
{
proxy.DeletePerson(person);
}
catch (FaultException<ErrorInformation> fe)
{
Console.WriteLine(fe.Detail.Messages + " in method " + fe.Detail.MethodName + " of service " + fe.Detail.ServiceName);
}
catch(FaultException)
{
Console.WriteLine("Unknow FaultException");
}
Console.Read();
}
}
}
}
Handle Infrastructure Exception
Except application exceptions, some Infrastructure Exceptions occasionally occur. For example: communication error, which may occur because of network unavailability, an incorrect address, the host process not running, and so on. The second type of error is related to the state of the proxy
and the channels. So We need some more catch:
catch (CommunicationException ex)
{
Console.WriteLine("Communication error:" + ex.Message);
}
catch(ObjectDisposedException ex)
{
Console.WriteLine("The proxy is closed:" + ex.Message);
}
catch (InvalidOperationException ex)
{
Console.WriteLine("InvalidOperation:" + ex.Message);
}
catch(TimeoutException ex)
{
Console.WriteLine("Timeout:" + ex.Message);
}
catch(Exception)
{
Console.WriteLine("Unknow Infrastructure Exception ");
}
Whole code of client:
using Entities;
using Service.Interface;
using System;
using System.ServiceModel;
namespace Client
{
class Program
{
static void Main(string[] args)
{
using (ChannelFactory<IPeopleOperator> channelFactory = new ChannelFactory<IPeopleOperator>(new WSHttpBinding(),
"http://localhost:9999/PeopleService"))
{
IPeopleOperator proxy = channelFactory.CreateChannel();
Person person = null;
try
{
proxy.DeletePerson(person);
}
//
// Application Exception
//
catch (FaultException<ErrorInformation> fe)
{
Console.WriteLine(fe.Detail.Messages + " in method " + fe.Detail.MethodName + " of service " + fe.Detail.ServiceName);
}
catch(FaultException)
{
Console.WriteLine("Unknow Application FaultException");
}
//
// Infrastructure Exception
//
catch (CommunicationException ex)
{
Console.WriteLine("Communication error:" + ex.Message);
}
catch(ObjectDisposedException ex)
{
Console.WriteLine("The proxy is closed:" + ex.Message);
}
catch (InvalidOperationException ex)
{
Console.WriteLine("InvalidOperation:" + ex.Message);
}
catch(TimeoutException ex)
{
Console.WriteLine("Timeout:" + ex.Message);
}
catch(Exception)
{
Console.WriteLine("Unknow Infrastructure Exception ");
}
Console.Read();
}
}
}
}
That's all.
Learning WCF:Fault Handling的更多相关文章
- Learning WCF:Life Cycle of Service instance
示例代码下载地址:WCFDemo1Day 概述 客户端向WCF服务发出请求后,服务端会实例化一个Service对象(实现了契约接口的对象)用来处理请求,实例化Service对象以及维护其生命周期的方式 ...
- Learning WCF:A Simple Demo
This is a very simple demo which can help you create a wcf applition quickly. Create a Solution Open ...
- Learning WCF Chapter1 Hosting a Service in IIS
How messages reach a service endpoint is a matter of protocols and hosting. IIS can host services ov ...
- Learning WCF Chapter1 Generating a Service and Client Proxy
In the previous lab,you created a service and client from scratch without leveraging the tools avail ...
- WCF:为 SharePoint 2010 Business Connectivity Services 构建 WCF Web 服务(第 1 部分,共 4 部分)
转:http://msdn.microsoft.com/zh-cn/library/gg318615.aspx 摘要:通过此系列文章(共四部分)了解如何在 Microsoft SharePoint F ...
- Learning WCF 书中的代码示例下载地址
Learning WCF Download Example Code 第一个压缩文件LearningWCF.zip是VS2005创建的项目,不要下载这个. 建议下载VS2008版的,以及Media
- Multiple address space mapping technique for shared memory wherein a processor operates a fault handling routine upon a translator miss
Virtual addresses from multiple address spaces are translated to real addresses in main memory by ge ...
- Learning WCF Chapter1 Exposing Multiple Service Endpoints
So far in this chapter,I have shown you different ways to create services,how to expose a service en ...
- Portal:Machine learning机器学习:门户
Machine learning Machine learning is a scientific discipline that explores the construction and stud ...
随机推荐
- 二、Jmeter脚本开发
目录 1.Jmeter协议录制 1.Jmeter协议录制 1.1 dboy进行录制 badboy下载地址:http://www.badboy.com.au/download/add badboy是一个 ...
- 同步对象(同步条件Event)
event = threading.Event() #创建同步对象 event.wait() #同步对象等待状态 event.set() #同步对象设置Trueevent.clear() ...
- Python面向对象编程(下)
本文主要通过几个实例介绍Python面向对象编程中的封装.继承.多态三大特性. 封装性 我们还是继续来看下上文中的例子,使用Student类创建一个对象,并修改对象的属性.代码如下: #-*- cod ...
- python入门学习1
实学习每一种语言,都可以找到很快乐的学习方法.有兴趣,有乐趣,才会一直想学.知道print().input().if/else就可以做一个简陋的游戏了. print() # 打印函数,将信息打印出来 ...
- Javascript中 toFixed
javascript中toFixed使用的是银行家舍入规则. 银行家舍入:所谓银行家舍入法,其实质是一种四舍六入五取偶(又称四舍六入五留双)法. 简单来说就是:四舍六入五考虑,五后非零就进一,五后为零 ...
- Ado.net之连接数据库
一.Ado.net的核心 Ado.net的核心为两组重要的类,一个负责处理软件内部的实际数据(DataSet),另一个负责与外部数据系统通信(DataProvider) DataSet:包含一个或多个 ...
- html----input标签
HTML 5 <input> 标签 定义和用法 <input> 标签规定用户可输入数据的输入字段. 根据不同的 type 属性,输入字段有多种形态.输入字段可以是文本字段.复选 ...
- 【VBA】セールの最初起動時、VBAを自動的に実行方法
方法一 セールの「ThisWorkbook」にて.「Workbook」の「Open」関数にて.ロジックを追加する Private Sub Workbook_Open() Msgbox "He ...
- FortiGate日常检查
1.1)CPU利用率:由于防火墙有芯片,正常的流量都走芯片转发,不走cpu,只有开了utm相关的应用层防护功能和DDOS之类的,才会走cpu,所以一般cpu都不会占用太多,甚至很多时间都是0%, 如果 ...
- Mysql的时间类型问题
时间类型有time, date, datetime, timestamp 如Mysql官方文档所述: time 没有date,date没有time,datetime是date和time的集合, 而ti ...