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 ...
随机推荐
- Python协程、异步IO
本节内容 Gevent协程 Select\Poll\Epoll异步IO与事件驱动 Python连接Mysql数据库操作 RabbitMQ队列 Redis\Memcached缓存 Paramiko SS ...
- HTTP协议规定,客户端的编写
HTTP协议是网络应用层协议,建立在TCP/IP协议基础上.HTTP协议基于客户/服务器模式,客户端主动发出HTTP请求,服务器接收HTTP请求,返回HTTP响应结果.HTTP协议对HTTP请求,以及 ...
- 操作系统切换CPU的方式
操作系统切换CPU的方式 1 IO等待切换. 2 时间轮询切换,也就是如果没有IO等待的情况下,就会有时间轮询切换,不让CPU一直处理一个任务 CPU的处理速度是纳秒级别的,所有我们可以同时听歌, ...
- Python3 timeit的用法
Python3中的timeit模块可以用来测试小段代码的运行时间 其中主要通过两个函数来实现:timeit和repeat,代码如下: def timeit(stmt="pass", ...
- js 冒泡事件 点击任意地方隐藏元素
$(function () { $("#but").click(function (e) {// $();//显示速度 /*阻止冒泡事件*/ e = window.event || ...
- 小强学渲染之OpenGL的GPU管线
GPU渲染流水线,是硬件真正体现渲染概念的操作过程,也是最终将图元画到2D屏幕上的阶段.GPU管线涵盖了渲染流程的 几何阶段 和 光栅化阶段,但对开发者而言,只有对顶点和片段着色器有可编程控制权,其他 ...
- php面试题五之nginx如何调用php和php-fpm的作用和工作原理
nginx如何调用php 采用nginx+php作为webserver的架构模式,在现如今运用相当广泛.然而第一步需要实现的是如何让nginx正确的调用php.由于nginx调用php并不是如同调用一 ...
- FortiGate日常检查
1.1)CPU利用率:由于防火墙有芯片,正常的流量都走芯片转发,不走cpu,只有开了utm相关的应用层防护功能和DDOS之类的,才会走cpu,所以一般cpu都不会占用太多,甚至很多时间都是0%, 如果 ...
- 23. pt-slave-delay
略过,用原生的延迟复制: stop slave; change master to master_delay=5; start slave;
- Java 正则表达式之捕获组
Java 正则表达式之捕获组 1. Java 正则表达式基础 2. Java 正则表达式之捕获组 一.概述 1.1 什么是捕获组 捕获组就是把正则表达式中子表达式匹配的内容,保存到内存中以数字编号或显 ...