客户端通道级编程

本主题介绍如何在不使用类及其关联的对象模型的情况下 System.ServiceModel.ClientBase<TChannel> 编写 Windows Communication Foundation (WCF) 客户端应用程序。

发送消息

若要准备好发送消息和接收和处理答复,需要执行以下步骤:

  1. 创建绑定。

  2. 生成通道工厂。

  3. 创建频道。

  4. 发送请求并读取回复。

  5. 关闭所有通道对象。

创建绑定

与接收案例(请参阅 服务 Channel-Level 编程)类似,发送消息从创建绑定开始。 本示例创建一个新的 System.ServiceModel.Channels.CustomBinding,并将一个 System.ServiceModel.Channels.HttpTransportBindingElement 添加到其 Elements 集合中。

生成 ChannelFactory

这次我们不是创建一个System.ServiceModel.Channels.IChannelListener,而是通过调用ChannelFactory.CreateFactory类型参数所在的System.ServiceModel.Channels.IRequestChannel绑定来创建一个System.ServiceModel.ChannelFactory<TChannel>。 那些等待传入消息的一方使用通道侦听器,而那些启动通信以创建通道的一方使用通道工厂。 和通道侦听器相似,必须先打开通道工厂之后才能使用通道工厂。

创建频道

然后,我们调用 ChannelFactory<TChannel>.CreateChannel 创建一个 IRequestChannel。 此调用获取我们通过所创建的新通道进行通信的终结点地址。 一旦我们有了一个频道,就调用Open使其进入通信状态。 根据传输的性质,对 Open 的此调用可能会启动与目标终结点的连接,或者可能根本不在网络上执行任何作。

发送请求并阅读答复

打开通道后,可以创建一条消息,并使用通道的请求方法发送请求,并等待回复返回。 此方法返回时,我们有一条回复消息,我们可以阅读该消息来找出终结点的答复内容。

关闭对象

为了避免资源泄露,我们会在不再需要时关闭用于通信的对象。

下面的代码示例演示了一个基本客户端,该客户端使用通道工厂发送消息并读取答复。

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