快捷搜索:

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 instead of

DuplexClientBase.

应用类型安然的DuplexClientBase代替DuplexClientBase.

14. Use the type-safe DuplexChannelFactory instead of

DuplexChannelFactory.

应用类型安然的DuplexChannelFactory代替DuplexChannelFactory.

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()

{...}

}

您可能还会对下面的文章感兴趣: