Silverlight 4.0: Duplex Communication over Http using PollingDuplexHttpBinding

Posted by: Mahesh Sabnis , on 6/25/2011, in Category Silverlight 2, 3, 4 and 5
Views: 31371
Abstract: In a real world application, many-a-times it is required that the application performs a two-way communication i.e. the client application sends request to the service and then service should have capability to initiate action itself and send the data back to the client. In Silverlight, we can perform this kind of operation using Socket programming as well as using Duplex bindings. In Silverlight 4.0, we can do this using net.tcp binding. In this small article, I have used Polling Duplex communication

In a real world application, many-a-times it is required that the application performs a two-way communication i.e. the client application sends request to the service and then service should have capability to initiate action itself and send the data back to the client.

In Silverlight, we can perform this kind of operation using Socket programming as well as using Duplex bindings. In Silverlight 4.0, we can do this using net.tcp binding. In this small article, I have used Polling Duplex communication. In WCF service, duplex communication is implemented using PollingDuplexBinding. When you install Silverlight, you get the following path where the polling duplex assembly is stored:

C:\Program Files (x86)\Microsoft SDKs\Silverlight\v4.0\Libraries\Server\System.ServiceModel.PollingDuplex.dll

The above assembly reference is used by the WCF Service for the Duplex communication mechanism and the assembly used by the Silverlight client application is available at:

C:\Program Files (x86)\Microsoft SDKs\Silverlight\v4.0\Libraries\Client\System.ServiceModel.PollingDuplex.dll

Since the communication between Silverlight 4 and WCF is performed using Http, the Silverlight client has to send the request to the service to initiate the communication. Once this connection is established, the PollingDuplexBinding allows the established channel to keep alive, until the timeout occurs.

Let’s see a practical implementation in this article

Step 1: Open VS2010 and create a new Silverlight application, name it as ‘SL4_Duplex_Service’. You will now see the ‘SL4_Duplex_Service.Web’ host project.

Step 2: In the Web host project, add a reference to the ‘System.ServiceModel.PollingDuplex.dll’ from the following path:

C:\Program Files (x86)\Microsoft SDKs\Silverlight\v4.0\Libraries\Server\System.ServiceModel.PollingDuplex.dll

Step 3: In the Web host project, add a new Silverlight enabled WCF service and call it ‘MyService.svc’. Also in the project, add a new Interface file and name it as ‘IService.cs’, the same file will define the ServiceContract and OperationContract as shown below:

silverlight-duplex-service

The above code has the ServiceContract and the CallbackContract defined.

Step 4: In the MyService.svc.cs, implement the IService interface - this code implements ‘GetData’ operation contract. The implementation defines an instance of the ‘System.Threading.Timer’ class to define the interval when the WCF service makes callback to the Silverlight client. The code is as shown below:

silverlight iservice implementation

In the above code, the getTimer object executes the GetDepartmentDataTimer() method after every 4 seconds (4000 ms) and using CallBack, sends result back to the client.

Task 5: Open Web.Config file and write the following configuration:

config duplex binding

The above configuration shows that the WCF service makes use of ‘PollingDuplexHttpBinding’, the assembly used for this binding is registered using bindingextensions.

Build the Web Host project and make sure that it is error free.

Step 6: In the Silverlight project ‘SL4_Duplex_Service’, add the reference of ‘System.ServiceModel.PollingDuplex.dll’ from the following path:

C:\Program Files (x86)\Microsoft SDKs\Silverlight\v4.0\Libraries\Client\System.ServiceModel.PollingDuplex.dll

Step 7: Add the service reference of the WCF service in the Silverlight project to create a proxy. However here you will not find any config contents generated in ‘ServiceReferences.ClientConfig’, so here we need to code the proxy manually.

Step 8: In the MainPage.Xaml write some Xaml code: (drag-drop the DataGrid):

silverlight-main-xaml

Task 9: In the MainPage.xaml.cs, write the following code:

silverlight service proxy

The above code defines the Endpoint for the WCF service using code. The CustomBinding defines the binding from the Silverlight client to make call to the WCF service, it also defines the binary encoded messages. Using the Proxy, the GetDataAsync asynchronous method is called, the code also subscribes to the SendResultCompleted event which is responsible to get the action completed by the WCF service and makes data available back to the client application.

Important: Make sure the interval defined for the WCF service for sending data back to the Silverlight,is specified as per your business requirements. If it is too less e.g. just 1 second, the service will be always down. It should also not be too much e.g. 20 mins then the polling effect will not be experienced.

Step 10: Run the application, click on the GetData Button and after sometime, department will be displayed and the DataGrid will show the new Department record after every 4 seconds.

Conclusion: Using this mechanism, we can implement simple Push-Back kind of effects in our Silverlight application.  Applications handling Stock Market data can effectively makes use of this polling functionality to automatically get the status of the stock.

The entire source code of the article can be downloaded over here

Give me a +1 if you think it was a good article. Thanks!
Recommended Articles


Page copy protected against web site content infringement by Copyscape


User Feedback
Comment posted by Yun on Tuesday, November 29, 2011 1:03 AM
Hi, this post really help me a lot, thanks!!!
Comment posted by Mike on Monday, January 16, 2012 6:42 AM
Hi, thank you for this post. Is it possible to configure this scenario for communication over HTTPS, and how I could implement it?
Thank you.

Post your comment
Name:  
E-mail: (Will not be displayed)
Comment:
Insert Cancel