The idea of duplexing is very old, when I first read it, it reminded me about my Mom. Well actually not reminded me, but she never allowed me to even forget her, can you imagine 50 missed calls in a day!!! Annoyed . Okay, imagine you being a client and your Mom being the server. It's usually the client that starts communicating with server, requests for data, for authentication or for money, in this case. The server can either respond positively or negatively to the client's request. That is if your request is for money, then you either get it or don't, the later one usually happens in my case. Now what if the server initiates the communication? Yes! It's not always that the server listens to the client's request, but there are situations when the server also broadcasts the messages to all it's clients. This process is called Duplex polling. In Silverlight however, there are many ways to perform this server to client broadcasting, one way is through WCF Services that we are going to use now. So grab a cup of Coffee or two, because the journey is little bit lengthy and a bit complicated.
Before proceeding let's establish the strategy of what exactly we are going to do. What I planned is to keep it neat, clean and simple. So to start, let's create a Silverlight application first, naming it as MyPolling.

Now before going deep into WCF let's first set up our UI. It's nothing though, but a simple button and a text block. Here's how I arranged them in my XAML:
1: <UserControl x:Class="MyPolling.MainPage"
2: xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3: xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4: xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
5: xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
6: mc:Ignorable="d"
7: d:DesignHeight="300" d:DesignWidth="400">
8:
9: <Grid x:Name="LayoutRoot" Background="White">
10: <Button x:Name="Start" Content="Send Message"
11: VerticalAlignment="Center"
12: HorizontalAlignment="Center"
13: Click="Start_OnClick"/>
14: <TextBlock x:Name="TxtBlkInfo" Text="Press Button to start communication"
15: TextWrapping="Wrap" Width="200" Margin="0,0,200,251" />
16: </Grid>
17: </UserControl>
Looks cool isn't it? Okay, it's time to add some WCF stuff. On the web project "Add" -> "New item" -> "WCF Service" and name it "MyPollingService.svc". After clicking "Add" Visual Studio should have opened the "IMyPollingService" interface for you.

Delete the DoWork() function that is already in the interface. Now replace the interface with exactly the following lines of code:
1: public interface IMyPollingService
2: {
3: [OperationContract(IsOneWay = true)]
4: void SubmitResult(string rS);
5: }
It's time to make another interface, the client one, that will actually do the heavy lifting of taking the data from the server to the client. So, below the IMyPollingService interface, add the following lines:
1: [ServiceContract(CallbackContract = typeof(IMyPollingClient))]
2: public interface IMyPollingService
3: {
4: [OperationContract(IsOneWay = true)]
5: void SubmitResult(string rS);
6: }
Before wrapping up with the interface part, we need to make the service know about the existence of the client interface. In order to do so, just above the IMyPollingService interface add the following (line #1):
1: [ServiceContract(CallbackContract = typeof(IMyPollingClient))]
2: public interface IMyPollingService
3: {
4: [OperationContract(IsOneWay = true)]
5: void SubmitResult(string rS);
6: }
The interface part is ready, what we've done so far is we've structured our service for how our service is to look like, what will it contain etc. In a nutshell, we have created the overall contract of our service. Next is to grab another cup of Coffee.
So far, so good. It's time to open MyPollingService.svc.cs file. As you will see, there would be a class MyPollingService implementing the interface IMyPollingService that we just have modified. Therefore, it might have errors. So click on the implemented interface and implement the non-implemented methods. After that, if you have the SubmitResult method left in the class, then you are with me. Now we are going to send numbers from 1…100 to the client from the server. In order to do that we will use my love of loops, here's the code:
1: public void SubmitResult(string rS)
2: {
3: var client = OperationContext.Current.GetCallbackChannel<IMyPollingClient>();
4: for (var i = 0; i < 100; i++)
5: {
6: client.ReturnResult(i.ToString(CultureInfo.InvariantCulture));
7: Thread.Sleep(TimeSpan.FromSeconds(2));
8: }
9: }
Let's pause the coding for a few minutes and edit the configuration file, web.config. First we need to add the DLL System.ServiceModel.PollingDuplex.dll at the web side, to do this just right-click on refrences and browse to the following location: "C:\Program Files\Microsoft SDKs\Silverlight\v3.0\Libraries\Server" and add only the DLL present there. Similarly on the client side add the same DLL but from "C:\Program Files\Microsoft SDKs\Silverlight\v3.0\Libraries\Client". We are then ready for web.config's modifications.
Modifying Web.config is very simple. Let's take small steps to configure the file. The original file that we have presented with is:
1: <?xml version="1.0"?>
2:
3: <!--
4: For more information on how to configure your ASP.NET application, please visit
5: http://go.microsoft.com/fwlink/?LinkId=169433
6: -->
7:
8: <configuration>
9: <system.web>
10: <compilation debug="true" targetFramework="4.0" />
11: </system.web>
12:
13: <system.serviceModel>
14: <behaviors>
15: <serviceBehaviors>
16: <behavior name="">
17: <serviceMetadata httpGetEnabled="true" />
18: <serviceDebug includeExceptionDetailInFaults="false" />
19: </behavior>
20: </serviceBehaviors>
21: </behaviors>
22: <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
23: </system.serviceModel>
24: </configuration>
Step I
Naming behavior of the service
First we need to name the behavior of the service (line #16); let's set it to "BehaviourOfService":
1: <serviceBehaviors>
2: <behavior name="BehaviourOfService">
3: <serviceMetadata httpGetEnabled="true" />
4: <serviceDebug includeExceptionDetailInFaults="false" />
5: </behavior>
6: </serviceBehaviors>


Dinesh BeniwalPosted Apr 6, 2013, 11:37 PM
Good Work, Welcome to the C# Corner Neelesh