Communication between Silverlight and WCF (2) "Push" to SL data using WCF Duplex communication

Source: Internet
Author: User
Tags bool silverlight

One, Duplex introduction

The last essay records the SL use timer to take the data from WCF to bind to the interface problem, today tried to use WCF duplex duplex communication To do this thing, but also in this example to illustrate the use of WCF duplex.

The principle of duplex communication is very simple, we usually use the client to call the server side of the method to obtain data, and duplex is the client also as the servers, the method on the client can also be invoked, to chat function as an example, user a connected to the server, before the practice is the client timing data, While duplex detects data changes at the service end, if a message is sent to a, the client's method is invoked immediately to push the information to a.

Second, establish the WCF service of duplex mode

With a simple chat feature, WCF provides three ways to connect to a server method, send an information method, and receive an information method. From the service contract is divided into two interfaces, respectively, for the client to send information and start the chat method of Ichatservice interface and server invoke client method Ichatservicecallback interface

IChatService.cs file

namespace ChatWCF
{
      [ServiceContract(CallbackContract=typeof(IChatServiceCallBack))]//这里需要定义 IChatService接口的回调接口IChatServiceCallBack 
     public interface IChatService
     {
         [OperationContract]
         bool SendMessage(MessageInfo msg); //发送信息
         [OperationContract]
         bool LoginChat(string User,string Partner);//开始聊天模式
     }
     [ServiceContract]
     public interface IChatServiceCallBack //供服务端回调的接口
     {
         [OperationContract(IsOneWay=true)]
         void ReceiveMessages(List<MessageInfo> listMessages);//客户端被服 务端回调后接收信息
     }
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.