Thursday, December 3, 2009

WCF Duplex HttpBinding [Polling] In Silverlight 3.0

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 

image

Make sure that you select Silverlight version 3.0 and also check to create Sliverlight Host application project as well.

image

After creating the project now right click on Silverlight web application project select add new item and add the Siliverlight-Enabled WCF Service Template.

image

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)

image

Client Side Library (C:\Program Files (x86)\Microsoft SDKs\Silverlight\v3.0\Libraries\Client)

image

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.

[ServiceContract]
  public interface IDuplexClientCallBack
  {
      [OperationContract(IsOneWay = true)]
      void GetServerTime(string serverTime);
  }

image

after completing the service implementation now time to change Web.config 

 

image

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.

image

Now we need to add WCF service reference to Silverlight client 

image

image

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.

 image

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.

Nandri(Thanks)

SreenivasaRagavan.

2 comments:

Priyanka said...

Hi,
I like the way you have illustrated your example.
When I try to run the web service in the browser, I get the following error message. May I know if I may have missed some settings? I am working in visual studio 2010.
I have gone exactly as per your steps.

error--
The type 'serv.Web.Service', provided as the Service attribute value in the ServiceHost directive, or provided in the configuration element system.serviceModel/serviceHostingEnvironment/serviceActivations could not be found.



Please help.
I need a solution urgently.

Thanks.

Priyanka said...

Contd....

Also, if I need to host a service in a website, do I need to follow the same steps?