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 IChannelFactory
    interface ICommunicationObject
    interface IDisposable
Public MustInherit Class ChannelFactory
Inherits CommunicationObject
Implements IChannelFactory, IDisposable
繼承
ChannelFactory
衍生
實作

範例

下列程式碼範例會示範如何以程式設計的方式插入用戶端行為,再由處理站建立通道物件。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.

給衍生自此類別之 Managed C++ 使用者的特別說明:Special note for Managed C++ users deriving from this class:

  • 請將您的清除程式碼置於 (On)(Begin)Close (and/or OnAbort),而不是解構函式。Put your cleanup code in (On)(Begin)Close (and/or OnAbort), not in a destructor.

  • 避免使用解構函式,因為它們會導致編譯器自動產生 IDisposableAvoid destructors; they cause the compiler to auto-generate IDisposable.

  • 避免使用非參考成員,因為它們會導致編譯器自動產生 IDisposableAvoid 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>()

從通道堆疊的適當層次傳回要求的型別物件 (如果存在的話),如果不存在則為 nullReturns the typed object requested, if present, from the appropriate layer in the channel stack, or null if not present.

GetType()

取得目前執行個體的 TypeGets 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 的淺層複本 (Shallow Copy)。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)

搭配完成作業的指定逾時,在內部通道處理站上呼叫關閉。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)

搭配完成作業的指定逾時,在目前通道處理站的內部通道處理站上呼叫開啟。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.

適用於