自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

WCF通道具體應用技巧分享

開發(fā) 開發(fā)工具
WCF通道的使用,可以幫助開發(fā)人員代替靜態(tài)代理,來對服務操作進行直接的調用。在這里我們將會針對此為大家詳細介紹。

WCF中的通道應用在實際編程中是一個非常重要的操作步驟。我們今天將會通過對WCF通道的使用技巧進行一個詳細的分析,希望可以讓大家從中獲得一些幫助,已解決在實際編程中出現(xiàn)的一些問題。

我們可以用WCF通道(channel)代替靜態(tài)代理(svcutil proxy),直接調用服務操作。ChannelFactory< T> 允許我們在運行時動態(tài)創(chuàng)建一個代理與服務進行交互。

  1. public class ContractDescription  
  2. {  
  3. public Type ContractType {get;set;}  
  4. //More members  
  5. }  
  6. public class ServiceEndpoint  
  7. {  
  8. public ServiceEndpoint(ContractDescription contract, 
    Binding binding, EndpointAddress address);  
  9. public EndpointAddress Address {get;set;}  
  10. public Binding Binding {get;set;}  
  11. public ContractDescription Contract {get;}  
  12. //More members  
  13. }  
  14. public abstract class ChannelFactory : ...  
  15. {  
  16. public ServiceEndpoint Endpoint {get;}  
  17. //More members  
  18. }  
  19. public class ChannelFactory< T> : ChannelFactory,...  
  20. {  
  21. public ChannelFactory(ServiceEndpoint endpoint);  
  22. public ChannelFactory(string configurationName);  
  23. public ChannelFactory(Binding binding, EndpointAddress 
    endpointAddress);  
  24. public static T CreateChannel(Binding binding, 
    EndpointAddress endpointAddress);  
  25. public T CreateChannel( );  
  26. //More Members  

我們需要從配置文件中獲取一個端點配置名稱,將其提交給 ChannelFactory< T> 構造方法,也可以直接使用相應的綁定和地址對象作為參數(shù)。然后,調用 CreateChannel() 方法獲取動態(tài)生成代理對象的引用。有兩種方法關閉代理,將WCF通道轉型成 IDisposable,并調用 Dispose() 方法關閉代理;或者轉型成 ICommunicationObject,調用 Close() 方法。

  1. ChannelFactory< IMyContract> factory = new ChannelFactory
    < IMyContract>( );  
  2. IMyContract proxy1 = factory.CreateChannel( );  
  3. using(proxy1 as IDisposable)  
  4. {  
  5. proxy1.MyMethod( );  
  6. }  
  7. IMyContract proxy2 = factory.CreateChannel( );  
  8. proxy2.MyMethod( );  
  9. ICommunicationObject channel = proxy2 as ICommunicationObject;  
  10. Debug.Assert(channel != null);  
  11. channel.Close( ); 

注: WCF通道對象除了實現(xiàn)服務契約接口外,還實現(xiàn)了 System.ServiceModel.IClientChannel。

  1. public interface IClientChannel : IContextChannel, IChannel, 
    ICommunicationObject, IDisposable ...  
  2. {  

除創(chuàng)建 ChannelFactory< T> 對象實例外,我們還可以直接使用靜態(tài)方法 CreateChannel() 來創(chuàng)建代理。不過這需要我們提供端點地址和綁定對象。

  1. Binding binding = new NetTcpBinding( );  
  2. EndpointAddress address = new EndpointAddress
    ("net.tcp://localhost:8000");  
  3. IMyContract proxy = ChannelFactory< IMyContract>.
    CreateChannel(binding, address);  
  4. using(proxy as IDisposable)  
  5. {  
  6. proxy1.MyMethod( );  

以上就是我們對WCF通道的相關應用的介紹。

【編輯推薦】

  1. WCF調用服務異?;窘鉀Q方案介紹
  2. WCF體系結構基本概念分享
  3. 各種常用WCF術語內容總結
  4. WCF特點具體優(yōu)勢總結
  5. WCF安全參數(shù)相關設置方法詳解
責任編輯:曹凱 來源: CSDN
相關推薦

2010-02-23 13:03:34

WCF序列化

2010-02-24 17:07:26

WCF序列化引擎

2010-03-01 13:06:49

WCF繼承

2010-02-22 15:20:54

WCF WS-Disc

2010-02-22 17:21:02

WCF消息交換

2010-02-25 18:04:02

WCF IIS宿主

2010-03-01 15:40:04

WCF實例停用

2010-03-02 10:50:57

WCF元數(shù)據(jù)交換

2010-02-26 10:46:12

WCF行為擴展

2010-03-01 09:48:23

WCF會話服務

2010-02-25 10:52:29

WCF響應服務

2010-03-01 17:52:03

WCF選擇綁定

2010-03-01 17:28:25

WCF Stream對

2010-02-25 11:23:29

WCF返回自定義格式

2010-02-24 17:41:05

WCF集合反序列化

2010-02-22 17:58:06

WCF異步上傳

2010-01-13 16:45:44

VB.NET刪除控件

2010-03-05 16:09:44

Python中文字符

2010-02-22 11:25:50

WCF DateSet

2010-02-26 14:12:27

WCF元數(shù)據(jù)
點贊
收藏

51CTO技術棧公眾號