WCF服务编程设计规范(4):操作与错误设计

简介:
WCF服务编程设计规范(4):操作与错误设计。主要包含服务操作与调用、错误设计规范。中英对照。欢迎留言交流。下一节会介绍事务、并发管理和队列服务的内容。
Operations and Calls
操作与调用
1. Do not treat one-way calls as asynchronous calls.
          不要把单向调用作为异步调用
2. Do not treat one-way calls as concurrent calls.
          不要把单向调用作为并发调用
3. Expect exceptions from a one-way operation.
          单向操作也应该返回异常
4. Enable reliability even on one-way calls. Use of ordered delivery is optional for one way calls.
          即使当单向调用的时候,也要启用可靠性。单向调用不必使用有序传递。
5. Avoid one-way operations on a sessionful service. If used, make it the terminating operation:
          避免在会话服务里使用单向调用。如果用到,作为结束操作。
[ServiceContract(SessionMode = SessionMode. Required)]
interface IMyContract
{
[OperationContract]
void MyMethod1();
[OperationContract( IsOneWay  true ,IsInitiating = false, IsTerminating  = true )]
void MyMethod2();
}
6. Name the callback contract on the service side after the service contract name, suffixed by  Callback :
          回调操作最好使用服务契约的名字加后缀 Callback
interface IMyContractCallback
{...}
[ServiceContract(CallbackContract = typeof( IMyContractCallback))]
interface IMyContract
{...}
7. Strive to mark callback operations as one-way.
          回调操作标记会单向操作
8. Use callback contracts for callbacks only.
          只在回调的时候使用回调契约。
9. Avoid mixing regular callbacks and events on the same callback contract.
          避免在回调契约上混用常规回调和事件
10. Event operations should be well designed:
          事件操作应该设计如下:
a)  void  return type
     避免返回类型
b) No out-parameters
          没有 out 参数
c) Marked as one-way operations
          标记为单向操作
11. Avoid using raw callback contracts for event management, and prefer using the publish-subscribe framework.
          避免在事件管理中使用原始回调契约,推荐使用发布 - 订阅框架
12. Always provide explicit methods for callback setup and teardown:
          为回调提供清晰的安装 (setup) 和拆卸  ( teardown) 方法
[ServiceContract(CallbackContract = typeof(IMyContractCallback))]
interface IMyContract
{
[OperationContract]
void DoSomething();
[OperationContract]
void Connect();
[OperationContract]
void Disconnect();
}
interface IMyContractCallback
{...}
13. Use the type-safe  DuplexClientBase<T,C>  instead of
DuplexClientBase<T> .
          使用类型安全的 DuplexClientBase<T,C> 代替 DuplexClientBase<T> .
 
14. Use the type-safe  DuplexChannelFactory<T,C>  instead of
DuplexChannelFactory<T> .
          使用类型安全的 DuplexChannelFactory<T,C> 代替 DuplexChannelFactory<T> .
 
15. When debugging or in intranet deployment of callbacks over the  WSDualHttpBinding , use the  CallbackBaseAddressBehavior  attribute with  CallbackPort  set to  0 :
          当调试或在企业局域网部署环境里使用 WSDualHttpBinding 时,使用 CallbackBaseAddressBehavior  ,并把  CallbackPort 设置 0
[CallbackBaseAddressBehavior( CallbackPort = 0 )]
class MyClient : IMyContractCallback
{...}
Faults
错误
1. Never use a proxy instance after an exception, even if you catch that exception.
          不要异常以后使用代理实例,尽管你捕获了异常。
2. Avoid fault contracts and allow WCF to mask the error.
          避免错误契约,让 WCF 来包装错误
3. Do not reuse the callback channel after an exception even if you catch that exception, as the channel may be faulted.
          不要在异常后还使用回调通道,尽管你捕获了异常,因为通道可能出于错误状态。
4. Use the  FaultContract  attribute with exception classes, as opposed to mere serializable types:
在异常类上使用 FaultContract 属性,而不是可序列化类型上:
//Avoid: 避免
[OperationContract]
[FaultContract(typeof( double))]
double Divide(double number1,double number2);
//Correct: 正确
[OperationContract]
[FaultContract(typeof( DivideByZeroException))]
double Divide(double number1,double number2);
5. Avoid lengthy processing such as logging in  IErrorHandler.ProvideFault() .
          避免冗长的处理,比如登入 IErrorHandler.ProvideFault() .
 
6. With both service classes and callback classes, set  IncludeExceptionDetailInFaults  to  true  in debug sessions, either in the config file or programmatically:
          对于服务类和回调类,在调试时,设置 IncludeExceptionDetailInFaults true ,配置文件或者编程都可以:
public class DebugHelper
{
public const bool IncludeExceptionDetailInFaults =
#if DEBUG
true;
#else
false;
#endif
}
[ServiceBehavior(IncludeExceptionDetailInFaults =
DebugHelper.IncludeExceptionDetailInFaults)]
class MyService : IMyContract
{...}
7. In release builds, do not return unknown exceptions as faults except in diagnostic scenarios.
          在发布构建版本里,不要返回不可知的异常做为错误,除非是在调试场景里。
8. Consider using the  ErrorHandlerBehavior  attribute on the service, both for promoting exceptions to fault contracts and for automatic error logging:
          当提升异常为错误契约,以及自动错误日志时,都可以考虑在服务上使用 ErrorHandlerBehavior 属性:
[ErrorHandlerBehavior]
class MyService : IMyContract
{...}
9. Consider using the  CallbackErrorHandlerBehaviorAttribute  on the callback client, both for promoting exceptions to fault contracts and for automatic
error logging:
      当提升异常为错误契约,以及自动错误日志时,考虑在回调客户端上使用 CallbackErrorHandlerBehaviorAttribute
[CallbackErrorHandlerBehavior(typeof(MyClient))]
class MyClient : IMyContractCallback
{
public void OnCallabck()
{...}
}
前面相关文章:




 本文转自 frankxulei 51CTO博客,原文链接:http://blog.51cto.com/frankxulei/318795 ,如需转载请自行联系原作者

相关文章
|
9月前
|
前端开发
WCF更新服务引用报错的原因之一
WCF更新服务引用报错的原因之一
|
8月前
|
C# 数据安全/隐私保护
c#如何创建WCF服务到发布(SqlServer版已经验证)
c#如何创建WCF服务到发布(SqlServer版已经验证)
38 0
|
8月前
|
安全 数据库连接 数据库
WCF服务创建到发布(SqlServer版)
在本示例开始之前,让我们先来了解一下什么是wcf? wcf有哪些特点? wcf是一个面向服务编程的综合分层架构。该架构的项层为服务模型层。 使用户用最少的时间和精力建立自己的软件产品和外界通信的模型。它使得开发者能够建立一个跨平台的安全、可信赖、事务性的解决方案。且能与已有系统兼容写作。 简单概括就是:一组数据通信的应用程序开发接口。
57 0
|
9月前
|
API 数据库
如何使用WCF框架和EF框架实现对数据库的操作
如何使用WCF框架和EF框架实现对数据库的操作
Visual Studio 2022 创建 WCF服务 找不到
Visual Studio 2022 创建 WCF服务 找不到
|
C++
WCF基础教程(二)——解析iis8和iis8.5+VS2013发布wcf服务问题
WCF基础教程(二)——解析iis8和iis8.5+VS2013发布wcf服务问题
102 0
WCF基础教程(二)——解析iis8和iis8.5+VS2013发布wcf服务问题
WCF使用纯代码的方式进行服务寄宿
服务寄宿的目的是为了开启一个进程,为WCF服务提供一个运行的环境。通过为服务添加一个或者多个终结点,使之暴露给潜在的服务消费,服务消费者通过匹配的终结点对该服务进行调用,除去上面的两种寄宿方式,还可以以纯代码的方式实现服务的寄宿工作。
849 0
|
Windows
WCF服务寄宿到IIS
一.WCF简介: Windows Communication Foundation(WCF)是由微软开发的一系列支持数据通信的应用程序框架,可以翻译为Windows 通讯开发平台。整合了原有的windows通讯的 .net Remoting,WebService,Socket的机制,并融合有HTTP和FTP的相关技术。
1046 0
WCF服务自我寄宿
WCF服务的寄宿方式 WCF寄宿方式是一种非常灵活的操作,可以寄宿在各种进程之中,常见的寄宿有: IIS服务、Windows服务、Winform程序、控制台程序中进行寄宿,从而实现WCF服务的运行,为调用者方便、高效提供服务调用。
994 0
|
网络架构
(纯代码)快速创建wcf rest 服务
因为有一个小工具需要和其它的业务对接数据,所以就试一下看能不能弄一个无需配置快速对接的方法出来,百(以)度(讹)过(传)后(讹),最后还是对照wcf配置对象调试出来了: 1.创建WebHttpBinding 2.
978 0