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 ...
随机推荐
- Android APK反编译(一)
apk是安卓工程打包的最终形式,将apk安装到手机或者模拟器上就可以使用APP.反编译apk则是将该安卓工程的源码.资源文件等内容破解出来进行分析. 一.APK反编译基本原理 1.APK分析 apk文 ...
- Apache Struts2高危漏洞(S2-057CVE-2018-11776)
花了两天时间,特此记录 一:背景: 2018年8月22日,Apache Strust2发布最新安全公告,Apache Struts2存在远程代码执行的高危漏洞. 二:漏洞产生原理: 1.需要知道对应跳 ...
- Java入门到精通第4版汇总
- K8s部署使用CFSSL创建证书
证书的编码格式 PEM(Privacy Enhanced Mail),通常用于数字证书认证机构(Certificate Authorities,CA),扩展名为.pem, .crt, .cer, 和 ...
- Windows 10 系统,配置jdk系统环境变量
1. 下载jdk包,下载路径为:http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html, ...
- 多层josn数据 修改
var aa = { a: 1, b: { c: 1, d: 1 }, e: [{ f: 1, g: 2 }, { h: 1, i: { j: 3, k: [{ l: 55, m: [1, 2, 3, ...
- Linux firewalld使用教程+rhce课程实验
--timeout= 设置规则生效300秒 调试阶段使用,防止规则设置错误导致无法远程连接 实验:在server0机器上部署httpd服务,通过添加富规则,只允许172.25.0.10/32访问,并且 ...
- 通过DOS界面查看电脑上端口使用情况
如何查看查看端口是否被占用? 打开电脑上的运行,输入cmd,进入DOS界面. 然后输入 netstat -an 即可显示电脑上所用的端口使用情况! 状态显示 LISTENING就表 ...
- [leetcode]49. Group Anagrams变位词归类
Given an array of strings, group anagrams together. Example: Input: ["eat", "tea" ...
- 分布式Snowflake雪花算法
前言 项目中主键ID生成方式比较多,但是哪种方式更能提高的我们的工作效率.项目质量.代码实用性以及健壮性呢,下面作了一下比较,目前雪花算法的优点还是很明显的. 优缺点比较 UUID(缺点:太长.没法排 ...