Introduction
In a typical Silverlight application, when we use any web service, the client makes a request for web service and waits for the response from the service and then processes it. This is a one way communication, in which the client must initiate every conversation.
The problem with one way communication is that we can't build chat application or implement features like notifications. Well we have a solution: Duplex Services.
How Duplex Services Work in Silverlight
Silverlight includes a feature for creating duplex services, which allows for 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 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"
Step2: Come to Add WCF Service from DuplexService.Web Hosting project

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

Step 3: We will first create the 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.
Two things which we need to concentrate upon are the following:
- The
OperationContract that decorates the SubmitTask() method sets the IsOneWay property to true. This makes it an 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, SubmitTask() can carry out its time-consuming work from start to finish, safe in the knowledge that the client isn't waiting.
- The
ServiceContract attribute that decorates the interface declaration 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 4: Now let's create the respective classes.
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 5: Now it's time to create the service:
The service implements the IAsyncTaskService, and provides the code for the SubmitTask() method. It isn't decorated with the ServiceContract attribute, because that attribute is already present on the interface.
The actual code in the SubmitTask() method carries out the operation and prepares the return value. The difference is that the return value is passed by explicitly calling the IAsyncTaskClient.ReturnResult() method.
Full implementation looks like below;
Step 6: It's time for configuring Web.Config.
Before going any further, we need to add an assembly reference to the System.ServiceModel.PollingDuplex.dll assembly that has the duplexing support we need. You can find it in a folder named C:\Program Files\Microsoft SDKs\Silverlight\v3.0\Libraries\Server.

Click on browse and trace this path
C:\Program Files\Microsoft SDKs\Silverlight\v3.0\Libraries\Server.

Once you've taken care of that, you're ready to make the first modification to the web.config file. Find the <system.serviceModel> element, and add this inside it.

Now our service is ready to be expose via these endpoints,
Step 7: Run without debugging by clicking Ctrl + F5.
Step 8: Come back to DuplexService project and click to refrence to add Service Reference.

Stpe 9: Add Service in this client project and name it "Service".

Step 10: Now we need a reference to the System.ServiceModel.PollingDuplex.dll assembly. Here we can't use the server-side version. Instead, we can find the Silverlight version in the folder C:\Program Files\Microsoft SDKs\Silverlight\v3.0\Libraries\Client.
Step 11: Now, when creating the proxy object, we need to explicitly create the duplex binding.
While consuming an ordinary web service, we attach an event handler to the completed event. Using a duplex service is similar, but we get one event for each method in the client interface, and the word Received is added to the end instead of Completed. In the current example, the IAsyncTaskClient interface defines a single method named ReturnResult(), and so the proxy class includes an event named ReturnResultReceived().
The interface looks like this:

Step 12: Here's the code for submit:
Step 13: Time to test, so start by clicking F5 and type any string. Yyou will get reverse of that string as per service side written business logic.

Benefits of Duplex Services
- The client doesn't wait for the server's response, but polls for it periodically.
- The server can call different client methods from the same web service method. IN fact, the service can call any method that's defined in the client interface.
About Vishal Nayan
 |
Vishal is a seasoned professional with hand on experience on Microsoft technologies.
He always look for challenging assignment that allows him to learn newer technologies while utilizing his experience of project development and software engineering ethics.
In spare time vishal can be found read...
This author has published 4 articles on DotNetSlackers. View other articles or the complete profile here.
|
You might also be interested in the following related blog posts
Exporting SWF & FLV format reports in SSRS 2005 and 2008
read more
HanselMinutes Interview on RIA Services
read more
Take Two: A jQuery WCF/ASMX ServiceProxy Client
read more
Part 3: Accessing Security and Authentication in Silverlight using .NET RIA Services.
read more
Business Apps Example for Silverlight 3 RTM and .NET RIA Services July Update: Part 18: Custom Linq Provider
read more
Remove the business context from your services
read more
CI and Configurable Service Installers
read more
Business Apps Example for Silverlight 3 RTM and .NET RIA Services July Update: Part 8: WCF Based Data Source
read more
Silverlight 3 and WCF Faults
read more
Use the "Oslo" Repository to Store Service Config
read more
|
|
Please login to rate or to leave a comment.