WCF通道具体应用技巧分享

开发 开发工具
WCF通道的使用,可以帮助开发人员代替静态代理,来对服务操作进行直接的调用。在这里我们将会针对此为大家详细介绍。

WCF中的通道应用在实际编程中是一个非常重要的操作步骤。我们今天将会通过对WCF通道的使用技巧进行一个详细的分析,希望可以让大家从中获得一些帮助,已解决在实际编程中出现的一些问题。

我们可以用WCF通道(channel)代替静态代理(svcutil proxy),直接调用服务操作。ChannelFactory< T> 允许我们在运行时动态创建一个代理与服务进行交互。

  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> 构造方法,也可以直接使用相应的绑定和地址对象作为参数。然后,调用 CreateChannel() 方法获取动态生成代理对象的引用。有两种方法关闭代理,将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通道对象除了实现服务契约接口外,还实现了 System.ServiceModel.IClientChannel。

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

除创建 ChannelFactory< T> 对象实例外,我们还可以直接使用静态方法 CreateChannel() 来创建代理。不过这需要我们提供端点地址和绑定对象。

  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调用服务异常基本解决方案介绍
  2. WCF体系结构基本概念分享
  3. 各种常用WCF术语内容总结
  4. WCF特点具体优势总结
  5. WCF安全参数相关设置方法详解
责任编辑:曹凯 来源: 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-26 10:46:12

WCF行为扩展

2010-03-02 10:50:57

WCF元数据交换

2010-03-01 15:40:04

WCF实例停用

2010-02-25 18:04:02

WCF IIS宿主

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-03-05 16:09:44

Python中文字符

2010-01-13 16:45:44

VB.NET删除控件

2010-02-22 11:25:50

WCF DateSet

2010-02-23 16:46:47

WCF并发能力
点赞
收藏

51CTO技术栈公众号