本主題描述如何在不使用 System.ServiceModel.ClientBase<TChannel> 類別及其相關聯的物件模型的情況下撰寫 Windows Communication Foundation (WCF) 用戶端應用程式。
傳送訊息
若要準備好傳送訊息和接收和處理回復,您需要下列步驟:
建立系結。
建置通道處理站。
建立通道。
傳送要求並讀取回復。
關閉所有通道物件。
建立系結
類似於接收案例(請參閱 Service Channel-Level 程式設計),傳送訊息會從建立系結開始。 這個範例會建立新的 System.ServiceModel.Channels.CustomBinding,並將 System.ServiceModel.Channels.HttpTransportBindingElement 加入至其 Elements 集合。
建立通道工廠
這次我們會呼叫 System.ServiceModel.Channels.IChannelListener 在類型參數 System.ServiceModel.ChannelFactory<TChannel>的系結上,並建立 ChannelFactory.CreateFactory,而不是建立 System.ServiceModel.Channels.IRequestChannel。 當通道監聽器由等待接收訊息的一方使用時,通道工廠會由發起通訊以建立通道的一方使用。 就像通道接聽程式一樣,通道處理站必須先開啟,才能使用。
建立通道
然後,我們會呼叫 ChannelFactory<TChannel>.CreateChannel 來建立 IRequestChannel。 此呼叫會採用我們想要使用所建立新通道進行通訊的端點位址。 一旦我們有通道,我們會在通道上呼叫 Open,使其處於準備好進行通訊的狀態。 根據傳輸的性質,對 Open 的呼叫可能會起始與目標端點的連線,也可能在網路上完全不執行任何動作。
傳送要求並讀取回復
開啟通道之後,我們可以建立訊息,並使用通道的 Request 方法來傳送要求,並等候回復回來。 當此方法傳回時,我們有一則回復訊息,可讀取以找出端點的回復內容。
關閉物件
為了避免資源流失,我們會在不再需要物件時關閉通訊中使用的物件。
下列程式代碼範例示範使用通道處理站傳送訊息並讀取回復的基本用戶端。
using System;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Configuration;
namespace ProgrammingChannels
{
class Client
{
static void RunClient()
{
//Step1: Create a binding with just HTTP.
BindingElement[] bindingElements = new BindingElement[2];
bindingElements[0] = new TextMessageEncodingBindingElement();
bindingElements[1] = new HttpTransportBindingElement();
CustomBinding binding = new CustomBinding(bindingElements);
//Step2: Use the binding to build the channel factory.
IChannelFactory<IRequestChannel> factory =
binding.BuildChannelFactory<IRequestChannel>(
new BindingParameterCollection());
//Open the channel factory.
factory.Open();
//Step3: Use the channel factory to create a channel.
IRequestChannel channel = factory.CreateChannel(
new EndpointAddress("http://localhost:8080/channelapp"));
channel.Open();
//Step4: Create a message.
Message requestmessage = Message.CreateMessage(
binding.MessageVersion,
"http://contoso.com/someaction",
"This is the body data");
//Send message.
Message replymessage = channel.Request(requestmessage);
Console.WriteLine("Reply message received");
Console.WriteLine($"Reply action: {replymessage.Headers.Action}");
string data = replymessage.GetBody<string>();
Console.WriteLine($"Reply content: {data}");
//Step5: Do not forget to close the message.
replymessage.Close();
//Do not forget to close the channel.
channel.Close();
//Do not forget to close the factory.
factory.Close();
}
public static void Main()
{
Console.WriteLine("Press [ENTER] when service is ready");
Console.ReadLine();
RunClient();
Console.WriteLine("Press [ENTER] to exit");
Console.ReadLine();
}
}
}
Imports System.ServiceModel
Imports System.ServiceModel.Channels
Imports System.ServiceModel.Configuration
Namespace ProgrammingChannels
Friend Class Client
Private Shared Sub RunClient()
'Step1: Create a binding with just HTTP.
Dim bindingElements(1) As BindingElement = {New TextMessageEncodingBindingElement(), _
New HttpTransportBindingElement()}
Dim binding As New CustomBinding(bindingElements)
'Step2: Use the binding to build the channel factory.
Dim factory = binding.BuildChannelFactory(Of IRequestChannel)(New BindingParameterCollection())
'Open the channel factory.
factory.Open()
'Step3: Use the channel factory to create a channel.
Dim channel = factory.CreateChannel(New EndpointAddress("http://localhost:8080/channelapp"))
channel.Open()
'Step4: Create a message.
Dim requestmessage = Message.CreateMessage(binding.MessageVersion, _
"http://contoso.com/someaction", _
"This is the body data")
'Send message.
Dim replymessage = channel.Request(requestmessage)
Console.WriteLine("Reply message received")
Console.WriteLine("Reply action: {0}", replymessage.Headers.Action)
Dim data = replymessage.GetBody(Of String)()
Console.WriteLine("Reply content: {0}", data)
'Step5: Do not forget to close the message.
replymessage.Close()
'Do not forget to close the channel.
channel.Close()
'Do not forget to close the factory.
factory.Close()
End Sub
Public Shared Sub Main()
Console.WriteLine("Press [ENTER] when service is ready")
Console.ReadLine()
RunClient()
Console.WriteLine("Press [ENTER] to exit")
Console.ReadLine()
End Sub
End Class
End Namespace