程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> 關於.NET >> WCF服務編程設計規范(4):操作與錯誤設計

WCF服務編程設計規范(4):操作與錯誤設計

編輯:關於.NET

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()
{...}
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved