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);//客户端被服 务端回调后接收信息
}
}