Silverlight 3’s New and Improved Duplex Networking

One of the many cool features added to the run-time in Silverlight 2 was support for WCF duplex services. That support allowed Silverlight clients to connect to duplex services and receive asynchronous callbacks through a callback channel. This made it possible to write Silverlight apps that, for example, update stock prices in near real time as the prices change on the server without explicitly polling for the data. (Polling is happening down in the network layer, but that’s abstracted out of the programming model.)

Unfortunately, writing a duplex client in Silverlight 2 was messy. One reason was that you had to write a boatload of code to connect to the service and service the callback channel. Another was that there was no built-in serialization support for method parameters, so all data transfer was Message-based.

Silverlight 3 dramatically improved the duplex client story by introducing strongly typed proxies for duplex services. The result? You can now write a complete duplex client with as little as 10 lines of code. You simply use Visual Studio’s “Add Service Reference” command (or the command-line equivalent, SlSvcUtil.exe, also new in Silverlight 3) to generate a proxy for the duplex service and then use it as you would any other Web service proxy–method parameters and all. As a bonus, the proxy notifies the client that a callback has been received on the UI thread, so there’s no longer any need to marshal off a thread-pool thread as there was in Silverlight 2.

To demonstrate, I updated a Silverlight 2 client I wrote last year to use the new duplex infrastructure in Silverlight 3. Here’s what the client looks like:

It’s a national debt clock. It updates approximately every 5 seconds to show you the current national debt. (Yes, it’s a depressing application.) At startup, it fires off a call to connect to a WCF duplex service named DebtService.svc. Then it updates its display in response to data pushed down to it by the service every few seconds.

You can try the application for yourself by clicking here. Or you can download the source code here. The source code includes the WCF DebtService, which implements a simple contract named IDuplexService and uses a timer as a trigger for callbacks. It also includes the Silverlight client, which connects to the service through a proxy and registers a handler for RefreshReceived events fired when fresh data arrives from the service. The entire client boils down to little more than this:

_proxy = new DuplexServiceClient(binding, address);

_proxy.RefreshReceived +=

    new EventHandler<RefreshReceivedEventArgs>(OnRefreshReceived);

_proxy.ConnectAsync();

void OnRefreshReceived(object sender, RefreshReceivedEventArgs e)

{

    // Update the display

}

In case you wondered, the debt figures displayed by the application are roughly accurate but not exact. My service uses a simple formula to compute the national debt–a formula that’s based on numbers from the Bush administration. I simply don’t have the heart to update the formula to account for Washington’s latest spending spree. Still, the figures should be accurate to within a trillion or two. And what’s a trillion bucks between friends?

Jeff Prosise

View Comments

  • Not sure what I enjoyed more, learning more about the duplex networking (Steve Porter touched on this at the Atlanta Silverlight Firestarter event), or laughing out loud at the commentary about the formulas being used ... thanks for another great post!

  • hi Jeff,
    excellent example. just what i needed.
    can you do a short example like this but for sending data from Silverlight client to the server.
    example,
    a textblock for DateTime.Now.ToTimeLong() and button on Silverlight 3.
    2 computers connected.
    when PC1 press a button, it updates textblock on PC2 and PC1.
    i'm trying to understand how to send data from client and updats all the connected computers.
    the MIX09 chat example is too complicated.
    need something short like yours. :-)
    thanks a bunch,
    Bob

  • Alexey, MaxConcurrentSessions=10 is still the default. I wish the default had been higher, but at least there's an easy work-around. Because WCF duplex services hold connections open for the callback channel (and for pinging), this solution doesn't scale to thousands of clients anyway. I guess it's true there's no such thing as a free lunch!

  • Hi Jeff,
    Nice simple demostration. Thanks. I used duplex in silverlight 2 and found it to work very well. I'm curious as to your opinion on when to implement a duplex solution versus completely client driven polling (without the http parking stuff) so that the server stays stateless?
    Thanks,
    Mike

  • Client-driven polling can be more scalable, for sure, because you don't have to hang on to connections for so long. I'd say if you're talking about

  • I tried your example placing it in my Desktop and all works fine.
    When i move to project folder on a network disk or in one other unit example E: it doesn't work and in the section Bindings of Web Config is reported followinf error :
    Warning 1 The element 'bindings' has invalid child element 'pollingDuplexHttpBinding'. List of possible elements expected: 'basicHttpBinding, customBinding, msmqIntegrationBinding, netPeerTcpBinding, netMsmqBinding, netNamedPipeBinding, netTcpBinding, wsFederationHttpBinding, ws2007FederationHttpBinding, wsHttpBinding, ws2007HttpBinding, wsDualHttpBinding, mexHttpBinding, mexHttpsBinding, mexNamedPipeBinding, mexTcpBinding, webHttpBinding, netTcpContextBinding, wsHttpContextBinding, basicHttpContextBinding'. C:UserscrossiDesktopDuplexDemoDuplexDemo.WebWeb.config 125 5 DuplexDemo.Web
    What cin i do to let your example work ?
    Thanks , Claudio

  • Danke für das Tutorial + Quellcode. Der Quellcode funktioniert super mit VS 2008 + min. Silverlight 3.
    Das Beispiel ist einfach gehalten, konnte es gut nachvollziehen.

Recent Posts

How to Navigate Azure Governance

 Cloud management is difficult to do manually, especially if you work with multiple cloud…

6 days ago

Why Azure’s Scalability is Your Key to Business Growth & Efficiency

Azure’s scalable infrastructure is often cited as one of the primary reasons why it's the…

3 weeks ago

Unlocking the Power of AI in your Software Development Life Cycle (SDLC)

https://www.youtube.com/watch?v=wDzCN0d8SeA Watch our "Unlocking the Power of AI in your Software Development Life Cycle (SDLC)"…

1 month ago

The Role of FinOps in Accelerating Business Innovation

FinOps is a strategic approach to managing cloud costs. It combines financial management best practices…

1 month ago

Azure Kubernetes Security Best Practices

Using Kubernetes with Azure combines the power of Kubernetes container orchestration and the cloud capabilities…

2 months ago

Mastering Compliance: The Definitive Guide to Managed Compliance Services

In the intricate landscape of modern business, compliance is both a cornerstone of operational integrity…

2 months ago