How to: Access a Duplex Service

 

How to: Access a Duplex Service

Silverlight

This topic describes how to create a Silverlight version 4 client that can access a Windows Communication Foundation (WCF) duplex service. It assumes that you have already built a WCF duplex service by completing the procedure described inHow to: Build a Duplex Service for a Silverlight Client, and it starts by assuming that you have that solution open. Independently of whether you chose the PollingDuplexHttpBinding or the NetTcpBinding binding in that tutorial, the steps here are the same, unless otherwise noted.

The procedures outlined here describe how to construct a Silverlight 4 client that sends an order to the duplex service and that is subsequently called back by that service twice: first, when the service starts to process the order; and second, when the order is completed.

To create the Silverlight client application

  1. To create a new Silverlight client project in the current solution for the Solution ‘DuplexService’, right-click the solution (not the project) in Solution Explorer (on the upper right), select Add, and thenNew Project.

  2. In the Add New Project dialog box, select Silverlight in your preferred programming language (C# or Visual Basic), then select theSilverlight Application template, and name it DuplexClient in theName box. Use the default location.

  3. Click OK.

  4. In the New Silverlight Application wizard, accept the default selection ofHost the Silverlight application in a new or existing Web site in the solution and the other defaults, and then clickOK.

To build a duplex client

  1. Right-click the DuplexClient project in Solution Explorer and selectAdd Service Reference. Click Discover to find the duplex service built previously. Check that the URL in theAddress box matches that of the duplex service. Type OrderService in theNamespace field and then click OK. This generates a proxy for the duplex service.

  2. Add the following using statement to the top of MainPage.xaml.cs in theDuplexClient project. The last statement references the namespace of the generated proxy.

    using System.ServiceModel;
    using System.ServiceModel.Channels;
    using DuplexClient.OrderService;
    
    
  3. If your service was built by using the PollingDuplexHttpBinding, create anEndpointAddress for the duplex service and instantiate thePollingDuplexHttpBinding used to communicate with the service by pasting the following code within the scope of theMainPage method, beneath the InitializeComponent() method.

    // 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(en-us,VS.95).gif Note:
    You must paste the correct address generated by your service (the one shown above, but with the port number changed to match the number generated when the service was deployed on your computer) into the code here for your client to access your service.

  4. If your service was configured with the NetTcpBinding element, the binding and address used to communicate with the service are already specified for you in the .ClientConfig file.

    // Instantiate the proxy, referencing the endpoint in the .ClientConfig file.
    
    DuplexServiceClient proxy = new DuplexServiceClient();
    
    
  5. Now call operations on the generated proxy. Paste the following code at the bottom of theMainPage method.

    // 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;
    
    

    First, you register a callback for the ReceiveReceived event. This event will be called whenever the service invokes theReceive method of the callback contract to send information back to the client. Then you instruct the service to start processing an order by calling theOrderAsync method. This method assumes that the reply is defined in aTextBox control in the Silverlight UI, which displays the service response.

  6. Now define the callback method to call when the service sends information to the client. Paste this inside the scope of theMainPage class.

    // 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;
                }
            }
    
        }
    }
    
    

    This will display the order status information returned by the service.

  7. To enable the display of this status, add a <TextBlock> control instead of the<Grid> element of the UserControl in MainPage.xaml as follows.

    // 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>
    
    

    Note that the only change to the default code made here is the addition of the<TextBlock x:Name="reply" /> instead of the Grid element.

  8. The Silverlight duplex client is now complete. To run the sample, right-click DuplexClientTestPage.html in Solution Explorer and selectView in Browser. You should see the following output.

    // 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(en-us,VS.95).gif Security Note:
    If the service is using a NetTcpBinding, make sure the sockets policy file that is described at the bottom of theNetwork Security Access Restrictions in Silverlight topic is available at the root of the host; otherwise the sample will fail. The TCP-based sockets policy retrieval on port 943 is not supported. There is no way to switch to TCP-based sockets policy retrieval when using the NetTcp binding.

Example

The following code sample summarizes the contents of the MainPage.xaml.cs file after completing the preceding procedure.

// 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;
                    }
                }

            }
        }

    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值