ChannelFactory 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
建立和管理由用戶端用於傳送訊息至服務端點的通道。
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 IChannelFactory
interface ICommunicationObject
interface IDisposable
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
- 繼承
- 衍生
- 實作
範例
下列程式碼範例會示範如何以程式設計的方式插入用戶端行為,再由處理站建立通道物件。
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/or OnAbort),而不是解構函式。
避免使用解構函式,因為它們會導致編譯器自動產生 IDisposable。
避免使用非參考成員,因為它們會導致編譯器自動產生 IDisposable。
避免使用完成項,但如果您要加入完成項,則應隱藏建置警告並從 (On)(Begin)Close (和/或 OnAbort) 呼叫 SuppressFinalize(Object) 和完成項本身,以模擬原本可能自動產生的 IDisposable 行為。
以程式設計的方式加入行為時,行為會加入至 Behaviors
上的適當 ChannelFactory 屬性,再建立任何通道。 如需程式碼範例,請參閱「範例」一節。
建構函式
ChannelFactory() |
初始化 ChannelFactory 類別的新執行個體。 |
屬性
Credentials |
取得認證,用戶端會在處理站產生的通道上使用該認證與服務端點通訊。 |
DefaultCloseTimeout |
取得針對要完成之關閉作業所提供的預設時間間隔。 |
DefaultOpenTimeout |
取得針對要完成之開啟作業所提供的預設時間間隔。 |
Endpoint |
取得由處理站產生的通道所要連接的服務端點。 |
IsDisposed |
取得值,這個值會指出是否已經處置通訊物件。 (繼承來源 CommunicationObject) |
State |
取得值,這個值表示通訊物件目前的狀態。 (繼承來源 CommunicationObject) |
ThisLock |
取得會在狀態轉換期間保護類別執行個體的互斥鎖定。 (繼承來源 CommunicationObject) |
方法
事件
Closed |
當通訊物件轉換至已關閉狀態時發生。 (繼承來源 CommunicationObject) |
Closing |
當通訊物件轉換至關閉狀態時就會發生。 (繼承來源 CommunicationObject) |
Faulted |
當通訊物件轉換至錯誤狀態時發生。 (繼承來源 CommunicationObject) |
Opened |
當通訊物件轉換至已開啟狀態時發生。 (繼承來源 CommunicationObject) |
Opening |
當通訊物件轉換至開啟狀態時發生。 (繼承來源 CommunicationObject) |
明確介面實作
IAsyncCommunicationObject.CloseAsync(TimeSpan) |
建立和管理由用戶端用於傳送訊息至服務端點的通道。 (繼承來源 CommunicationObject) |
IAsyncCommunicationObject.OpenAsync(TimeSpan) |
建立和管理由用戶端用於傳送訊息至服務端點的通道。 (繼承來源 CommunicationObject) |
IAsyncDisposable.DisposeAsync() |
建立和管理由用戶端用於傳送訊息至服務端點的通道。 |
IDisposable.Dispose() |
關閉目前的通道處理站。 |
擴充方法
CloseHelperAsync(ICommunicationObject, TimeSpan) |
建立和管理由用戶端用於傳送訊息至服務端點的通道。 |
OpenHelperAsync(ICommunicationObject, TimeSpan) |
建立和管理由用戶端用於傳送訊息至服務端點的通道。 |
GetInternalCloseTimeout(CommunicationObject) |
建立和管理由用戶端用於傳送訊息至服務端點的通道。 |
ConfigureAwait(IAsyncDisposable, Boolean) |
設定如何執行從非同步可處置項目傳回的工作 await。 |
適用於
意見反應
https://aka.ms/ContentUserFeedback。
即將登場:在 2024 年,我們將逐步淘汰 GitHub 問題作為內容的意見反應機制,並將它取代為新的意見反應系統。 如需詳細資訊,請參閱:提交並檢視相關的意見反應