經驗之談WCF behaviors配置節
學習WCF時,你可能會遇到WCF behaviors配置節問題,這里將介紹WCF behaviors配置節問題的解決方法,在這里拿出來和大家分享一下。當我們在定義一個實現了Service Contract的類時, binding和address信息是客戶端必須知道的,否則無法調用該服務。然而,如果需要指定服務在執行方面的相關特性時,就必須定義服務的 WCF behaviors配置節。在WCF中,定義behavior就可以設置服務的運行時屬性,甚至于通過自定義WCF behaviors配置節插入一些自定義類型。例如通過指定 ServiceMetadataBehavior,可以使WCF服務對外公布Metadata。配置如下:
- <behaviors>
- <serviceBehaviors>
- <behavior name="metadataSupport">
- <serviceMetadata httpGetEnabled="true" httpGetUrl=""/>
- < SPAN>behavior>
- <serviceBehaviors>
- <behaviors>
在 WCF中,behavior被定義為Attribute,其中,System.ServiceModel.ServiceBehaviorAttribute和 System.ServiceModel.OperationBehaviorAttribute是最常用的behavior。雖然,WCF behaviors配置節作為Attribute可以通過編程的方式直接施加到服務上,但出于靈活性的考慮,將behavior定義到配置文件中才是***的設計方式。
利用ServiceBehavior與OperationBehavior可以控制服務的如下屬性:
#T#◆對象實例的生命周期;
◆并發與異步處理;
◆配置行為;
◆事務行為;
◆序列化行為;
◆元數據轉換;
◆會話的生命周期;
◆地址過濾以及消息頭的處理;
◆模擬(Impersonation);
例如,通過ServiceBehavior設置對象實例的生命周期:
- <behaviors>
- <serviceBehaviors>
- <behavior name="metadataSupport">
- <instanceContextMode httpGetEnabled="true" httpGetUrl=""/>
- < SPAN>behavior>
- <serviceBehaviors>
- <behaviors>


















