您的位置:首页 > 编程语言

WCF服务编程设计规范(8):服务托管、自托管代码、客户端代理管理

2010-05-07 00:14 357 查看
本节是翻译整理的WCF服务编程设计规范(8),内容主要包括:服务托管、自托管代码、客户端代理类、客户端代理管理、客户端异常管理、数据契约、SOAP与REST。如何选择托管类型?,如何管理客户端代理?如何处理客户端异常?如何选择数据契约?如何选择SOAP与REST服务?给出了选择标准。

Service Hosting

服务托管

[align=left]l Favor WAS Hosting when Server 2008 is an option[/align]
[align=left]可以使用Server2008的时候,推荐使用WAS[/align]
[align=left]Ø Multiple protocol support IIS Hosting model and tools[/align]
[align=left]多协议支持IIS托管模型和工具[/align]
[align=left]l Favor IIS for external HTTP only services[/align]
[align=left]对外开放的http服务推荐使用IIS[/align]
[align=left]Ø Better on-box scalability / availability through worker[/align]
[align=left]通过工作线程能获得更好的扩展性/可用性[/align]
[align=left]Ø process model[/align]
[align=left]处理模型[/align]
[align=left]Ø Better management tools[/align]
[align=left]更好的管理工具[/align]
[align=left]l Favor self-hosting for stateful services, callbacks, .NET Service Bus, debugging[/align]
[align=left]状态服务、回调、.NET Service Bus和调试推荐自托管[/align]
[align=left]l Have a console-based debug self-host for development time[/align]
[align=left]开发时使用Console控制台托管[/align]
[align=left]Ø Can be a Windows Service project that is used for production self-hosting with a mode switch for debugging[/align]
[align=left]可以用于产品阶段托管在Windows服务的项目类型,利于模型转换与调试[/align]
[align=left]l Consider Dublin hosting in the future[/align]
[align=left]将来考虑Dulbin托管(Windows Application Server)[/align]

Self Host Code

自托管代码

[align=left]l Do not put ServiceHost in a using statement in production code[/align]
[align=left]不要是在产品的Using代码块里使用ServiceHost[/align]
[align=left]Ø Dispose can throw an exception that masks the real[/align]
[align=left]Dispose可以跑出掩盖事实的异常[/align]
[align=left]Ø Exception thrown from Open call[/align]
[align=left]开放调用跑出的异常[/align]
[align=left]Ø Explicitly call Close in try/catch, log/ deal with exception in catch[/align]
[align=left]在try/catch里明确调用Close方法,在Catch代码里Log并处理异常[/align]

Client Proxy Classes

客户端代理类

[align=left]l Favor static proxy class over ChannelFactory[/align]
[align=left]在ChannelFactory上推荐静态代理类[/align]
[align=left]Ø Connection caching in the base class in 3.5[/align]
[align=left]WCF3.5在基类里支持连接缓存机制[/align]
[align=left]Ø Place for encapsulation of common patterns[/align]
[align=left]封装常用模式的地方[/align]
[align=left]l Hand-code or micro-code generate proxy classes for internal services[/align]
[align=left]为内部服务手动编写代码或者微代码 生成代理类[/align]
[align=left]Ø Less bloated code[/align]
[align=left]避免臃肿的代码[/align]
[align=left]Ø Share service contract and data contracts through libraries[/align]
[align=left]通过类库共享服务和数据契约[/align]
[align=left]Ø Explicit control over config file[/align]
[align=left]通过配置文件明确控制[/align]
[align=left]l Add Service Reference for external services or when you want an async API on the client[/align]
[align=left]为外部服务添加服务引用或者当你想在客户端使用异步API的时候[/align]
[align=left]Ø Clean up config after it destroys it[/align]
[align=left]当你销毁它的时候,清楚配置[/align]
[align=left]Ø Make sure to add references to data contract libraries before adding the service reference to avoid duplicate definitions[/align]
[align=left]在引用服务之前确保引用数据契约类库,避免代码重复[/align]
[align=left]Ø Live with the duplicate service contract definition instead of needing to repeatedly clean up the proxy code[/align]
[align=left]使用复制的服务契约定义来代替重复的清理代理代码的工作[/align]

Client Proxy Management

客户端代理管理

[align=left]l Cache client proxies if frequent calls to avoid session establishment cost[/align]
[align=left]如果调用频繁,使用代理缓存,避免建立会话代价[/align]
[align=left]Ø If secure / reliable session enabled[/align]
[align=left]如果启用安全/可靠的会话[/align]
[align=left]Ø Have to deal more cautiously with faulted proxies[/align]
[align=left]特别注意处理错误的客户端代理[/align]
[align=left]u Check proxy state before using[/align]
[align=left]使用之前检查代理的状态[/align]
[align=left]u Get rid of proxy after exception[/align]
[align=left]异常以后清除代理[/align]
[align=left]l Don’t put proxies in a using statement[/align]
[align=left]不要把代理放到一个using代码块中[/align]
[align=left]Ø Dispose call might throw exception and mask real exception[/align]
[align=left]销毁调用可能抛出异常并掩盖真实的异常[/align]
[align=left]Ø Explicitly close in a try/catch block, and if Close throws an exception, abort the proxy to ensure resource clean up[/align]
[align=left]在try/catch块里,清晰地关闭,并且,如果close的时候抛出异常,终止代理确保资源释放[/align]

Client Exception Management

客户端异常管理

[align=left]l All exceptions thrown from a service call derive from CommunicationException[/align]
[align=left]所有的调用服务的异常都继承自CommunicationException[/align]
[align=left]l FaultException could be wrapped unhandled exception on the client, or explicit error returned from the service[/align]
[align=left]FaultException可以被包装在一个客户端的未处理异常中,或者明确返回自服务[/align]
[align=left]l FaultException<T> always an explicit error returned from the service[/align]
[align=left]FaultException<T>始终明确从服务返回的错误[/align]
[align=left]l Simple approach:[/align]
[align=left]简单的方法[/align]
[align=left]Ø Any exception from a proxy call, safe close the proxy[/align]
[align=left]代理调用时发生任何异常,都要安全地关闭代理[/align]
[align=left]l Advanced approach:[/align]
[align=left]高级方法[/align]
[align=left]Ø FaultException<T> - proxy is reusable[/align]
[align=left]FaultException<T>-代理是可以复用的[/align]

Data Contracts

数据契约

[align=left]l Favor data contracts over serializable types[/align]
[align=left]推荐使用可序列化类型作为数据契约[/align]
[align=left]Ø More explicit model, better control over what the client sees[/align]
[align=left]更清晰的模型,对于客户端说看到的数据进行更好的控制[/align]
[align=left]l Implement IExtensibleDataObject[/align]
[align=left]实现IExtensibleDataObject接口[/align]
[align=left]Ø Avoids dropping data that the service / client does not understand[/align]
[align=left]避免使用服务/客户端都不明白的数据[/align]
[align=left]l Avoid passing complex .NET specific types for interoperable services[/align]
[align=left]避免在互操作服务里传递复杂的.NET类型[/align]
[align=left]Ø DataSets and Exception types[/align]
[align=left]DataSets和 Exception类型[/align]
[align=left]l Avoid XmlSerializer and MessageContracts except for interoperable scenarios and REST services[/align]
[align=left]除了互操作的场景和REST服务,避免XmlSerializer(XML序列化器)和MessageContracts(消息契约)[/align]

SOAP vs. REST

SOAP与REST

[align=left]l Favor SOAP services when you are writing a service that only your code will consume[/align]
[align=left]当你编写的服务只有你自己使用时,推荐SOAP[/align]
[align=left]l Favor REST services for publicly exposed, data oriented services[/align]
[align=left]当你的服务是公开暴露、面向数据时,推荐使用REST[/align]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息