我的WCF之旅(10):如何在WCF进行Exception Handling-程序员宅基地

技术标签: 网络  runtime  

在任何Application的开发中,对不可预知的异常进行troubleshooting时,异常处理显得尤为重要。对于一般的.NET系统来说,我们简单地借助try/catch可以很容易地实现这一功能。但是对于 一个分布式的环境来说,异常处理就没有那么简单了。按照面向服务的原则,我们把一些可复用的业务逻辑以Service的形式实现,各个Service处于一个自治的环境中,一个Service需要和另一个Service进行交互,只需要获得该Service的描述(Description)就可以了(比如WSDL,Schema和Strategy)。借助标准的、平台无关的通信构架,各个Service之间通过标准的Soap Message进行交互。Service Description、Standard Communication Infrastructure、Soap Message based Communication促使各Service以松耦合的方式结合在一起。但是由于各个Service是自治的,如果一个Service调用另一个Service,在服务提供方抛出的Exception必须被封装在Soap Message中,方能被处于另一方的服务的使用者获得、从而进行合理的处理。下面我们结合一个简单的Sample来简单地介绍我们可以通过哪些方式在WCF中进行Exception Handling。

一、传统的Exception Handling

我们沿用我们一直使用的Calculator的例子和简单的4层构架:


1.    Service Contract- Artech.ExceptionHandling.Contract

using System;
using System.Collections.Generic;
using System.Text;
using System.ServiceModel;

namespace Artech.ExceptionHandling.Contract
ExpandedBlockStart.gif {
    [ServiceContract]
     public  interface ICalculator
ExpandedSubBlockStart.gif    {
        [OperationContract]
         double Divide( double x,  double y);
    }
}

定义了一个单一的进行除法运算的Operation。

2.    Service:Artech.ExceptionHandling.Service. CalculatorService

using System;
using System.Collections.Generic;
using System.Text;
using Artech.ExceptionHandling.Contract;

namespace Artech.ExceptionHandling.Service
ExpandedBlockStart.gif {
     public  class CalculatorService:ICalculator
ExpandedSubBlockStart.gif    {
ContractedSubBlock.gif         ICalculator Members
    }
}

如果被除数是零,抛出一个DivideByZeroException Exception。

3.    Service Hosting

Configuration:

<? xml version="1.0" encoding="utf-8"  ?>
< configuration >
     < system .serviceModel >
         < behaviors >
             < serviceBehaviors >
                 < behavior  name ="calculatorServiceBehavior" >
                     < serviceMetadata  httpGetEnabled ="true"   />
                 </ behavior >
             </ serviceBehaviors >
         </ behaviors >
         < services >
             < service  behaviorConfiguration ="calculatorServiceBehavior"  name ="Artech.ExceptionHandling.Service.CalculatorService" >
                 < endpoint  binding ="basicHttpBinding"  bindingConfiguration =""  contract ="Artech.ExceptionHandling.Contract.ICalculator"   />
                 < host >
                     < baseAddresses >
                         < add  baseAddress ="http://localhost:8888/Calculator"   />
                     </ baseAddresses >
                 </ host >
             </ service >
         </ services >
     </ system.serviceModel >
</ configuration >

Program

using System;
using System.Collections.Generic;
using System.Text;
using System.ServiceModel;
using Artech.ExceptionHandling.Service;

namespace Artech.ExceptionHandling.Hosting
ExpandedBlockStart.gif {
     class Program
ExpandedSubBlockStart.gif    {
         static  void Main( string[] args)
ExpandedSubBlockStart.gif        {
             using (ServiceHost calculatorHost =  new ServiceHost( typeof(CalculatorService)))
ExpandedSubBlockStart.gif            {
                calculatorHost.Opened +=  delegate
ExpandedSubBlockStart.gif                {
                    Console.WriteLine("The Calculator service has begun to listen via the address:{0}", calculatorHost.BaseAddresses[0]);
                };
                calculatorHost.Open();
                Console.Read();
            }
        }
    }
}

4.    Client

Configuration:

<? xml version="1.0" encoding="utf-8"  ?>
< configuration >
     < system .serviceModel >
           < client >
             < endpoint  address =http://localhost:8888/Calculator  binding ="basicHttpBinding"  contract ="Artech.ExceptionHandling.Contract.ICalculator"
                name
="defualtEndpoint"   />
         </ client >
     </ system.serviceModel >
</ configuration >

Program

using System;
using System.Collections.Generic;
using System.Text;
using Artech.ExceptionHandling.Contract;
using System.ServiceModel;

namespace Artech.ExceptionHandling.Client
ExpandedBlockStart.gif {
     class Program
ExpandedSubBlockStart.gif    {
         static  void Main( string[] args)
ExpandedSubBlockStart.gif        {
            ChannelFactory<ICalculator> calculatorFactory =  new ChannelFactory<ICalculator>("defualtEndpoint");
            ICalculator calculator = calculatorFactory.CreateChannel();
             try
ExpandedSubBlockStart.gif            {
                Console.WriteLine("Try to invoke Divide method");
                Console.WriteLine("x / y =  {2} when x = {0} and y = {1}", 2, 0, calculator.Divide(2,0));
            }
             catch (Exception ex)
ExpandedSubBlockStart.gif            {
                Console.WriteLine("An Exception is thrown.\n\tException Type:{0}\n\tError Message:{1}", ex.GetType(), ex.Message);
            }
            Console.Read();
        }
    }
}

把Service调用放在一个try/catch block中,看看Service端抛出的DivideByZeroException Exception能否被Catch。

我们运行这个程序,看看Client有怎样的输出:


我们发现Client catch住的不是我们Service端真正抛出的DivideByZeroException Exception,而是一个比较General的FaultException。Error message也是很general:

"The server was unable to process the request due to an internal error. For more information about the error, either turn on IncludeExceptionDetailInFaults (either from ServiceBehaviorAttribute or from the <serviceDebug> configuration behavior) on the server in order to send the exception information back to the client, or turn on tracing as per the Microsoft .NET Framework 3.0 SDK documentation and inspect the server trace logs."

二、基于ServiceDebug的Exception Handling

很显然Client端Catch住的Exception对我们进行troubleshooting。为了利于我们进行有效的Debug,WCF提供了ServiceDebug Service Behavior。我们通过includeExceptionDetailInFaults属性设为true,那么如果Service抛出Exception,WCF会简单得包装这个Exception并把它置于Soap中Response到Service的访问者。介于此,我修改了Hosting的Configuration:

<? xml version="1.0" encoding="utf-8"  ?>
< configuration >
     < system .serviceModel >
         < behaviors >
             < serviceBehaviors >
                 < behavior  name ="calculatorServiceBehavior" >
                     < serviceMetadata  httpGetEnabled ="true"   />
                     < serviceDebug  includeExceptionDetailInFaults ="true"   />
                 </ behavior >
             </ serviceBehaviors >
         </ behaviors >
         < services >
             < service  behaviorConfiguration ="calculatorServiceBehavior"  name ="Artech.ExceptionHandling.Service.CalculatorService" >
                 < endpoint  binding ="basicHttpBinding"  bindingConfiguration =""  contract ="Artech.ExceptionHandling.Contract.ICalculator"   />
                 < host >
                     < baseAddresses >
                         < add  baseAddress ="http://localhost:8888/Calculator"   />
                     </ baseAddresses >
                 </ host >
             </ service >
         </ services >
     </ system.serviceModel >
</ configuration >

现在再次运行程序,看看现在的运行结果:


可以看到我们我们Catch的是一个FaultException< ExceptionDetail>Type的Exception,不是原来的FaultException。该Exception的Detail属性就是Service抛出的DivideByZeroException Exception。有兴趣的人可以自己测试一下。而且我们在Service端指定的Error Message也被Client获得。这种方式的Exception Handling方式确实比上面一种具有很强的指示性,对我们进行Debug确实很有帮助。但是这种方式确实不能正式用于我们最终发布的版本中,因为它会把Exception所有的信息返回到Client端,很容易泄露一些很敏感的信息。这也正是WCF把这个列入ServiceDebug Service Behavior的原因。

三、基于Fault Contract 的Exception Handling

既然上面通过定制ServiceDebug只能用于Debug阶段。我们必须寻求另外一种Exception Handling的方式。那就是我们现在将要介绍的基于FaultContract的解决方案。我们知道WCF采用一种基于Contract,Contract定义了进行交互的双方进行消息交换所遵循的准则和规范。Service Contract定义了包含了所有Operation的Service的接口,Data Contract定义了交互的数据的结构,而FaultContract实际上定义需要再双方之间进行交互的了异常、错误的表示。我们现在来看看如何来使用基于FaultContract的Exception Handling。

我们首先来定义一个表示Fault的类:MathError。考虑到这个类需要在Service 和Client使用,我把它定义在Artech.ExceptionHandling.Contract中:

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Serialization;

namespace Artech.ExceptionHandling.Contract
ExpandedBlockStart.gif {
    [DataContract]
    public   class MathError
ExpandedSubBlockStart.gif    {
         private  string _operation;
         private  string _errorMessage;

        public MathError( string operation,  string errorMessage)
ExpandedSubBlockStart.gif       {
            this._operation = operation;
            this._errorMessage = errorMessage;
       }

        [DataMember]
         public  string Operation
ExpandedSubBlockStart.gif        {
ExpandedSubBlockStart.gif             get {  return _operation; }
ExpandedSubBlockStart.gif             set { _operation = value; }
        }

       [DataMember]
         public  string ErrorMessage
ExpandedSubBlockStart.gif        {
ExpandedSubBlockStart.gif             get {  return _errorMessage; }
ExpandedSubBlockStart.gif             set { _errorMessage = value; }
        }
    }
}

在MathError中定义了两个成员:表示出错操作的Operation和出错信息的ErrorMessage。由于该类的对象需要在Endpoint之间传递,所以必须是可序列化的,在WCF中,我们一般用两个不同的Serializer实现Object和XML的Serialization和Deserialization:Datacontract Serializer和XML Serializer。而对于Fault,只能使用前者。

定义了MathError,我们需要通过FaultContract将其运用到Service Contract中制定的Operation上面,我们通过下面的方式来实现:

using System;
using System.Collections.Generic;
using System.Text;
using System.ServiceModel;

namespace Artech.ExceptionHandling.Contract
ExpandedBlockStart.gif {
    [ServiceContract]
     public  interface ICalculator
ExpandedSubBlockStart.gif    {
        [OperationContract]
        [FaultContract( typeof(MathError))]
         double Divide( double x,  double y);
    }
}

我们在Divide上运用了FaultContract,并指定了封装了Fault对应的类型,那么最终这个基于MathError类型的FaultContract会被写入Service Description中,Client通过获取该Service Description(一般是获取WSDL),它就被识别它,就会将从接收到的Soap中对该Fault的XML Mapping到具体的MathError类型。

接着我们在Service Implementation中以抛出Exception的方式植入这个MathError对象:

using System;
using System.Collections.Generic;
using System.Text;
using Artech.ExceptionHandling.Contract;
using System.ServiceModel;

namespace Artech.ExceptionHandling.Service
ExpandedBlockStart.gif {
     public  class CalculatorService:ICalculator
ExpandedSubBlockStart.gif    {
ContractedSubBlock.gif         ICalculator Members
    }
}

在被除数为0的时候,抛出FaultException<MathError> Exception,并指定具体的MathError对象,以及一个FaultCode(一般指明出错的来源)和FaultReason(出错的原因)。

我们现在先不修改Client的Exception Handling的相关代码,先运行Hosting,看看WSDL中什么特别之处:


通过上面的Screenshot,我们可以看到,在PortType section中的Divide Operation定义了Message为tns:ICalculator_Divide_MathErrorFault_FaultMessage 的<wsdl:fault>节点。通过查看Message Section,我们发现tns:ICalculator_Divide_MathErrorFault_FaultMessage的Element为q1:MathError,该q1:MathError type实际上是被定义在一个XSD中,其Uri为http://localhost:8888/Calculator?xsd=xsd2,我们定义的所有DataContract都在其中,下面的整个内容:

<? xml version="1.0" encoding="utf-8" ?>
< xs:schema  elementFormDefault ="qualified"  targetNamespace ="http://schemas.datacontract.org/2004/07/Artech.ExceptionHandling.Contract"  xmlns:xs ="http://www.w3.org/2001/XMLSchema"  xmlns:tns ="http://schemas.datacontract.org/2004/07/Artech.ExceptionHandling.Contract" >
   < xs:complexType  name ="MathError" >
     < xs:sequence >
       < xs:element  minOccurs ="0"  name ="ErrorMessage"  nillable ="true"  type ="xs:string" />
       < xs:element  minOccurs ="0"  name ="Operation"  nillable ="true"  type ="xs:string" />
     </ xs:sequence >
   </ xs:complexType >
   < xs:element  name ="MathError"  nillable ="true"  type ="tns:MathError" />
</ xs:schema >

弄清楚了Fault在WSDL中表示后,我们来修改我们Client端的代码,来有效地进行Exception Handling:

static  void Main( string[] args)
ExpandedBlockStart.gif         {
            ChannelFactory<ICalculator> calculatorFactory =  new ChannelFactory<ICalculator>("defualtEndpoint");
            ICalculator calculator = calculatorFactory.CreateChannel();
             try
ExpandedSubBlockStart.gif            {
                Console.WriteLine("Try to invoke Divide method");
                Console.WriteLine("x / y =  {2} when x = {0} and y = {1}", 2, 0, calculator.Divide(2, 0));
            }
             catch (FaultException<MathError> ex)
ExpandedSubBlockStart.gif            {
                MathError error = ex.Detail;
                Console.WriteLine("An Fault is thrown.\n\tFault code:{0}\n\tFault Reason:{1}\n\tOperation:{2}\n\tMessage:{3}", ex.Code, ex.Reason, error.Operation, error.ErrorMessage);
            }

             catch (Exception ex)
ExpandedSubBlockStart.gif            {
                Console.WriteLine("An Exception is thrown.\n\tException Type:{0}\n\tError Message:{1}", ex.GetType(), ex.Message);
            }
            Console.Read();
        }

下面是运行后的输出结果:


WCF相关内容:
[原创]我的WCF之旅(1):创建一个简单的WCF程序
[原创]我的WCF之旅(2):Endpoint Overview
[原创]我的WCF之旅(3):在WCF中实现双向通信(Bi-directional Communication)
[原创]我的WCF之旅(4):WCF中的序列化(Serialization)- Part I
[原创]我的WCF之旅(4):WCF中的序列化(Serialization)- Part II
[原创]我的WCF之旅(5):Service Contract中的重载(Overloading)
[原创]我的WCF之旅(6):在Winform Application中调用Duplex Service出现TimeoutException的原因和解决方案
[原创]我的WCF之旅(7):面向服务架构(SOA)和面向对象编程(OOP)的结合——如何实现Service Contract的继承
[原创]我的WCF之旅(8):WCF中的Session和Instancing Management
[原创]我的WCF之旅(9):如何在WCF中使用tcpTrace来进行Soap Trace
[原创]我的WCF之旅(10): 如何在WCF进行Exception Handling
[原创]我的WCF之旅(11):再谈WCF的双向通讯-基于Http的双向通讯 V.S. 基于TCP的双向通讯

[原创]我的WCF之旅(12):使用MSMQ进行Reliable Messaging
[原创]我的WCF之旅(13):创建基于MSMQ的Responsive Service


作者:蒋金楠
微信公众账号:大内老A
微博: www.weibo.com/artech
如果你想及时得到个人撰写文章以及著作的消息推送,或者想看看个人推荐的技术资料,可以扫描左边二维码(或者长按识别二维码)关注个人公众号(原来公众帐号 蒋金楠的自媒体将会停用)。
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_34309543/article/details/90334699

智能推荐

大学生网页作业设计HTML,库里网页_库里的html动态网页-程序员宅基地

文章浏览阅读1.3k次,点赞3次,收藏11次。大学生网页代码,静态动态都可以做,这是我随手做的球星库里的网页_库里的html动态网页

SVN命令行基本操作_svn命令 显示修改列表-程序员宅基地

文章浏览阅读467次。原文地址:http://blog.csdn.net/hekunhotmail/article/details/9302541以下是我常用到的svn 命令行,整理了一下,这东西版本控制上很有用,所以不会不行,当然,你也可以选择一些svn图形界面的工具0 查看当前工作目录svn 状态简写:svn st1、将文件checkout到本地目录svn checkout path(path是_svn命令 显示修改列表

mysql decimal 整数_MySQL数据类型DECIMAL用法详解-程序员宅基地

文章浏览阅读834次。MySQLDECIMAL数据类型用于在数据库中存储精确的数值。我们经常将DECIMAL数据类型用于保留准确精确度的列,例如会计系统中的货币数据。要定义数据类型为DECIMAL的列,请使用以下语法:column_name DECIMAL(P,D);在上面的语法中:P是表示有效数字数的精度。P范围为1〜65。D是表示小数点后的位数。D的范围是0~30。MySQL要求D小于或等于(<=)P。DEC..._mysql decimal 保存成整数

下载安装 gSoap_gsoap download-程序员宅基地

文章浏览阅读750次。文章目录一、官网1. Products (产品介绍)2. Dev Center (开发者中心)(1)Getting Started(入门教程)(2)Tutorials(教程)(3)Documentation(文档)(4)Download & Help(下载和帮助)总结一、官网官网地址是 https://www.genivia.com/,主页很简单。接下来看一下主要内容。1. Products (产品介绍)最先进的C/XC++自动编码工具是XML Web服务API和其他XML应用程序,是_gsoap download

设备驱动模型中设备的init_name成员_struct device init_name-程序员宅基地

文章浏览阅读796次。现象是:依次加载了总线bus,设备device,驱动driver,在加载驱动时候出现了segmentation fault。环境条件:采用的是国嵌的教材(可能是教材比较古老了吧,导致出现这样问题),内核版本使用的是2.6.32.2。最后查找原因是由于空指针导致了在strncmp产生了段错误。_struct device init_name

快速玩转 Llama2!机器学习 PAI 最佳实践(二)—全参数微调训练_llama 全参数训练-程序员宅基地

文章浏览阅读1.1k次。本实践将采用阿里云机器学习平台PAI-DSW模块针对 Llama-2-7B-Chat 进行全参数微调。PAI-DSW是交互式建模平台,该实践适合需要定制化微调模型,并追求模型调优效果的开发者。_llama 全参数训练

随便推点

pandas基础学习-程序员宅基地

文章浏览阅读27次。作为参数,0表示第一行,1表示第2行,以此类推。DataFrame意为数据框架,是Pandas库中的一种数据结构,类似于二维表,由行和列组成,与Series一样支持多种数据类型。loc属性,以列名(columns)和行名(index)作为参数,当只有一个参数时,默认是行名,即抽取整行数据,包括所有列。修改行标题,使用DataFrame对象的index属性直接赋值,,或者使用DataFrame对象的rename方法修改行标题。缺失值的处理方式有不处理、删除、填充或替换、插值(均值、中位数、众数等填补)_pandas基础

关于将android项目发布到jcenter的最新最全说明_publication not found-程序员宅基地

文章浏览阅读3.1k次。方式一 一些坑提醒 项目中含有中文,那么请全局设置utf-8编码或者,使用另一种方式生成JavaDoc(可以仔细看下方生成JavaDoc方式,不一样的) 上传的库的名字,是和你Module的名字是一样的!!!所以你的Module叫什么,你的gradle依赖路径就是: groupId:moduleName:publishVersion 下面的步骤只是将项目提交到Maven里面,你要在提交完成后_publication not found

RTKLIB中ppp代码解析_rtklib进行ppp解算-程序员宅基地

文章浏览阅读1.1k次,点赞21次,收藏25次。v= zk - hx 在代码中对应的是v[nv]=y-(r+cdtr-CLIGHT*dts[i*2]+dtrp+C*dion+dcb+bias);9) ppp_res(9,obs,n,rs,dts,var,svh,dr,exc,nav,xp,rtk,v,H,R,azel)) 模糊度固定的残差校验。kalman方程中的测量xk = xk/k-1 +kv 以及pk = pk/k-1 - kh*pk/k-1 状态更新。待估参数的预测,对应klaman公式中的xk/k-1、 Pk/k-1。_rtklib进行ppp解算

learn-SVG学习_如何训练svg标注数据集-程序员宅基地

文章浏览阅读60次。SVG 是可缩放矢量图形的缩写,它是一种用于描述二维矢量图形的XML标记语言。与传统的栅格图像不同,SVG图像可以无限缩放而不会失真,同时也支持交互和动画等特性。SVG最早于1999年由W3C发布,用于在Web上展示矢量图形,并于2001年正式成为标准。在过去的二十多年中,SVG经历了多次更新和改进,增加了更多的功能和特性,如动画、交互等,并逐渐得到广泛的应用。SVG2.0于2016年9月15日成为W3C候选推荐标准,最新草案于。_如何训练svg标注数据集

WINDOWS 端CUDA10.2+CUDNN8环境搭建for YOLOV5 and Pytorch1.7-程序员宅基地

文章浏览阅读1.5k次,点赞4次,收藏11次。Windows 端CUDA10.2+CUDNN8环境搭建for Pytorch1.71.CUDA下载安装1.1 查看自己的显卡1.2 显卡驱动下载及安装1.3 显卡驱动安装检测1.4 CUDN下载1.5 CUDN安装1.6 环境变量设置2 CUDNN安装2.1 CUDNN下载2.2 CUDNN安装2.3 CUDA安装检测最近,我做了一些使用YOLOV5算法的项目,遇到了很多的坑,也感叹YOLOV5的人性化操作,如此真香的算法,忍不住拿出来与大家进行分享,现在就把项目过程中的一些操作逐个记录一遍。很不好意_cudnn8

优化Hadoop Balancer运行速度_failed to move blk_1076014847_2274553 with size=89-程序员宅基地

文章浏览阅读6.7k次。 1. 修改dfs.datanode.max.transfer.threads = 4096 (如果运行hbase的话建议为16384),指定用于在DataNode间传输block数据的最大线程数,老版本的对应参数为dfs.datanode.max.xcievers2. 修改dfs.datanode.balance.bandwidthPerSec = 52428800,指定DataNod..._failed to move blk_1076014847_2274553 with size=89971516 from 172.16.33.248:

推荐文章

热门文章

相关标签