Calling WCF Duplex service in Silverlight


In a typical Silverlight application, a client web service requests a web service and waits for the response from the service and then processes it. This is distinctly a one way communication, in which a client must initiate every conversation.

The problem with one way communication is that we can't build a chat application with it or implement features such as notifications. Well there is a solution: Duplex Services.

How Duplex Services Works in Silverlight:

Silverlight includes a feature for creating duplex services, which allow two-way communication (meaning the server can contact your client when needed). The client's network request is left open but in an inactive state that doesn't hassle the server. It stays open until it times out, 90 seconds later, at which point the client connects again.

So let's start creating a duplex WCF service at server side and let Silverlight access it at the client side.

Step 1: Create a Silverlight application and name it "DuplexService".

Step 2: Add a WCF Service from DuplexService.Web Hosting project


Step 3: Select WCF Service and name it "AsyncTask.svc"


Step 4:
 We will first create an interface: In order for a client application to have a two-way conversation with a web service, the client needs to know something about the web service, and the web service needs to know something about the client. When calling the service, the client uses the service interface (which, in this example, is named IAsyncTaskService). When calling the client, the service uses the client interface (which is named IAsyncTaskClient)

So open IAsyncTask.cs, and let's create these interfaces
[ServiceContract(CallbackContract = typeof(IAsyncTaskClient))]public interface IAsyncTaskService{
    [OperationContract(IsOneWay = true)]
    void SubmitTask(TaskDescription task);

[ServiceContract]public interface IAsyncTaskClient{    [OperationContract(IsOneWay = true)]
    void ReturnResult(TaskResult result);

The second thing which we need to concentrate upon are as below;
  1. The OperationContract that decorates the SubmitTask() method sets the IsOneWay property to true. This makes it a one-way method. When calling a one-way method, the client will disconnect after the request message is sent, without waiting for a response. This also makes the server-side programming model easier. Rather than starting a new thread or running a timer, the SubmitTask() can carry out its time-consuming work from start to finish, safe in the knowledge that the client isn't waiting.
  2. The ServiceContract attribute that decorates the interface declaration. It sets the CallbackContract property to indicate the interface that the client will use. The client interface also consists of a single one-way method. This method is named ReturnResult(), and the server calls it to pass back the result to the client when the operation is complete.
Step 5: Now let's create the respective classes.
[DataContract()]public class TaskDescription{
    public string DataToProcess{ getset; }
[DataContract()]public class TaskResult{
    public string ProcessedData { getset; }
The TaskDescription class encapsulates the information in the task request that the client sends to the server

The TaskResult class encapsulates the final, processed data that the server returns to the client.

Step 6: Now it's time to create the service:
public class AsyncTask : IAsyncTaskService{    #region IAsyncTaskService Members

    public void SubmitTask(TaskDescription taskDescription)