One of the new feature introduced in WCF for Silverlight 4.0 is an improvement of PollingDuplex binding. You must be aware of commonly used request-reply pattern, where client (Silverlight) initiate the request and response from WCF service is delivered on the same HTTP connection. The duplex binding allows either WCF service or Silverlight 4.0 client to initiate message exchange. The duplex messaging is useful when server needs to notify the client. In older version of Silverlight when client communicates to server, the client directly send request to server and server send response to client in the same connection. When server needs to be enabled to communicate with client, the client has to poll the server and server uses same poll response to send message to clients. In this model the server will send single message on every poll response, and server will wait for new poll to return every message. The Silverlight 4 added a new mode MultipleMessagesPerPoll in existing PollingDuplex binding to address this issue. The new polling mode is using HTTP response streaming to send multiple messages in a single poll response. The server will return chunked response with as many messages as are pending and then it close the poll response. Silverlight 4.0 offers duplex messaging over HTTP or TCP. In this article I am going to cover HTTP based duplex messaging. This article first talks about how to created Duplex WCF service and next we will see how Silverlight client consume the Duplex service. Following are the steps to create Duplex service and consume it in Silverlight client. Create new Silverlight application. Go to, File > New > Project. From the new Project dialog select "Silverlight Application", specify the project name HTTPDuplexMessaging. Click on "OK" button. Create Duplex Service:
[OperationContract] public void AttachClient() { clients.Add(OperationContext.Current.GetCallbackChannel<IChatClient>()); } The clients will use AttchClient contract to register for chat.
Implement client notification Add PublishMessage contract, this contract will be used to notify registered clients. [OperationContract] public void PublishMessage(string message) { foreach (IChatClient channel in clients) { channel.NotifyClient(message); } }
Add binding extension to use PollingDuplex Add PollingDuplex binding extension in web.config. Here I have added binding extension with the name "duplexHttpBinding". <extensions> <bindingExtensions> <add name="duplexHttpBinding" type="System.ServiceModel.Configuration.PollingDuplexHttpBindingCollectionElement, System.ServiceModel.PollingDuplex, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" /> </bindingExtensions> </extensions> Now use duplexHttpBinding declared above in binding section to make use of this binding. Here the important thing to note is, the duplexMode property is set to MultipleMessagesPerPoll mode. <bindings> <duplexHttpBinding> <binding name="myDuplexHttpBinding" duplexMode="MultipleMessagesPerPoll" maxOutputDelay="00:00:07"/> </duplexHttpBinding
proxy = new ChatService.ChatServiceClient( new PollingDuplexHttpBinding { DuplexMode = PollingDuplexMode.MultipleMessagesPerPoll}, new EndpointAddress("../ChatService.svc"));
proxy.NotifyClientReceived += new EventHandler<ChatService.NotifyClientReceivedEventArgs>(proxy_NotifyClientReceived);
void proxy_NotifyClientReceived(object sender, ChatService.NotifyClientReceivedEventArgs e) { txtReceivedMsg.Text += e.message + "\n"; }
You need to be a premium member to use this feature. To access it, you'll have to upgrade your membership.
Become a sharper developer and jumpstart your career.
$0
$
. 00
monthly
For Basic members:
$20
For Premium members:
$45
For Elite members: