天天看點

WCF服務程式設計設計規範(8):服務托管、自托管代碼、用戶端代理管理

本節是翻譯整理的WCF服務程式設計設計規範(8),内容主要包括:服務托管、自托管代碼、用戶端代理類、用戶端代理管理、用戶端異常管理、資料契約、SOAP與REST。如何選擇托管類型?,如何管理用戶端代理?如何處理用戶端異常?如何選擇資料契約?如何選擇SOAP與REST服務?給出了選擇标準。

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)

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并處理異常

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

使用複制的服務契約定義來代替重複的清理代理代碼的工作

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的時候抛出異常,終止代理確定資源釋放

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>-代理是可以複用的

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(消息契約)

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

 本文轉自 frankxulei 51CTO部落格,原文連結:http://blog.51cto.com/frankxulei/320878,如需轉載請自行聯系原作者