ChannelFactory 类
定义
创建和管理客户端用来将消息发送到服务终结点的通道。Creates and manages the channels that are used by clients to send messages to service endpoints.
public ref class ChannelFactory abstract : System::ServiceModel::Channels::CommunicationObject, IDisposable, System::ServiceModel::Channels::IChannelFactory
public abstract class ChannelFactory : System.ServiceModel.Channels.CommunicationObject, IDisposable, System.ServiceModel.Channels.IChannelFactory
type ChannelFactory = class
inherit CommunicationObject
interface IDisposable
interface IChannelFactory
interface ICommunicationObject
type ChannelFactory = class
inherit CommunicationObject
interface IChannelFactory
interface ICommunicationObject
interface IDisposable
Public MustInherit Class ChannelFactory
Inherits CommunicationObject
Implements IChannelFactory, IDisposable
- 继承
- 派生
- 实现
示例
下面的代码示例演示如何在工厂创建通道对象前,以编程方式插入客户端行为。The following code example shows how to insert programmatically a client behavior prior to the creation of the channel object by the factory.
public class Client
{
public static void Main()
{
try
{
// Picks up configuration from the config file.
ChannelFactory<ISampleServiceChannel> factory
= new ChannelFactory<ISampleServiceChannel>("WSHttpBinding_ISampleService");
// Add the client side behavior programmatically to all created channels.
factory.Endpoint.Behaviors.Add(new EndpointBehaviorMessageInspector());
ISampleServiceChannel wcfClientChannel = factory.CreateChannel();
// Making calls.
Console.WriteLine("Enter the greeting to send: ");
string greeting = Console.ReadLine();
Console.WriteLine("The service responded: " + wcfClientChannel.SampleMethod(greeting));
Console.WriteLine("Press ENTER to exit:");
Console.ReadLine();
// Done with service.
wcfClientChannel.Close();
Console.WriteLine("Done!");
}
catch (TimeoutException timeProblem)
{
Console.WriteLine("The service operation timed out. " + timeProblem.Message);
Console.Read();
}
catch (FaultException<SampleFault> fault)
{
Console.WriteLine("SampleFault fault occurred: {0}", fault.Detail.FaultMessage);
Console.Read();
}
catch (CommunicationException commProblem)
{
Console.WriteLine("There was a communication problem. " + commProblem.Message);
Console.Read();
}
}
Public Class Client
Public Shared Sub Main()
Try
' Picks up configuration from the config file.
Dim factory As New ChannelFactory(Of ISampleServiceChannel)("WSHttpBinding_ISampleService")
' Add the client side behavior programmatically to all created channels.
factory.Endpoint.Behaviors.Add(New EndpointBehaviorMessageInspector())
Dim wcfClientChannel As ISampleServiceChannel = factory.CreateChannel()
' Making calls.
Console.WriteLine("Enter the greeting to send: ")
Dim greeting As String = Console.ReadLine()
Console.WriteLine("The service responded: " & wcfClientChannel.SampleMethod(greeting))
Console.WriteLine("Press ENTER to exit:")
Console.ReadLine()
' Done with service.
wcfClientChannel.Close()
Console.WriteLine("Done!")
Catch timeProblem As TimeoutException
Console.WriteLine("The service operation timed out. " & timeProblem.Message)
Console.Read()
Catch fault As FaultException(Of SampleFault)
Console.WriteLine("SampleFault fault occurred: {0}", fault.Detail.FaultMessage)
Console.Read()
Catch commProblem As CommunicationException
Console.WriteLine("There was a communication problem. " & commProblem.Message)
Console.Read()
End Try
End Sub
注解
实现 IChannelFactory 接口的通道工厂及其关联通道一般由通信模式的发起方使用。Channel factories that implement the IChannelFactory interface and their associated channels are generally used by the initiators of a communication pattern. 而实现 IChannelListener 接口的侦听器工厂及其关联侦听器则提供一些用于接受通道以进行通信的机制。Listener factories that implement the IChannelListener interface and their associated listeners provide the mechanisms with which channels are accepted for communications.
此类不属于通道模型,而属于服务模型。This class is not part of the channel model, but of the service model. CreateFactory 方法提供为服务终结点创建 IChannelFactory 的方式。The CreateFactory method provides the means to create an IChannelFactory for a service endpoint. 使用该方法可以构建与服务上接口协定关联的客户端,而无需使用元数据或策略。Use it to construct a client that hooks up to an interface contract on the service without using metadata or policy.
备注
将 ChannelFactory.Credentials.Windows.AllowedImpersonationLevel 设置为 TokenImpersonationLevel.Anonymous 总是会导致匿名登录,而不论模拟级别为何。Setting ChannelFactory.Credentials.Windows.AllowedImpersonationLevel to TokenImpersonationLevel.Anonymous always results in an anonymous logon regardless of impersonation level.
针对从此类派生的托管 C++ 用户的特别说明:Special note for Managed C++ users deriving from this class:
将清理代码放入 (On)(Begin)Close(和/或 OnAbort)中,而不是放入析构函数中。Put your cleanup code in (On)(Begin)Close (and/or OnAbort), not in a destructor.
避免使用析构函数;它们会导致编译器自动生成 IDisposable。Avoid destructors; they cause the compiler to auto-generate IDisposable.
避免使用非引用成员;它们可能会导致编译器自动生成 IDisposable。Avoid non-reference members; they can cause the compiler to auto-generate IDisposable.
避免使用终结器;但如果包含一个终结器,则应该禁止显示生成警告并从 (On)(Begin)Close(和/或 OnAbort)调用 SuppressFinalize(Object) 和终结器本身,以便模拟已自动生成的 IDisposable 行为。Avoid using a finalizer; but if you include one, you should suppress the build warning and call SuppressFinalize(Object) and the finalizer itself from (On)(Begin)Close (and/or OnAbort) to emulate what would have been the auto-generated IDisposable behavior.
以编程方式添加行为时,可以在创建任何通道之前将行为添加到 Behaviors 上相应的 ChannelFactory 属性。When adding behaviors programmatically, the behavior is added to the appropriate Behaviors property on the ChannelFactory prior to the creation of any channel. 有关代码示例,请参见“示例”部分。See the example section for a code sample.
构造函数
| ChannelFactory() |
初始化 ChannelFactory 类的新实例。Initializes a new instance of the ChannelFactory class. |
属性
| Credentials |
获取客户端在通过由工厂产生的通道与服务终结点进行通信时使用的凭据。Gets the credentials used by clients to communicate a service endpoint over the channels produced by the factory. |
| DefaultCloseTimeout |
获取为完成关闭操作提供的默认时间间隔。Gets the default interval of time provided for a close operation to complete. |
| DefaultOpenTimeout |
获取为完成打开操作提供的默认时间间隔。Gets the default interval of time provided for an open operation to complete. |
| Endpoint |
获取由工厂产生的通道所要连接的服务终结点。Gets the service endpoint to which the channels produced by the factory connect. |
| IsDisposed |
获取一个值,该值指示通信对象是否已被释放。Gets a value that indicates whether the communication object has been disposed. (继承自 CommunicationObject) |
| State |
获取指示通信对象当前状态的值。Gets a value that indicates the current state of the communication object. (继承自 CommunicationObject) |
| ThisLock |
获取在状态转换过程中保护类实例的相互排斥锁。Gets the mutually exclusive lock that protects the class instance during a state transition. (继承自 CommunicationObject) |
方法
| Abort() |
使通信对象立即从其当前状态转换到正在关闭状态。Causes a communication object to transition immediately from its current state into the closing state. (继承自 CommunicationObject) |
| ApplyConfiguration(String) |
使用由指定配置文件提供的行为和通道工厂服务终结点中的那些行为来初始化通道工厂。Initializes the channel factory with the behaviors provided by a specified configuration file and with those in the service endpoint of the channel factory. |
| BeginClose(AsyncCallback, Object) |
开始一个异步操作以关闭通信对象。Begins an asynchronous operation to close a communication object. (继承自 CommunicationObject) |
| BeginClose(TimeSpan, AsyncCallback, Object) |
开始一个异步操作以在指定超时内关闭通信对象。Begins an asynchronous operation to close a communication object with a specified timeout. (继承自 CommunicationObject) |
| BeginOpen(AsyncCallback, Object) |
开始一个异步操作以打开通信对象。Begins an asynchronous operation to open a communication object. (继承自 CommunicationObject) |
| BeginOpen(TimeSpan, AsyncCallback, Object) |
开始一个异步操作以在指定时间间隔内打开通信对象。Begins an asynchronous operation to open a communication object within a specified interval of time. (继承自 CommunicationObject) |
| Close() |
使通信对象从其当前状态转换到关闭状态。Causes a communication object to transition from its current state into the closed state. (继承自 CommunicationObject) |
| Close(TimeSpan) |
使通信对象在指定时间间隔内从当前状态转换到关闭状态。Causes a communication object to transition from its current state into the closed state within a specified interval of time. (继承自 CommunicationObject) |
| CreateDescription() |
在派生类中实现时,创建与通道工厂关联的服务终结点说明。When implemented in a derived class, creates a description of the service endpoint associated with the channel factory. |
| CreateFactory() |
为工厂的当前终结点生成通道工厂。Builds the channel factory for the current endpoint of the factory. |
| EndClose(IAsyncResult) |
完成一个异步操作以关闭通信对象。Completes an asynchronous operation to close a communication object. (继承自 CommunicationObject) |
| EndOpen(IAsyncResult) |
完成一个异步操作以打开通信对象。Completes an asynchronous operation to open a communication object. (继承自 CommunicationObject) |
| EnsureOpened() |
打开尚未打开的当前通道工厂。Opens the current channel factory if it is not yet opened. |
| Equals(Object) |
确定指定对象是否等于当前对象。Determines whether the specified object is equal to the current object. (继承自 Object) |
| Fault() |
使通信对象从其当前状态转换到出错状态。Causes a communication object to transition from its current state into the faulted state. (继承自 CommunicationObject) |
| GetCommunicationObjectType() |
获取通信对象的类型。Gets the type of communication object. (继承自 CommunicationObject) |
| GetHashCode() |
作为默认哈希函数。Serves as the default hash function. (继承自 Object) |
| GetProperty<T>() |
从通道堆栈的适当层返回所请求的类型化对象,如果不存在,则返回 |
| GetType() |
获取当前实例的 Type。Gets the Type of the current instance. (继承自 Object) |
| InitializeEndpoint(Binding, EndpointAddress) |
使用指定的绑定和地址初始化通道工厂的服务终结点。Initializes the service endpoint of the channel factory with a specified binding and address. |
| InitializeEndpoint(ServiceEndpoint) |
使用指定的终结点初始化通道工厂的服务终结点。Initializes the service endpoint of the channel factory with a specified endpoint. |
| InitializeEndpoint(String, EndpointAddress) |
使用指定的地址和配置初始化通道工厂的服务终结点。Initializes the service endpoint of the channel factory with a specified address and configuration. |
| MemberwiseClone() |
创建当前 Object 的浅表副本。Creates a shallow copy of the current Object. (继承自 Object) |
| OnAbort() |
终止当前通道工厂的内部通道工厂。Terminates the inner channel factory of the current channel factory. |
| OnBeginClose(TimeSpan, AsyncCallback, Object) |
对当前通道工厂(具有关联的状态对象)的内部通道工厂,开始一个异步关闭操作。Begins an asynchronous close operation on the inner channel factory of the current channel factory that has a state object associated with it. |
| OnBeginOpen(TimeSpan, AsyncCallback, Object) |
对当前通道工厂(具有关联的状态对象)的内部通道工厂,开始一个异步打开操作。Begins an asynchronous open operation on the inner channel factory of the current channel factory that has a state object associated with it. |
| OnClose(TimeSpan) |
使用完成操作的指定超时对内部通道工厂调用 close。Calls close on the inner channel factory with a specified time-out for the completion of the operation. |
| OnClosed() |
在通信对象转换到正在关闭状态的过程中被调用。Invoked during the transition of a communication object into the closing state. (继承自 CommunicationObject) |
| OnClosing() |
在通信对象转换到正在关闭状态的过程中被调用。Invoked during the transition of a communication object into the closing state. (继承自 CommunicationObject) |
| OnEndClose(IAsyncResult) |
对当前通道工厂的内部通道工厂,完成一个异步关闭操作。Completes an asynchronous close operation on the inner channel factory of the current channel factory. |
| OnEndOpen(IAsyncResult) |
对当前通道工厂的内部通道工厂,完成一个异步打开操作。Completes an asynchronous open operation on the inner channel factory of the current channel factory. |
| OnFaulted() |
在调用了同步错误操作,从而引起通信对象转换为出错状态的情况下,该方法插入对通信对象的处理。Inserts processing on a communication object after it transitions to the faulted state due to the invocation of a synchronous fault operation. (继承自 CommunicationObject) |
| OnOpen(TimeSpan) |
使用完成操作的指定超时对当前通道工厂的内部通道工厂调用 open。Calls open on the inner channel factory of the current channel factory with a specified time-out for the completion of the operation. |
| OnOpened() |
为通道工厂初始化 ClientCredentials 对象的一个只读副本。Initializes a read-only copy of the ClientCredentials object for the channel factory. |
| OnOpening() |
为当前的通道生成内部通道工厂。Builds the inner channel factory for the current channel. |
| Open() |
使通信对象从已创建状态转换到已打开状态。Causes a communication object to transition from the created state into the opened state. (继承自 CommunicationObject) |
| Open(TimeSpan) |
使通信对象在指定时间间隔内从已创建状态转换到已打开状态。Causes a communication object to transition from the created state into the opened state within a specified interval of time. (继承自 CommunicationObject) |
| ThrowIfDisposed() |
如果通信对象已释放,则引发异常。Throws an exception if the communication object is disposed. (继承自 CommunicationObject) |
| ThrowIfDisposedOrImmutable() |
如果通信对象的 State 属性未设置为 Created 状态,则引发异常。Throws an exception if the communication object the State property is not set to the Created state. (继承自 CommunicationObject) |
| ThrowIfDisposedOrNotOpen() |
如果通信对象未处于 Opened 状态,则引发异常。Throws an exception if the communication object is not in the Opened state. (继承自 CommunicationObject) |
| ToString() |
返回表示当前对象的字符串。Returns a string that represents the current object. (继承自 Object) |
事件
| Closed |
当通信对象转换到已关闭状态时发生。Occurs when a communication object transitions into the closed state. (继承自 CommunicationObject) |
| Closing |
当通信对象转换到正在关闭状态时发生。Occurs when a communication object transitions into the closing state. (继承自 CommunicationObject) |
| Faulted |
在通信对象转换到出错状态时发生。Occurs when a communication object transitions into the faulted state. (继承自 CommunicationObject) |
| Opened |
当通信对象转换到已打开状态时发生。Occurs when a communication object transitions into the opened state. (继承自 CommunicationObject) |
| Opening |
当通信对象转换到正在打开状态时发生。Occurs when a communication object transitions into the opening state. (继承自 CommunicationObject) |
显式接口实现
| IDisposable.Dispose() |
关闭当前的通道工厂。Closes the current channel factory. |