《WCF教程10》.doc

  1. 1、本文档共16页,可阅读全部内容。
  2. 2、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
  3. 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载
  4. 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
《WCF教程10》.doc

[原创]我的WCF之旅(10):如何在WCF进行Exception Handling 在任何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 { ????[ServiceContract] ????public?interface?ICalculator ????{ ????????[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 { ????public?class?CalculatorService:ICalculator ????{ ????????ICalculator?Members#region?ICalculator?Members ????????public?double?Divide(double?x,?double?y) ????????{ ????????????if?(y?==?0) ????????????{ ????????????????throw?new?DivideByZeroException(Divide?by?zero); ????????????} ????????????return?x?/?y; ????????} ????????#endregion ????} } 如果被除数是零,抛出一个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 ????????????/serv

文档评论(0)

wgvi + 关注
实名认证
内容提供者

该用户很懒,什么也没介绍

1亿VIP精品文档

相关文档