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:

DuplexDemo 

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?