In Silverlight HTTP polling duplex binding protocol has been designed to enable data push from the WCF server to the client. Now In this blog post i am going to show you how we can use WCF Duplex Http Binding to push data from server to client. I am using VS.NET 2010 Beta 2. you can also use VS.NET 2008 SP1 with Siliverlight 3.0 SDK Installed
1) First Create Silver application project
Make sure that you select Silverlight version 3.0 and also check to create Sliverlight Host application project as well.
After creating the project now right click on Silverlight web application project select add new item and add the Siliverlight-Enabled WCF Service Template.
After that we need to add two references one on server project where the WCF services added other one in Silverlight client project.
Server Side Library ( Located in C:\Program Files (x86)\Microsoft SDKs\Silverlight\v3.0\Libraries\Server)
Client Side Library (C:\Program Files (x86)\Microsoft SDKs\Silverlight\v3.0\Libraries\Client)
Now we need to add Client Callback contract Interface as shown below in WCF service side and make sure that the operation contract decorated with IsOneWay=true.
public interface IDuplexClientCallBack
[OperationContract(IsOneWay = true)]
void GetServerTime(string serverTime);
after completing the service implementation now time to change Web.config
Save the Web.config file now we are ready to browse the WCF Service in a Browser . The following screen shows that we are successfully created the WCF Duplex Service.
Now we need to add WCF service reference to Silverlight client
In Silverlight all service calls are made as Asynchronous calls. if you look at the above client side code we have event handler for GetCurrentServerTimeAsync() method.
Now run the client you will see there is callback to Silverlight client from WCF service which will update the time every 5 seconds.
Using WCF Polling Duplex Http binding i have created small apps which will updates Cricket Score every 1 min from 3rd party website. this URL of the game is configurable like that you can even use it for stock quote update.