WCF服务编程设计规范(8):服务托管、自托管代码、客户端代理类、客户端代理管理、客户端异常管理、数据契约、SOAP与REST
2010-05-07 00:14
330 查看
Posted on 2010-05-07 00:14 Frank Xu Lei 阅读(793) 评论(12) 编辑 收藏 所属分类: SOA and EAI, WCF服务编程设计规范
![](http://www.cnblogs.com/frank_xl/aggbug/1729342.html?type=1&webview=1)
本节是翻译整理的WCF服务编程设计规范(8),内容主要包括:服务托管、自托管代码、客户端代理类、客户端代理管理、客户端异常管理、数据契约、SOAP与REST。如何选择托管类型?,如何管理客户端代理?如何处理客户端异常?如何选择数据契约?如何选择SOAP与REST服务?给出了选择标准。
可以使用Server2008的时候,推荐使用WAS
Ø Multiple protocol support IIS Hosting model and tools
多协议支持IIS托管模型和工具
l Favor IIS for external HTTP only services
对外开放的http服务推荐使用IIS
Ø Better on-box scalability / availability through worker
通过工作线程能获得更好的扩展性/可用性
Ø process model
处理模型
Ø Better management tools
更好的管理工具
l Favor self-hosting for stateful services, callbacks, .NET Service Bus, debugging
状态服务、回调、.NET Service Bus和调试推荐自托管
l Have a console-based debug self-host for development time
开发时使用Console控制台托管
Ø Can be a Windows Service project that is used for production self-hosting with a mode switch for debugging
可以用于产品阶段托管在Windows服务的项目类型,利于模型转换与调试
l Consider Dublin hosting in the future
将来考虑Dulbin托管(Windows Application Server)
不要是在产品的Using代码块里使用ServiceHost
Ø Dispose can throw an exception that masks the real
Dispose可以跑出掩盖事实的异常
Ø Exception thrown from Open call
开放调用跑出的异常
Ø Explicitly call Close in try/catch, log/ deal with exception in catch
在try/catch里明确调用Close方法,在Catch代码里Log并处理异常
在ChannelFactory上推荐静态代理类
Ø Connection caching in the base class in 3.5
WCF3.5在基类里支持连接缓存机制
Ø Place for encapsulation of common patterns
封装常用模式的地方
l Hand-code or micro-code generate proxy classes for internal services
为内部服务手动编写代码或者微代码 生成代理类
Ø Less bloated code
避免臃肿的代码
Ø Share service contract and data contracts through libraries
通过类库共享服务和数据契约
Ø Explicit control over config file
通过配置文件明确控制
l Add Service Reference for external services or when you want an async API on the client
为外部服务添加服务引用或者当你想在客户端使用异步API的时候
Ø Clean up config after it destroys it
当你销毁它的时候,清楚配置
Ø Make sure to add references to data contract libraries before adding the service reference to avoid duplicate definitions
在引用服务之前确保引用数据契约类库,避免代码重复
Ø Live with the duplicate service contract definition instead of needing to repeatedly clean up the proxy code
使用复制的服务契约定义来代替重复的清理代理代码的工作
如果调用频繁,使用代理缓存,避免建立会话代价
Ø If secure / reliable session enabled
如果启用安全/可靠的会话
Ø Have to deal more cautiously with faulted proxies
特别注意处理错误的客户端代理
u Check proxy state before using
使用之前检查代理的状态
u Get rid of proxy after exception
异常以后清除代理
l Don’t put proxies in a using statement
不要把代理放到一个using代码块中
Ø Dispose call might throw exception and mask real exception
销毁调用可能抛出异常并掩盖真实的异常
Ø Explicitly close in a try/catch block, and if Close throws an exception, abort the proxy to ensure resource clean up
在try/catch块里,清晰地关闭,并且,如果close的时候抛出异常,终止代理确保资源释放
所有的调用服务的异常都继承自CommunicationException
l FaultException could be wrapped unhandled exception on the client, or explicit error returned from the service
FaultException可以被包装在一个客户端的未处理异常中,或者明确返回自服务
l FaultException<T> always an explicit error returned from the service
FaultException<T>始终明确从服务返回的错误
l Simple approach:
简单的方法
Ø Any exception from a proxy call, safe close the proxy
代理调用时发生任何异常,都要安全地关闭代理
l Advanced approach:
高级方法
Ø FaultException<T> - proxy is reusable
FaultException<T>-代理是可以复用的
推荐使用可序列化类型作为数据契约
Ø More explicit model, better control over what the client sees
更清晰的模型,对于客户端说看到的数据进行更好的控制
l Implement IExtensibleDataObject
实现IExtensibleDataObject接口
Ø Avoids dropping data that the service / client does not understand
避免使用服务/客户端都不明白的数据
l Avoid passing complex .NET specific types for interoperable services
避免在互操作服务里传递复杂的.NET类型
Ø DataSets and Exception types
DataSets和 Exception类型
l Avoid XmlSerializer and MessageContracts except for interoperable scenarios and REST services
除了互操作的场景和REST服务,避免XmlSerializer(XML序列化器)和MessageContracts(消息契约)
当你编写的服务只有你自己使用时,推荐SOAP
l Favor REST services for publicly exposed, data oriented services
当你的服务是公开暴露、面向数据时,推荐使用REST
【老徐的博客】
【作 者】:Frank Xu Lei
【地 址】:
http://www.cnblogs.com/frank_xl/
【中文论坛】:微软WCF中文技术论坛
【英文论坛】:微软WCF英文技术论坛
本节是翻译整理的WCF服务编程设计规范(8),内容主要包括:服务托管、自托管代码、客户端代理类、客户端代理管理、客户端异常管理、数据契约、SOAP与REST。如何选择托管类型?,如何管理客户端代理?如何处理客户端异常?如何选择数据契约?如何选择SOAP与REST服务?给出了选择标准。
Service Hosting
服务托管
l Favor WAS Hosting when Server 2008 is an option可以使用Server2008的时候,推荐使用WAS
Ø Multiple protocol support IIS Hosting model and tools
多协议支持IIS托管模型和工具
l Favor IIS for external HTTP only services
对外开放的http服务推荐使用IIS
Ø Better on-box scalability / availability through worker
通过工作线程能获得更好的扩展性/可用性
Ø process model
处理模型
Ø Better management tools
更好的管理工具
l Favor self-hosting for stateful services, callbacks, .NET Service Bus, debugging
状态服务、回调、.NET Service Bus和调试推荐自托管
l Have a console-based debug self-host for development time
开发时使用Console控制台托管
Ø Can be a Windows Service project that is used for production self-hosting with a mode switch for debugging
可以用于产品阶段托管在Windows服务的项目类型,利于模型转换与调试
l Consider Dublin hosting in the future
将来考虑Dulbin托管(Windows Application Server)
Self Host Code
自托管代码
l Do not put ServiceHost in a using statement in production code不要是在产品的Using代码块里使用ServiceHost
Ø Dispose can throw an exception that masks the real
Dispose可以跑出掩盖事实的异常
Ø Exception thrown from Open call
开放调用跑出的异常
Ø Explicitly call Close in try/catch, log/ deal with exception in catch
在try/catch里明确调用Close方法,在Catch代码里Log并处理异常
Client Proxy Classes
客户端代理类
l Favor static proxy class over ChannelFactory在ChannelFactory上推荐静态代理类
Ø Connection caching in the base class in 3.5
WCF3.5在基类里支持连接缓存机制
Ø Place for encapsulation of common patterns
封装常用模式的地方
l Hand-code or micro-code generate proxy classes for internal services
为内部服务手动编写代码或者微代码 生成代理类
Ø Less bloated code
避免臃肿的代码
Ø Share service contract and data contracts through libraries
通过类库共享服务和数据契约
Ø Explicit control over config file
通过配置文件明确控制
l Add Service Reference for external services or when you want an async API on the client
为外部服务添加服务引用或者当你想在客户端使用异步API的时候
Ø Clean up config after it destroys it
当你销毁它的时候,清楚配置
Ø Make sure to add references to data contract libraries before adding the service reference to avoid duplicate definitions
在引用服务之前确保引用数据契约类库,避免代码重复
Ø Live with the duplicate service contract definition instead of needing to repeatedly clean up the proxy code
使用复制的服务契约定义来代替重复的清理代理代码的工作
Client Proxy Management
客户端代理管理
l Cache client proxies if frequent calls to avoid session establishment cost如果调用频繁,使用代理缓存,避免建立会话代价
Ø If secure / reliable session enabled
如果启用安全/可靠的会话
Ø Have to deal more cautiously with faulted proxies
特别注意处理错误的客户端代理
u Check proxy state before using
使用之前检查代理的状态
u Get rid of proxy after exception
异常以后清除代理
l Don’t put proxies in a using statement
不要把代理放到一个using代码块中
Ø Dispose call might throw exception and mask real exception
销毁调用可能抛出异常并掩盖真实的异常
Ø Explicitly close in a try/catch block, and if Close throws an exception, abort the proxy to ensure resource clean up
在try/catch块里,清晰地关闭,并且,如果close的时候抛出异常,终止代理确保资源释放
Client Exception Management
客户端异常管理
l All exceptions thrown from a service call derive from CommunicationException所有的调用服务的异常都继承自CommunicationException
l FaultException could be wrapped unhandled exception on the client, or explicit error returned from the service
FaultException可以被包装在一个客户端的未处理异常中,或者明确返回自服务
l FaultException<T> always an explicit error returned from the service
FaultException<T>始终明确从服务返回的错误
l Simple approach:
简单的方法
Ø Any exception from a proxy call, safe close the proxy
代理调用时发生任何异常,都要安全地关闭代理
l Advanced approach:
高级方法
Ø FaultException<T> - proxy is reusable
FaultException<T>-代理是可以复用的
Data Contracts
数据契约
l Favor data contracts over serializable types推荐使用可序列化类型作为数据契约
Ø More explicit model, better control over what the client sees
更清晰的模型,对于客户端说看到的数据进行更好的控制
l Implement IExtensibleDataObject
实现IExtensibleDataObject接口
Ø Avoids dropping data that the service / client does not understand
避免使用服务/客户端都不明白的数据
l Avoid passing complex .NET specific types for interoperable services
避免在互操作服务里传递复杂的.NET类型
Ø DataSets and Exception types
DataSets和 Exception类型
l Avoid XmlSerializer and MessageContracts except for interoperable scenarios and REST services
除了互操作的场景和REST服务,避免XmlSerializer(XML序列化器)和MessageContracts(消息契约)
SOAP vs. REST
SOAP与REST
l Favor SOAP services when you are writing a service that only your code will consume当你编写的服务只有你自己使用时,推荐SOAP
l Favor REST services for publicly exposed, data oriented services
当你的服务是公开暴露、面向数据时,推荐使用REST
【老徐的博客】
【作 者】:Frank Xu Lei
【地 址】:
http://www.cnblogs.com/frank_xl/
【中文论坛】:微软WCF中文技术论坛
【英文论坛】:微软WCF英文技术论坛
相关文章推荐
- WCF服务编程设计规范(8):服务托管、自托管代码、客户端代理管理
- WCF服务编程设计规范(3):服务契约、数据契约和实例管理设计规范
- WCF服务编程设计规范(3):服务契约、数据契约和实例管理设计规范
- Webservice_23_SOAP的基于契约优先开发用户管理_添加异常
- 通过WSDL生成soap客户端代码时报的异常
- 我不要紧的编码组两人,至少两年的dotnet网咯服务开发经验,熟悉数据库orm, 系统管理人员:熟悉各linux发行版的系统管理和安全管理,linux常用数据库的配置与运维等,有代码托管服务管理经验优
- WCF托管服务和客户端代理
- webservice接口常见问题1:客户端使用SOAP方式调用CXF服务时异常
- 架构免费的在线项目管理与代码托管服务
- Webservice_23_SOAP的基于契约优先开发用户管理_实现客户端
- WCF托管服务和客户端代理
- [教程]使用google code在线代码托管服务管理你的项目源码
- Python针对特定服务定制的代理工具V2.0----------------(代码组织简介)
- 常用Git代码托管服务分享 推荐
- 构建自己的SVN 代码托管服务
- 在微服务中如何管理数据
- Python针对特定服务定制的代理工具V2.0----------------(代码组织简介)
- axis1.4发布返回复杂类型的webservice服务+代码访问+客户端访问
- WCF契约的简单介绍(服务契约 数据契约 消息契约)
- Pro Android学习笔记(七十):HTTP服务(4):SOAP/JSON/XML、异常