如何:访问双工服务

如何:访问双工服务

Silverlight

本主题描述了如何创建可访问 Windows Communication Foundation (WCF) 双工服务的 Silverlight 版本 4 客户端。本文假定您已完成如何:为 Silverlight 客户端生成双工服务中所述的过程,生成了 WCF 双工服务,并假定该解决方案处于打开状态。无论您在该教程中选择的是 PollingDuplexHttpBinding 还是NetTcpBinding 绑定,此处的步骤都是相同的(除非另有说明)。

本文中简要介绍的过程说明如何构造 Silverlight 4 客户端,该客户端向双工服务发送订单,随后由该服务回调两次:第一次是在该服务开始处理订单时,第二次是在订单完成时。

创建 Silverlight 客户端应用程序

  1. 若要在 Solution ‘DuplexService’的当前解决方案中创建新的 Silverlight 客户端项目,请在“解决方案资源管理器”(位于右上角)中右击该解决方案(而非项目),依次选择“添加”“新建项目”

  2. “添加新项目”对话框中,选择您的首选编程语言(C# 或 Visual Basic)的 Silverlight,然后选择“Silverlight 应用程序”模板,在“名称”框中将其命名为 DuplexClient。使用默认的位置。

  3. 单击“确定”

  4. “新建 Silverlight 应用程序”向导中,接受默认选择“在新网站或现有网站的解决方案中承载 Silverlight 应用程序”和其他默认选择,然后单击“确定”

构建双工客户端

  1. 右击“解决方案资源管理器”中的“DuplexClient”项目,然后选择“添加服务引用”。单击“发现”找到以前生成的双工服务。检查“地址”框中的 URL 是否与双工服务的 URL 匹配。在“命名空间”字段中键入 OrderService,然后单击“确定”。这将为双工服务生成代理。

  2. 将下列 using 语句添加至 DuplexClient 项目中 MainPage.xaml.cs 的顶部。最后一个语句引用生成的代理的命名空间。

    using System.ServiceModel;
    using System.ServiceModel.Channels;
    using DuplexClient.OrderService;
    
    
  3. 如果已使用 PollingDuplexHttpBinding 生成您的服务,则可通过将以下代码粘贴到 MainPage 方法(在 InitializeComponent() 方法的下面)的作用域内,为双工服务创建 EndpointAddress,并实例化用于与该服务进行通信的 PollingDuplexHttpBinding

    // Create the endpoint address and binding for the proxy and instantiate it.
    
    EndpointAddress address = new EndpointAddress("http://localhost:19021/Service1.svc");
    
    PollingDuplexHttpBinding binding = new PollingDuplexHttpBinding(PollingDuplexMode.MultipleMessagesPerPoll);
    
    DuplexServiceClient proxy = new DuplexServiceClient(binding, address);
    
    
    
    Ee844557.Important(zh-cn,VS.95).gif 注意:
    必须将该服务生成的正确地址(上面显示的地址,但是端口号要更改为与在您的计算机上部署该服务时生成的号码匹配)粘贴到此处的代码中,以便您的客户端访问该服务。

  4. 如果已使用 NetTcpBinding 元素配置服务,则在 .ClientConfig 文件中已为您指定了用于与该服务进行通信的绑定和地址。

    // Instantiate the proxy, referencing the endpoint in the .ClientConfig file.
    
    DuplexServiceClient proxy = new DuplexServiceClient();
    
    
  5. 现在对生成的代理调用操作。在 MainPage 方法的底部粘贴以下代码。

    // Register the callback for the ReceiveReceived event.
    proxy.ReceiveReceived += new EventHandler<ReceiveReceivedEventArgs>(proxy_ReceiveReceived);
    
    // Place the order.
    proxy.OrderAsync("Widget", 3);
    
    // Display the status of the order.
    reply.Text = "Sent order of 3 Widgets." + Environment.NewLine;
    
    

    首先,为 ReceiveReceived 事件注册回调。每当服务调用回调协定的 Receive 方法来将信息发送回客户端时,都将调用此事件。接着,您通过调用OrderAsync 方法指示服务开始处理订单。 此方法假定回复是在 Silverlight UI 的 TextBox 控件(用于显示服务响应)中定义的。

  6. 现在定义当服务向客户端发送信息时要调用的回调方法。在 MainPage 类的作用域中粘贴此方法。

    // Define the callback method.
    
    void proxy_ReceiveReceived(object sender, ReceiveReceivedEventArgs e)
    {
        if (e.Error == null)
        {
            reply.Text += "Service reports Widget order is " + e.order.Status + "." + Environment.NewLine;
    
            if (e.order.Status == OrderStatus.Completed)
            {
                reply.Text += "Here is the completed order:" + Environment.NewLine;
    
                foreach (string order in e.order.Payload)
                {
                    reply.Text += order + Environment.NewLine;
                }
            }
    
        }
    }
    
    

    这将显示由服务返回的订单状态信息。

  7. 若要启用此状态的显示,请按如下方式在 MainPage.xaml 中添加一个 <TextBlock> 控件,而不是 UserControl 的 <Grid> 元素。

    // XAML in the MainPage.xaml page.
    
    <UserControl x:Class="DuplexClient.MainPage"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        d:DesignHeight="300" d:DesignWidth="400">
        <TextBlock x:Name="reply" />
    </UserControl>
    
    

    请注意,此处对默认代码的唯一更改是添加了 <TextBlock x:Name="reply" />,而不是 Grid 元素。

  8. Silverlight 双工客户端现已完成。若要运行该示例,请在“解决方案资源管理器”中右击“DuplexClientTestPage.html”,然后选择“在浏览器中查看”。您将看到以下输出:

    // Output from the client.
    
    Sent order of 3 Widgets.
    Service reports Widget order is Processing.
    Service reports Widget order is Completed.
    Here is the completed order:
    Widget2
    Widget1
    Widget0
    
    
    Ee844557.security(zh-cn,VS.95).gif 安全性注意:
    如果该服务正在使用 NetTcpBinding,请确保在 Silverlight 中的网络安全访问限制主题底部描述的套接字策略文件在主机的根文件夹中可用,否则该示例将失败。不支持端口 943 上基于 TCP 的套接字策略检索。使用 NetTcp 绑定时,无法切换到基于 TCP 的套接字策略检索。

示例

下列代码示例汇总了完成上述过程后 MainPage.xaml.cs 文件的内容。

// Code in the MainPage.xaml.cs page.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ServiceModel;
using System.ServiceModel.Channels;
using DuplexClient.OrderService;


namespace DuplexClient
{
    public partial class MainPage : UserControl
    {

        public MainPage()
        {
            InitializeComponent();
 
            // If using the PollingDuplexHttpBinding on the service
            EndpointAddress address = new EndpointAddress("http://localhost:19021/Service1.svc");
            PollingDuplexHttpBinding binding = new PollingDuplexHttpBinding(PollingDuplexMode.MultipleMessagesPerPoll);

            DuplexServiceClient proxy = new DuplexServiceClient(binding, address);
            
            // If using a NetTcpBinding on the service
            // DuplexServiceClient proxy = new DuplexServiceClient();
            proxy.ReceiveReceived += new EventHandler<ReceiveReceivedEventArgs>(proxy_ReceiveReceived);
            proxy.OrderAsync("Widget", 3);
            reply.Text = "Sent order of 3 Widgets." + Environment.NewLine;

        }

        void proxy_ReceiveReceived(object sender, ReceiveReceivedEventArgs e)
        {
            if (e.Error == null)
            {
                reply.Text += "Service reports Widget order is " + e.order.Status + "." + Environment.NewLine;

                if (e.order.Status == OrderStatus.Completed)
                {
                    reply.Text += "Here is the completed order:" + Environment.NewLine;

                    foreach (string order in e.order.Payload)
                    {
                        reply.Text += order + Environment.NewLine;
                    }
                }

            }
        }

    }
}

posted @ 2011-09-07 10:00  Areas  阅读(307)  评论(0编辑  收藏  举报