ChannelFactory 類別

定義

建立和管理用戶端用來將訊息傳送至服務端點的通道。

public ref class ChannelFactory abstract : System::ServiceModel::Channels::CommunicationObject, IDisposable, System::ServiceModel::Channels::IChannelFactory
public ref class ChannelFactory abstract : System::ServiceModel::Channels::CommunicationObject, IAsyncDisposable, IDisposable, System::ServiceModel::Channels::IChannelFactory
public abstract class ChannelFactory : System.ServiceModel.Channels.CommunicationObject, IDisposable, System.ServiceModel.Channels.IChannelFactory
public abstract class ChannelFactory : System.ServiceModel.Channels.CommunicationObject, IAsyncDisposable, IDisposable, System.ServiceModel.Channels.IChannelFactory
type ChannelFactory = class
    inherit CommunicationObject
    interface IDisposable
    interface IChannelFactory
    interface ICommunicationObject
type ChannelFactory = class
    inherit CommunicationObject
    interface IDisposable
    interface IChannelFactory
    interface ICommunicationObject
    interface IAsyncDisposable
type ChannelFactory = class
    inherit CommunicationObject
    interface IChannelFactory
    interface ICommunicationObject
    interface IDisposable
Public MustInherit Class ChannelFactory
Inherits CommunicationObject
Implements IChannelFactory, IDisposable
Public MustInherit Class ChannelFactory
Inherits CommunicationObject
Implements IAsyncDisposable, IChannelFactory, IDisposable
繼承
ChannelFactory
衍生
實作

範例

下列程式代碼範例示範如何在 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 介面及其相關聯通道的通道處理站通常會由通訊模式的啟動器使用。 實作 IChannelListener 介面及其相關聯接聽程式的接聽程式處理站會提供通道接受通訊的機制。

這個類別不是通道模型的一部分,而是服務模型的一部分。 CreateFactory 方法提供為服務端點建立 IChannelFactory 的方法。 使用它來建構用戶端,該用戶端會連結到服務上的介面合約,而不需使用元數據或原則。

注意

ChannelFactory.Credentials.Windows.AllowedImpersonationLevel 設定為 TokenImpersonationLevel.Anonymous 一律會導致匿名登入,而不論模擬層級為何。

衍生自此類別之 Managed C++使用者的特殊注意事項:

  • 將您的清除程序代碼放在 (On)(Begin)Close (and/或 OnAbort),而不是在解構函式中。

  • 避免解構函式;它們會導致編譯程式自動產生 IDisposable

  • 避免非參考成員;它們可能會導致編譯程式自動產生 IDisposable

  • 避免使用完成項;但是,如果您包含一個,您應該隱藏組建警告,並從 (On)(Begin)Close (and/或 OnAbort) 呼叫 SuppressFinalize(Object) 和完成項本身,以模擬自動產生的 IDisposable 行為。

以程序設計方式新增行為時,行為會在建立任何通道之前,新增至 ChannelFactory 上適當的 Behaviors 屬性。 如需程式代碼範例,請參閱範例一節。

建構函式

ChannelFactory()

初始化 ChannelFactory 類別的新實例。

屬性

Credentials

取得客戶端用來透過處理站所產生的通道來通訊服務端點的認證。

DefaultCloseTimeout

取得為關閉作業完成所提供的預設時間間隔。

DefaultOpenTimeout

取得開啟作業要完成的預設時間間隔。

Endpoint

取得處理站所產生通道所連線的服務端點。

IsDisposed

取得值,這個值表示是否已處置通訊物件。

(繼承來源 CommunicationObject)
State

取得值,這個值表示通訊物件的目前狀態。

(繼承來源 CommunicationObject)
ThisLock

取得在狀態轉換期間保護類別實例的互斥鎖定。

(繼承來源 CommunicationObject)

方法

Abort()

讓通訊物件立即從其目前狀態轉換成關閉狀態。

(繼承來源 CommunicationObject)
ApplyConfiguration(String)

使用指定組態檔所提供的行為以及通道處理站服務端點中的行為,初始化通道處理站。

BeginClose(AsyncCallback, Object)

開始異步操作以關閉通訊物件。

(繼承來源 CommunicationObject)
BeginClose(TimeSpan, AsyncCallback, Object)

開始異步操作,以關閉具有指定逾時的通訊物件。

(繼承來源 CommunicationObject)
BeginOpen(AsyncCallback, Object)

開始異步操作以開啟通訊物件。

(繼承來源 CommunicationObject)
BeginOpen(TimeSpan, AsyncCallback, Object)

開始異步操作,以在指定的時間間隔內開啟通訊物件。

(繼承來源 CommunicationObject)
Close()

使通訊物件從其目前狀態轉換成已關閉狀態。

(繼承來源 CommunicationObject)
Close(TimeSpan)

使通訊物件在指定的時間間隔內,從目前狀態轉換成關閉狀態。

(繼承來源 CommunicationObject)
CreateDescription()

在衍生類別中實作時,建立與通道處理站相關聯的服務端點描述。

CreateFactory()

建置處理站目前端點的通道處理站。

EndClose(IAsyncResult)

完成異步操作以關閉通訊物件。

(繼承來源 CommunicationObject)
EndOpen(IAsyncResult)

完成異步操作以開啟通訊物件。

(繼承來源 CommunicationObject)
EnsureOpened()

如果尚未開啟,請開啟目前的通道處理站。

Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
Fault()

導致通訊物件從其目前狀態轉換成錯誤狀態。

(繼承來源 CommunicationObject)
GetCommunicationObjectType()

取得通訊物件的型別。

(繼承來源 CommunicationObject)
GetHashCode()

做為預設哈希函式。

(繼承來源 Object)
GetProperty<T>()

從通道堆疊中適當的圖層傳回所要求的具型別物件,如果不存在,則傳回 null

GetType()

取得目前實例的 Type

(繼承來源 Object)
InitializeEndpoint(Binding, EndpointAddress)

使用指定的系結和位址,初始化通道處理站的服務端點。

InitializeEndpoint(ServiceEndpoint)

使用指定的端點,初始化通道處理站的服務端點。

InitializeEndpoint(String, EndpointAddress)

使用指定的位址和組態,初始化通道處理站的服務端點。

MemberwiseClone()

建立目前 Object的淺層複本。

(繼承來源 Object)
OnAbort()

終止目前通道處理站的內部通道處理站。

OnBeginClose(TimeSpan, AsyncCallback, Object)

在目前通道處理站的內部通道處理站上開始異步關閉作業,該處理站具有與其相關聯的狀態物件。

OnBeginOpen(TimeSpan, AsyncCallback, Object)

在目前通道處理站的內部通道處理站上開始異步開啟作業,該處理站具有與其相關聯的狀態物件。

OnClose(TimeSpan)

在內部通道處理站上呼叫關閉,並指定逾時以完成作業。

OnClosed()

在將通訊物件轉換成關閉狀態期間叫用。

(繼承來源 CommunicationObject)
OnClosing()

在將通訊物件轉換成關閉狀態期間叫用。

(繼承來源 CommunicationObject)
OnEndClose(IAsyncResult)

在目前通道處理站的內部通道處理站上完成異步關閉作業。

OnEndOpen(IAsyncResult)

在目前通道處理站的內部通道處理站上完成異步開啟作業。

OnFaulted()

在通訊物件轉換到錯誤狀態之後,插入處理,因為同步錯誤作業的叫用。

(繼承來源 CommunicationObject)
OnOpen(TimeSpan)

在目前通道處理站的內部通道處理站上呼叫開啟,並指定逾時以完成作業。

OnOpened()

初始化通道處理站 ClientCredentials 物件的唯讀複本。

OnOpening()

建置目前通道的內部通道處理站。

Open()

使通訊物件從已建立的狀態轉換成開啟的狀態。

(繼承來源 CommunicationObject)
Open(TimeSpan)

使通訊物件在指定的時間間隔內,從建立的狀態轉換成開啟的狀態。

(繼承來源 CommunicationObject)
ThrowIfDisposed()

如果處置通訊物件,則會擲回例外狀況。

(繼承來源 CommunicationObject)
ThrowIfDisposedOrImmutable()

如果通訊物件 State 屬性未設定為 Created 狀態,則會擲回例外狀況。

(繼承來源 CommunicationObject)
ThrowIfDisposedOrNotOpen()

如果通訊物件不是處於 Opened 狀態,則會擲回例外狀況。

(繼承來源 CommunicationObject)
ToString()

傳回表示目前 物件的字串。

(繼承來源 Object)

事件

Closed

當通訊物件轉換成關閉狀態時發生。

(繼承來源 CommunicationObject)
Closing

當通訊物件轉換成關閉狀態時發生。

(繼承來源 CommunicationObject)
Faulted

當通訊物件轉換成錯誤狀態時發生。

(繼承來源 CommunicationObject)
Opened

當通訊物件轉換成開啟的狀態時發生。

(繼承來源 CommunicationObject)
Opening

當通訊物件轉換成開啟狀態時發生。

(繼承來源 CommunicationObject)

明確介面實作

IAsyncDisposable.DisposeAsync()

建立和管理用戶端用來將訊息傳送至服務端點的通道。

IDisposable.Dispose()

關閉目前的通道處理站。

擴充方法

ConfigureAwait(IAsyncDisposable, Boolean)

設定如何執行從異步可處置專案傳回的工作等候。

適用於