Compartir a través de


How to: Access a Duplex Service

Microsoft Silverlight will reach end of support after October 2021. Learn more.

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 in How 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 then New Project.

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

  3. Click OK.

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

To build a duplex client

  1. Right-click the DuplexClient project in Solution Explorer and select Add Service Reference. Click Discover to find the duplex service built previously. Check that the URL in the Address box matches that of the duplex service. Type OrderService in the Namespace 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 the DuplexClient 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 an EndpointAddress for the duplex service and instantiate the PollingDuplexHttpBinding used to communicate with the service by pasting the following code within the scope of the MainPage method, beneath the InitializeComponent() method.

    // Create the endpoint address and binding for the proxy and instantiate it.
    
    EndpointAddress address = new EndpointAddress("https://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 the MainPage 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 the Receive method of the callback contract to send information back to the client. Then you instruct the service to start processing an order by calling the OrderAsync method. This method assumes that the reply is defined in a TextBox 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 the MainPage 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="https://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="https://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="https://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 select View 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 the Network 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("https://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;
                    }
                }

            }
        }

    }
}

See Also

Send comments about this topic to Microsoft.

Copyright © 2010 by Microsoft Corporation. All rights reserved.