惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

H
Heimdal Security Blog
A
Arctic Wolf
K
Kaspersky official blog
V
Vulnerabilities – Threatpost
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Simon Willison's Weblog
Simon Willison's Weblog
L
LINUX DO - 热门话题
MongoDB | Blog
MongoDB | Blog
T
Threat Research - Cisco Blogs
D
Docker
爱范儿
爱范儿
T
Tenable Blog
C
Check Point Blog
B
Blog
C
Cisco Blogs
Vercel News
Vercel News
The Cloudflare Blog
T
Threatpost
NISL@THU
NISL@THU
T
Tor Project blog
V2EX - 技术
V2EX - 技术
P
Palo Alto Networks Blog
Application and Cybersecurity Blog
Application and Cybersecurity Blog
T
Tailwind CSS Blog
G
GRAHAM CLULEY
P
Privacy & Cybersecurity Law Blog
SecWiki News
SecWiki News
博客园 - 司徒正美
S
Security @ Cisco Blogs
GbyAI
GbyAI
S
Secure Thoughts
Microsoft Security Blog
Microsoft Security Blog
The Register - Security
The Register - Security
Recorded Future
Recorded Future
Cloudbric
Cloudbric
Webroot Blog
Webroot Blog
N
News and Events Feed by Topic
Y
Y Combinator Blog
博客园_首页
T
Troy Hunt's Blog
The Hacker News
The Hacker News
雷峰网
雷峰网
Google DeepMind News
Google DeepMind News
U
Unit 42
AWS News Blog
AWS News Blog
PCI Perspectives
PCI Perspectives
V
Visual Studio Blog
博客园 - 聂微东
有赞技术团队
有赞技术团队
酷 壳 – CoolShell
酷 壳 – CoolShell

博客园 - 蝈蝈

LAMPR Ver 1.0 发布 - 轻松搞定LAMP环境 在Windows Server下集成Apache、Tomcat和IIS Windows Workflow 学习笔记二 序语言流行度最新排行榜,C和C++正在衰落 Windows Workflow学习笔记 EAI,ESB与SOA How to Building ASP.NET AJAX Controls 使用页面Gzip压缩提速 Microsoft AJAX Library 简介 Configuration Section Designer示例 开源的内容管理系统 Windows Live Writer截屏插件 试用Windows Live Writer写博客 wcf step by step:服务的状态 wcf step by step:消息交换模式 wcf step by step:如何保护你的wcf服务 wcf step by step:改进HelloWorld程序 wcf step by step:host服务与多次Endpoint wcf step by step之HelloWorld
wcf step by step之异常处理
蝈蝈 · 2008-03-01 · via 博客园 - 蝈蝈

分布式系统的异常处理比普通的应用程序的异常处理复杂的多,wcf的服务端异常抛出后,客户端是否能catch到,catch到的是否是我们期望的,我们通过demo来一步一步分析下去。
在我们以前的demo中我们未对异常进行任何的处理,一量异常发生,我们的系统则没有能力自我保护,轻微的错误会导到当前用户的操作失败,严重的是系统崩溃。我们总是习惯对于可以预见的错误以一种友好的方式来提醒用户,而不是把系统抛出的信息暴露给客户,因为这样可能会给我们的系统带来危害,这就是异常处理的原则。
下面我们通过改造以前的demo来看wcf对异常是怎么样处理的。
一、添加异常
string GetEmpNameByID(int id)接口是获取指定id的员工姓名,当id不存在时,我们希望抛出一个异常,客户端通过catch这个异常来告诉客户当前状况。
public string GetEmpNameByID(int id)
        {
            List<Employee> emps = Database.GetAll();
            var names = from p in emps
                        where p.ID == id
                        select p.Name;
            if (names.Count() > 0)
            {
                return names.First<string>();
            }

            throw new Exception("employee no exists");
        }
抛出一个Excepion的异常是一种简单的处理办法,实际应用不应当是这样的,此处仅为演示方便。
客户端在调用GetEmpNameByID时添加try/catch(Exception ex)并把异常的类型和异常的message打印出来。运行程序后,实际上我们看到的异常信息是这样的。
异常类型:System.ServiceModel.FaultException
异常信息:由于内部错误,服务器无法处理该请求。有关该错误的详细信息,请打开服务器上的 IncludeExceptionDetailInFaults (从 ServiceBehaviorAttribute 或从 <serviceDebug> 配置行为)以便将异常信息发送回客户端,或在打开每个 Microsoft .NET Framework 3.0 SDK 文档的跟踪的同时检查服务器跟踪日志。
如果把这些信息显示给我们的用户,一点价值都没有。根据提示,我们在服务端的web.config中增加<serviceDebug>配置行为<serviceDebug includeExceptionDetailInFaults="True"/>,再次运行后。
异常类型:System.ServiceModel.FaultException`1[System.ServiceModel.ExceptionDetail]
异常信息:employee no exists
呵呵,似乎很相似,只是类型不一样。可以看来在服务端抛出的异常总会是FaultException异常类,那么我们想得到具体的异常类型如何做呢。
wcf对自定义异常提供了支持,通过定义一个异常描述的契约,如
namespace Shore.cnblogs.com.WCFData
{
    [DataContract]
    public class EmployeeNotFind
    {
        [DataMember]
        public string Code;

        [DataMember]
        public string Message;
    }
}
我们把它入到WCFData组件中,因为这也是一个数据契约。
修改服务契约
[ServiceContract]
    public interface IEmployeeService
    {
        [OperationContract]
        void AddEmployee(Employee em);

        [FaultContract(typeof(EmployeeNotFind))]
        [OperationContract]
        string GetEmpNameByID(int id);
    }
注释trow new Exception("employee no exists");
改为
            EmployeeNotFind notFind = new EmployeeNotFind();
            notFind.Code = id.ToString();
            notFind.Message = "employee no exists";
            throw new FaultException<EmployeeNotFind>(notFind);
客户端
catch (System.ServiceModel.FaultException<EmployeeNotFind> fe)
            {
                EmployeeNotFind notfind = fe.Detail;
                Console.WriteLine("exception type={0},exception description={1}", notfind.GetType().ToString(), notfind.Message);
            }

下载Demo