WCF中的通道应用在实际编程中是一个非常重要的操作步骤。我们今天将会通过对WCF通道的使用技巧进行一个详细的分析,希望可以让大家从中获得一些帮助,已解决在实际编程中出现的一些问题。
我们提供的服务有:做网站、成都网站设计、微信公众号开发、网站优化、网站认证、渭南ssl等。为超过千家企事业单位解决了网站和推广的问题。提供周到的售前咨询和贴心的售后服务,是有科学管理、有技术的渭南网站制作公司
我们可以用WCF通道(channel)代替静态代理(svcutil proxy),直接调用服务操作。ChannelFactory< T> 允许我们在运行时动态创建一个代理与服务进行交互。
- public class ContractDescription
- {
- public Type ContractType {get;set;}
- //More members
- }
- public class ServiceEndpoint
- {
- public ServiceEndpoint(ContractDescription contract,
Binding binding, EndpointAddress address);- public EndpointAddress Address {get;set;}
- public Binding Binding {get;set;}
- public ContractDescription Contract {get;}
- //More members
- }
- public abstract class ChannelFactory : ...
- {
- public ServiceEndpoint Endpoint {get;}
- //More members
- }
- public class ChannelFactory< T> : ChannelFactory,...
- {
- public ChannelFactory(ServiceEndpoint endpoint);
- public ChannelFactory(string configurationName);
- public ChannelFactory(Binding binding, EndpointAddress
endpointAddress);- public static T CreateChannel(Binding binding,
EndpointAddress endpointAddress);- public T CreateChannel( );
- //More Members
- }
我们需要从配置文件中获取一个端点配置名称,将其提交给 ChannelFactory< T> 构造方法,也可以直接使用相应的绑定和地址对象作为参数。然后,调用 CreateChannel() 方法获取动态生成代理对象的引用。有两种方法关闭代理,将WCF通道转型成 IDisposable,并调用 Dispose() 方法关闭代理;或者转型成 ICommunicationObject,调用 Close() 方法。
- ChannelFactory< IMyContract> factory = new ChannelFactory
< IMyContract>( );- IMyContract proxy1 = factory.CreateChannel( );
- using(proxy1 as IDisposable)
- {
- proxy1.MyMethod( );
- }
- IMyContract proxy2 = factory.CreateChannel( );
- proxy2.MyMethod( );
- ICommunicationObject channel = proxy2 as ICommunicationObject;
- Debug.Assert(channel != null);
- channel.Close( );
注: WCF通道对象除了实现服务契约接口外,还实现了 System.ServiceModel.IClientChannel。
- public interface IClientChannel : IContextChannel, IChannel,
ICommunicationObject, IDisposable ...- {
- }
除创建 ChannelFactory< T> 对象实例外,我们还可以直接使用静态方法 CreateChannel() 来创建代理。不过这需要我们提供端点地址和绑定对象。
- Binding binding = new NetTcpBinding( );
- EndpointAddress address = new EndpointAddress
("net.tcp://localhost:8000");- IMyContract proxy = ChannelFactory< IMyContract>.
CreateChannel(binding, address);- using(proxy as IDisposable)
- {
- proxy1.MyMethod( );
- }
以上就是我们对WCF通道的相关应用的介绍。
文章题目:WCF通道具体应用技巧分享
转载来于:http://www.csdahua.cn/qtweb/news3/400653.html
网站建设、网络推广公司-快上网,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 快上网