ChannelFactory<TChannel> 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
此處理站可建立不同型別的通道,用戶端使用這些通道將訊息傳送給各種已設定的服務端點。
generic <typename TChannel>
public ref class ChannelFactory : System::ServiceModel::ChannelFactory, System::ServiceModel::Channels::IChannelFactory<TChannel>
public class ChannelFactory<TChannel> : System.ServiceModel.ChannelFactory, System.ServiceModel.Channels.IChannelFactory<TChannel>
type ChannelFactory<'Channel> = class
inherit ChannelFactory
interface IChannelFactory<'Channel>
interface IChannelFactory
interface ICommunicationObject
type ChannelFactory<'Channel> = class
inherit ChannelFactory
interface IChannelFactory
interface ICommunicationObject
interface IChannelFactory<'Channel>
Public Class ChannelFactory(Of TChannel)
Inherits ChannelFactory
Implements IChannelFactory(Of TChannel)
類型參數
- TChannel
由通道處理站產生的通道型別。 這個型別必須是 IOutputChannel 或 IRequestChannel。
- 繼承
- 衍生
- 實作
範例
下列範例說明如何建立通道處理站,並使用它建立並管理通道。
BasicHttpBinding binding = new BasicHttpBinding();
EndpointAddress address = new EndpointAddress("http://localhost:8000/ChannelApp");
ChannelFactory<IRequestChannel> factory =
new ChannelFactory<IRequestChannel>(binding, address);
IRequestChannel channel = factory.CreateChannel();
channel.Open();
Message request = Message.CreateMessage(MessageVersion.Soap11, "hello");
Message reply = channel.Request(request);
Console.Out.WriteLine(reply.Headers.Action);
reply.Close();
channel.Close();
factory.Close();
}
下列程式碼範例會示範如何以程式設計的方式插入用戶端行為,再由處理站建立通道物件。
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
備註
此泛型類別可啟用其中需要建立通道處理站的更進階案例,以用來建立多種通道型別。
以程式設計的方式加入行為時,行為會加入至 Behaviors
上的適當 ChannelFactory 屬性,再建立任何通道。 如需程式碼範例,請參閱「範例」一節。
使用此型別的用戶端或呼叫應用程式 (例如,中間層應用程式也是用戶端應用程式),除了效能需求之外,常常也有複雜的狀態管理需求。 如需這些案例的詳細資訊,請參閱 仲介層用戶端應用程式。
建構函式
ChannelFactory<TChannel>() |
初始化 ChannelFactory<TChannel> 類別的新執行個體。 |
ChannelFactory<TChannel>(Binding) |
初始化 ChannelFactory<TChannel> 類別的新執行個體。 |
ChannelFactory<TChannel>(Binding, EndpointAddress) |
使用指定的繫結與端點位址,來初始化 ChannelFactory<TChannel> 類別的新執行個體。 |
ChannelFactory<TChannel>(Binding, String) |
使用指定的繫結與遠端位址,來初始化 ChannelFactory<TChannel> 類別的新執行個體。 |
ChannelFactory<TChannel>(ServiceEndpoint) |
初始化 ChannelFactory<TChannel> 類別的新執行個體,以使用指定端點來產生通道。 |
ChannelFactory<TChannel>(String) |
使用指定的端點組態名稱,來初始化 ChannelFactory<TChannel> 類別的新執行個體。 |
ChannelFactory<TChannel>(String, EndpointAddress) |
以指定的端點組態名稱與遠端位址,來初始化 ChannelFactory<TChannel> 類別的新執行個體。 |
ChannelFactory<TChannel>(Type) |
初始化 ChannelFactory<TChannel> 類別的新執行個體。 |
屬性
Credentials |
取得認證,用戶端會在處理站產生的通道上使用該認證與服務端點通訊。 (繼承來源 ChannelFactory) |
DefaultCloseTimeout |
取得針對要完成之關閉作業所提供的預設時間間隔。 (繼承來源 ChannelFactory) |
DefaultOpenTimeout |
取得針對要完成之開啟作業所提供的預設時間間隔。 (繼承來源 ChannelFactory) |
Endpoint |
取得由處理站產生的通道所要連接的服務端點。 (繼承來源 ChannelFactory) |
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() |
此處理站可建立不同型別的通道,用戶端使用這些通道將訊息傳送給各種已設定的服務端點。 (繼承來源 ChannelFactory) |
IDisposable.Dispose() |
關閉目前的通道處理站。 (繼承來源 ChannelFactory) |
擴充方法
CloseHelperAsync(ICommunicationObject, TimeSpan) |
此處理站可建立不同型別的通道,用戶端使用這些通道將訊息傳送給各種已設定的服務端點。 |
OpenHelperAsync(ICommunicationObject, TimeSpan) |
此處理站可建立不同型別的通道,用戶端使用這些通道將訊息傳送給各種已設定的服務端點。 |
GetInternalCloseTimeout(CommunicationObject) |
此處理站可建立不同型別的通道,用戶端使用這些通道將訊息傳送給各種已設定的服務端點。 |
ConfigureAwait(IAsyncDisposable, Boolean) |
設定如何執行從非同步可處置項目傳回的工作 await。 |
適用於
執行緒安全性
此型別具備執行緒安全。
意見反應
https://aka.ms/ContentUserFeedback。
即將登場:在 2024 年,我們將逐步淘汰 GitHub 問題作為內容的意見反應機制,並將它取代為新的意見反應系統。 如需詳細資訊,請參閱:提交並檢視相關的意見反應