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.
在发布构建版本里,不要返回不可知的异常做为错误,除非是在调试场景里。
猜您也喜欢...
- · Windows XP与Visual Studio 2010的结合 VS2010已经发布了正式版,在这个新的工具中,有很多地方可以与XP....
- · 界面开发(一)--- Hook所有的窗体 这篇开始,我开始讲解一下我的界面开发的全部过程,一步一步的讲....
- · 实践重于理论 - 创建一个监控程序探测WCF的并发处理机制 为了使读者对采用不同实例上下文对并发的影响有一个深刻的认识,....
- · 基于CallContextInitializer的WCF扩展导致的严重问题 WCF是一个具有极高扩展度的分布式通信框架,无论是在信道层(Cha....
- · 通过WCF引用ListData.svc查询,更新数据 SharePoint 2010自带了一个开箱即用的WCF服务ListData。本博文中....