Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.

Channels ▼

Web Development

Pushing Data to a Silverlight Client with a WCF Duplex Service: Part I

Creating the Service Factory

Once the service class is created a service factory can be created along with a service host. The factory is responsible for creating the appropriate host while the host defines the service endpoint. An example of creating service factory and host classes is shown next:

using System;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Channels;
using System.ServiceModel.Configuration;

namespace WCFPushService
    public class PollingDuplexServiceHostFactory : ServiceHostFactoryBase
        public override ServiceHostBase CreateServiceHost(string constructorString,
            Uri[] baseAddresses)
            return new PollingDuplexServiceHost(baseAddresses);

    class PollingDuplexServiceHost : ServiceHost
        public PollingDuplexServiceHost(params System.Uri[] addresses)
            base.InitializeDescription(typeof(GameStreamService), new UriSchemeKeyedCollection(addresses));

        protected override void InitializeRuntime()
            // Define the binding and set time-outs
            PollingDuplexBindingElement bindingElement = new PollingDuplexBindingElement()
                PollTimeout = TimeSpan.FromSeconds(10),
                InactivityTimeout = TimeSpan.FromMinutes(1)

            // Add an endpoint for the given service contract
                new CustomBinding(
                    new TextMessageEncodingBindingElement(
                    new HttpTransportBindingElement()),


This code was pulled directly from the Silverlight SDK example which provides a great starting point for creating WCF/Silverlight polling duplex services. The service factory class (PollingDuplexServiceHostFactory) creates a new instance of the service host class (PollingDuplexServiceHost) within the CreateServiceHost() method. The service host class then overrides the InitializeRuntime() method and creates a PollingDuplexBindingElement instance which defines the client's polling and inactivity timeouts.

The Silverlight SDK states the following about the PollingDuplexBindingElement class's PollTimeout and InactivityTimeout properties:

The PollTimeout property determines the length of time (in milliseconds) that the service holds a poll from the client before returning. The InactivityTimeout property determines the length of time (in milliseconds) that can elapse without any message exchange with the client before the service closes its session.

The PollingDuplexBindingElement class is located in an assembly named System.ServiceModel.PollingDuplex.dll which is part of the Silverlight SDK. You'll need to reference the assembly in your WCF project as well as the System.ServiceModel.Channels namespace to use the PollingDuplexBindingElement class. Once the binding element is created a call is made to the host object's AddServiceEndPoint() method which references the PollingDuplexBindingElement object and the server's IGameStreamService interface to create a custom binding that uses HTTP under the covers for message exchange.

Once the factory and service classes are created the factory can be referenced in the service's .svc file in the following manner:

<%@ ServiceHost Language="C#" Factory="WCFPushService.PollingDuplexServiceHostFactory" %>

Looking through all of the code you can see that there's definitely some initial setup work required to get a Silverlight callable WCF duplex service created. Since the client does have to poll the service to check for queued messages you may wonder what the benefit is over writing a manual polling Silverlight client that calls a WCF service. Microsoft's Scott Guthrie was kind enough to provide additional details on that subject. Here's what he had to say:

The duplex support does use polling in the background to implement notifications -- although the way it does it is different than manual polling. It initiates a network request, and then the request is effectively "put to sleep" waiting for the server to respond (it doesn't come back immediately). The server then keeps the connection open but not active until it has something to send back (or the connection times out after 90 seconds -- at which point the duplex client will connect again and wait). This way you are avoiding hitting the server repeatedly -- but still get an immediate response when there is data to send.

When the client polls in the background it sends the following message to the server:

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
        <wsmc:MakeConnection xmlns:wsmc="http://docs.oasis-open.org/ws-rx/wsmc/200702">

Next Time

In the next article I demonstrate how to call a WCF polling duplex service and listen for data in a Silverlight 2 application. Here's an example of the Silverlight interface that I'll discuss:

[Click image to view at full size]
Figure 1:A Silverlight 2 interface that has data pushed to it from a socket server.

In the meantime, you can download the Silverlight 2 Beta 2 sample application including the WCF service and Silverlight client here.

Related Reading

More Insights

Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.