用戶端通道層級的程式設計
本主題說明如何撰寫 Windows Communication Foundation (WCF) 用戶端應用程式,而不使用 System.ServiceModel.ClientBase<TChannel> 類別及其相關的物件模型。
傳送訊息
以下為準備傳送訊息及接收和處理回覆所需的步驟:
建立繫結。
建置通道處理站。
建立通道。
傳送要求和讀取回覆。
關閉所有通道物件。
建立繫結。
與接收案例類似 (請參閱服務通道層級的程式設計),傳送訊息也是由建立繫結開始。 這個範例會建立新的 System.ServiceModel.Channels.CustomBinding,並將 System.ServiceModel.Channels.HttpTransportBindingElement 加入至其 Elements 集合。
建置 ChannelFactory
這次我們不會建立 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: {0}",
replymessage.Headers.Action);
string data = replymessage.GetBody<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();
}
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