DuplexClientBase<TChannel> 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
用於建立雙工服務通道並將該通道與回呼物件相關聯。
generic <typename TChannel>
where TChannel : classpublic ref class DuplexClientBase abstract : System::ServiceModel::ClientBase<TChannel>
public abstract class DuplexClientBase<TChannel> : System.ServiceModel.ClientBase<TChannel> where TChannel : class
type DuplexClientBase<'Channel (requires 'Channel : null)> = class
inherit ClientBase<'Channel (requires 'Channel : null)>
Public MustInherit Class DuplexClientBase(Of TChannel)
Inherits ClientBase(Of TChannel)
類型參數
- TChannel
要建立的通道類型。
- 繼承
範例
下列範例示範在雙工 WCF 用戶端類型的 SampleDuplexHelloClient
用戶端中使用 ,以傳遞具有回呼物件的新 System.ServiceModel.InstanceContext 物件來接聽回呼。
using System;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.Threading;
namespace Microsoft.WCF.Documentation
{
[CallbackBehaviorAttribute(
IncludeExceptionDetailInFaults= true,
UseSynchronizationContext=true,
ValidateMustUnderstand=true
)]
public class Client : SampleDuplexHelloCallback
{
AutoResetEvent waitHandle;
public Client()
{
waitHandle = new AutoResetEvent(false);
}
public void Run()
{
// Picks up configuration from the configuration file.
SampleDuplexHelloClient wcfClient
= new SampleDuplexHelloClient(new InstanceContext(this), "WSDualHttpBinding_SampleDuplexHello");
try
{
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("Enter a greeting to send and press ENTER: ");
Console.Write(">>> ");
Console.ForegroundColor = ConsoleColor.Green;
string greeting = Console.ReadLine();
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("Called service with: \r\n\t" + greeting);
wcfClient.Hello(greeting);
Console.WriteLine("Execution passes service call and moves to the WaitHandle.");
this.waitHandle.WaitOne();
Console.ForegroundColor = ConsoleColor.Blue;
Console.WriteLine("Set was called.");
Console.Write("Press ");
Console.ForegroundColor = ConsoleColor.Red;
Console.Write("ENTER");
Console.ForegroundColor = ConsoleColor.Blue;
Console.Write(" to exit...");
Console.ReadLine();
}
catch (TimeoutException timeProblem)
{
Console.WriteLine("The service operation timed out. " + timeProblem.Message);
Console.ReadLine();
}
catch (CommunicationException commProblem)
{
Console.WriteLine("There was a communication problem. " + commProblem.Message);
Console.ReadLine();
}
}
public static void Main()
{
Client client = new Client();
client.Run();
}
public void Reply(string response)
{
Console.WriteLine("Received output.");
Console.WriteLine("\r\n\t" + response);
this.waitHandle.Set();
}
}
}
Imports System.ServiceModel
Imports System.ServiceModel.Channels
Imports System.Threading
Namespace Microsoft.WCF.Documentation
<CallbackBehaviorAttribute(IncludeExceptionDetailInFaults:= True, UseSynchronizationContext:=True, ValidateMustUnderstand:=True)> _
Public Class Client
Implements SampleDuplexHelloCallback
Private waitHandle As AutoResetEvent
Public Sub New()
waitHandle = New AutoResetEvent(False)
End Sub
Public Sub Run()
' Picks up configuration from the configuration file.
Dim wcfClient As New SampleDuplexHelloClient(New InstanceContext(Me), "WSDualHttpBinding_SampleDuplexHello")
Try
Console.ForegroundColor = ConsoleColor.White
Console.WriteLine("Enter a greeting to send and press ENTER: ")
Console.Write(">>> ")
Console.ForegroundColor = ConsoleColor.Green
Dim greeting As String = Console.ReadLine()
Console.ForegroundColor = ConsoleColor.White
Console.WriteLine("Called service with: " & Constants.vbCrLf & Constants.vbTab & greeting)
wcfClient.Hello(greeting)
Console.WriteLine("Execution passes service call and moves to the WaitHandle.")
Me.waitHandle.WaitOne()
Console.ForegroundColor = ConsoleColor.Blue
Console.WriteLine("Set was called.")
Console.Write("Press ")
Console.ForegroundColor = ConsoleColor.Red
Console.Write("ENTER")
Console.ForegroundColor = ConsoleColor.Blue
Console.Write(" to exit...")
Console.ReadLine()
Catch timeProblem As TimeoutException
Console.WriteLine("The service operation timed out. " & timeProblem.Message)
Console.ReadLine()
Catch commProblem As CommunicationException
Console.WriteLine("There was a communication problem. " & commProblem.Message)
Console.ReadLine()
End Try
End Sub
Public Shared Sub Main()
Dim client As New Client()
client.Run()
End Sub
Public Sub Reply(ByVal response As String) Implements SampleDuplexHelloCallback.Reply
Console.WriteLine("Received output.")
Console.WriteLine(Constants.vbCrLf & Constants.vbTab & response)
Me.waitHandle.Set()
End Sub
End Class
End Namespace
備註
使用 DuplexClientBase<TChannel> 類別來建立可指定回呼物件的服務通道。 物件會DuplexClientBase<TChannel>System.ServiceModel.DuplexChannelFactory<TChannel>包裝 物件,以提供WCF對象的連線能力。 您可以使用任何一種型別來連接雙工服務。 如需雙工服務的詳細資訊,請參閱雙工服務。
給衍生自此類別之 Managed C++ 使用者的特別說明:
請將您的清除程式碼置於 (On)(Begin)Close (and/or OnAbort),而不是解構函式。
避免使用解構函式:它們會導致編譯器自動產生 IDisposable
避免使用非參考成員:它們會導致編譯器自動產生 IDisposable
避免使用完成項;但是假如您加入一個完成項,就應該抑制建置警告並呼叫來自 (On)(Begin)Close (and/or OnAbort) 的 SuppressFinalize(Object) 和完成項,以模擬原本可能自動產生的 IDisposable 行為。
建構函式
屬性
Channel |
取得用來傳送訊息至多種不同設定服務端點的內部通道。 (繼承來源 ClientBase<TChannel>) |
ChannelFactory |
取得基礎 ChannelFactory<TChannel> 物件。 (繼承來源 ClientBase<TChannel>) |
ClientCredentials |
取得用來呼叫作業的用戶端認證。 (繼承來源 ClientBase<TChannel>) |
Endpoint |
取得 WCF 用戶端可連線之服務的目標端點。 (繼承來源 ClientBase<TChannel>) |
InnerChannel |
取得基礎 IClientChannel 實作。 (繼承來源 ClientBase<TChannel>) |
InnerDuplexChannel |
取得雙面通道的底層 IClientChannel 實作。 |
State |
取得 ClientBase<TChannel> 物件的目前狀態。 (繼承來源 ClientBase<TChannel>) |
方法
明確介面實作
擴充方法
CloseHelperAsync(ICommunicationObject, TimeSpan) |
用於建立雙工服務通道並將該通道與回呼物件相關聯。 |
OpenHelperAsync(ICommunicationObject, TimeSpan) |
用於建立雙工服務通道並將該通道與回呼物件相關聯。 |
ConfigureAwait(IAsyncDisposable, Boolean) |
設定如何執行從非同步可處置項目傳回的工作 await。 |
適用於
意見反應
https://aka.ms/ContentUserFeedback。
即將登場:在 2024 年,我們將逐步淘汰 GitHub 問題作為內容的意見反應機制,並將它取代為新的意見反應系統。 如需詳細資訊,請參閱:提交並檢視相關的意見反應