子曾经曰过

  博客园  :: 首页  ::  ::  ::  :: 管理

传说中的通道Channel

我们可以直接使用通道调用服务的操作,而无须借助于代理类。

ChannelFactory<T>类以及它所支持的类型有助于我们轻松的创建代理,说白了是创建代理的另外一种方式,没有代理是不能调用服务的,看看定义

View Code
using System;
using System.ServiceModel.Channels;
using System.ServiceModel.Description;

namespace System.ServiceModel
{
// 摘要:
// 创建和管理客户端用来将消息发送到服务终结点的通道。
public abstract class ChannelFactory : CommunicationObject, IChannelFactory, ICommunicationObject, IDisposable
{
// 摘要:
// 初始化 System.ServiceModel.ChannelFactory 类的新实例。
protected ChannelFactory();

// 摘要:
// 获取客户端在通过由工厂产生的通道与服务终结点进行通信时使用的凭据。
//
// 返回结果:
// 如果已为工厂配置凭据或终结点不为 null 且处于已创建或正在打开通信状态,则为客户端使用的 System.ServiceModel.Description.ClientCredentials;否则为
// null。
public ClientCredentials Credentials { get; }
//
// 摘要:
// 获取为完成关闭操作提供的默认时间间隔。
//
// 返回结果:
// 默认的 System.Timespan,指定在超时前可用于完成关闭操作的时间。
protected override TimeSpan DefaultCloseTimeout { get; }
//
// 摘要:
// 获取为完成打开操作提供的默认时间间隔。
//
// 返回结果:
// 默认的 System.Timespan,指定在超时前可用于完成打开操作的时间。
protected override TimeSpan DefaultOpenTimeout { get; }
//
// 摘要:
// 获取由工厂产生的通道所要连接的服务终结点。
//
// 返回结果:
// 由工厂产生的通道所要连接的 System.ServiceModel.Description.ServiceEndpoint。
public ServiceEndpoint Endpoint { get; }

// 摘要:
// 使用由指定配置文件提供的行为和通道工厂服务终结点中的那些行为来初始化通道工厂。
//
// 参数:
// configurationName:
// 配置文件的名称。
//
// 异常:
// System.InvalidOperationException:
// 通道工厂的服务终结点为 null。
protected virtual void ApplyConfiguration(string configurationName);
//
// 摘要:
// 在派生类中实现时,创建与通道工厂关联的服务终结点说明。
//
// 返回结果:
// 与通道工厂关联的 System.ServiceModel.Description.ServiceEndpoint。
protected abstract ServiceEndpoint CreateDescription();
//
// 摘要:
// 为工厂的当前终结点生成通道工厂。
//
// 返回结果:
// 当前工厂的终结点的 System.ServiceModel.Channels.IChannelFactory。
//
// 异常:
// System.InvalidOperationException:
// 工厂通道所连接的服务终结点为 null,或者终结点的绑定为 null 或是缺少具有指定配置名称的元素。
protected virtual IChannelFactory CreateFactory();
//
// 摘要:
// 打开尚未打开的当前通道工厂。
//
// 异常:
// System.ObjectDisposedException:
// 当前工厂的状态为正在关闭或已关闭,因此无法打开。
protected void EnsureOpened();
//
// 摘要:
// 从通道堆栈的适当层返回所请求的类型化对象,如果不存在,则返回 null。
//
// 类型参数:
// T:
// 方法正在查询的类型化对象。
//
// 返回结果:
// 如果存在,则为所请求的类型化对象 T,如果不存在,则为 null。
public T GetProperty<T>() where T : class;
//
// 摘要:
// 使用指定的终结点初始化通道工厂的服务终结点。
//
// 参数:
// endpoint:
// 用以初始化通道工厂的 System.ServiceModel.Description.ServiceEndpoint。
//
// 异常:
// System.ArgumentNullException:
// endpoint 为 null。
protected void InitializeEndpoint(ServiceEndpoint endpoint);
//
// 摘要:
// 使用指定的绑定和地址初始化通道工厂的服务终结点。
//
// 参数:
// binding:
// 用以初始化通道工厂的 System.ServiceModel.Channels.Binding。
//
// address:
// 用以初始化通道工厂的 System.ServiceModel.EndpointAddress。
protected void InitializeEndpoint(Binding binding, EndpointAddress address);
//
// 摘要:
// 使用指定的地址和配置初始化通道工厂的服务终结点。
//
// 参数:
// configurationName:
// 用于初始化通道工厂的配置文件名称。
//
// address:
// 用以初始化通道工厂的 System.ServiceModel.EndpointAddress。
protected void InitializeEndpoint(string configurationName, EndpointAddress address);
//
// 摘要:
// 终止当前通道工厂的内部通道工厂。
protected override void OnAbort();
//
// 摘要:
// 对当前通道工厂(具有关联的状态对象)的内部通道工厂,开始一个异步关闭操作。
//
// 参数:
// timeout:
// System.Timespan,指定在超时前可用于完成操作的时间。
//
// callback:
// 接收异步操作完成通知的 System.AsyncCallback 委托。
//
// state:
// 一个由应用程序指定的对象,包含与异步操作相关联的状态信息。
//
// 返回结果:
// 引用异步操作的 System.IAsyncResult。
protected override IAsyncResult OnBeginClose(TimeSpan timeout, AsyncCallback callback, object state);
//
// 摘要:
// 对当前通道工厂(具有关联的状态对象)的内部通道工厂,开始一个异步打开操作。
//
// 参数:
// timeout:
// System.Timespan,指定在超时前可用于完成操作的时间。
//
// callback:
// 接收异步操作完成通知的 System.AsyncCallback 委托。
//
// state:
// 一个由应用程序指定的对象,包含与异步操作相关联的状态信息。
//
// 返回结果:
// 引用异步操作的 System.IAsyncResult。
protected override IAsyncResult OnBeginOpen(TimeSpan timeout, AsyncCallback callback, object state);
//
// 摘要:
// 使用完成操作的指定超时对内部通道工厂调用 close。
//
// 参数:
// timeout:
// System.Timespan,指定在超时前可用于完成操作的时间。
protected override void OnClose(TimeSpan timeout);
//
// 摘要:
// 对当前通道工厂的内部通道工厂,完成一个异步关闭操作。
//
// 参数:
// result:
// 通过调用 System.ServiceModel.ChannelFactory.OnBeginClose(System.TimeSpan,System.AsyncCallback,System.Object)
// 方法返回的 System.IAsyncResult。
protected override void OnEndClose(IAsyncResult result);
//
// 摘要:
// 对当前通道工厂的内部通道工厂,完成一个异步打开操作。
//
// 参数:
// result:
// 通过调用 System.ServiceModel.ChannelFactory.OnBeginOpen(System.TimeSpan,System.AsyncCallback,System.Object)
// 方法返回的 System.IAsyncResult。
protected override void OnEndOpen(IAsyncResult result);
//
// 摘要:
// 使用完成操作的指定超时对当前通道工厂的内部通道工厂调用 open。
//
// 参数:
// timeout:
// System.Timespan,指定在超时前必须完成打开操作的时间。
//
// 异常:
// System.InvalidOperationException:
// 当前通道的内部通道为 null。
protected override void OnOpen(TimeSpan timeout);
//
// 摘要:
// 为通道工厂初始化 System.ServiceModel.Description.ClientCredentials 对象的一个只读副本。
protected override void OnOpened();
//
// 摘要:
// 为当前的通道生成内部通道工厂。
//
// 异常:
// System.InvalidOperationException:
// 通道工厂的内部通道工厂为 null。
protected override void OnOpening();
}
}

几种重要而且实用的调用方式

ChannelFactory<ServiceReference1.IService> factory = new ChannelFactory<WindowsFormsApplication2.ServiceReference1.IService>();
ServiceReference1.IService proxy
= factory.CreateChannel();
using (proxy as IDisposable)
{
proxy.Fun();
}
//或者
ServiceReference1.IService proxy2 = factory.CreateChannel();
proxy2.Fun();
ICommunicationObject channel
= proxy2 as ICommunicationObject;
channel.Close();
//或者
NetTcpBinding binding = new NetTcpBinding();
EndpointAddress address
= new EndpointAddress("net.tcp://localhost:9999");
ServiceReference1.IService proxy3
= ChannelFactory<ServiceReference1.IService>.CreateChannel(binding, address);
using (proxy3 as IDisposable)
{
proxy3.Fun();
}

这边需要写一个完整的例子来加强印象,待续。

posted on 2011-02-23 09:21  人的本质是什么?  阅读(431)  评论(0编辑  收藏  举报