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()
{...}
}
前面相關文章: