Overview
WCF land continued to be used many times, but every time is a little, to be enough to solve the problem for the king, these days a bit idle, special search for some information to see, last night, tried to use WCF Duplex mode to implement a simple instant messaging program, through the service-side forwarding to achieve communication between the client. This is just a demo, without considering exception handling and performance issues. The solution structure is as follows:
Contract
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.ServiceModel;
Using System.Text;
Using System.Threading.Tasks;
Namespace Service.interface
{
[ServiceContract (callbackcontract = typeof (Icallback))]
public Interface Inoticeoperator
{
[operationcontract]
void Register (String id);
[OperationContract]
void unregister (String id);
[OperationContract]
void SendMessage (string from, string to, String message);
}
The interface defines three behaviors, respectively:
• Register
• Logoff
• Send a message
In this, the contract Icallback for the service-side callback client method is specified in the attribute [ServiceContract (callbackcontract = typeof (Icallback)) , which is defined as follows:
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.ServiceModel;
Using System.Text;
Using System.Threading.Tasks;
Namespace Service.interface
{public
Interface icallback
{
[OperationContract (IsOneWay = True)]
void Notice (String message);
}
Entity
This demo has only one entity, which is used to indicate the ID of the registered user and an instance of the specific implementation of the corresponding callback contract:
Using Service.interface;
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Using System.Threading.Tasks;
Namespace Models
{public
class Client
{public
String Id {get; set;}
Public Icallback CallBack {get; set;}}}
Implementation code of the contract
Using Models;
Using Service.interface;
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.ServiceModel;
Using System.Text;
Using System.Threading.Tasks; Namespace Service {public class Noticeoperator:inoticeoperator {private static list<client> clientlist = new
List<client> ();
public void Register (string id) {Console.WriteLine ("Register:" + ID);
Icallback callBack = operationcontext.current.getcallbackchannel<icallback> ();
Clientlist.add (New Client () {id = id, CallBack = CallBack});
public void unregister (string id) {Console.WriteLine ("Unregister:" + ID);
Client client = Clientlist.find (c => c.id = Id);
if (client!= null) {clientlist.remove (client); {client-client = Clientlist.find (c => c.id = SendMessage (string from, string to, String) {
= to); if (client!= null) {String longmessage = String.Format (' message from {0} to {1} ' at {2}: {3} '), from, to, DateTime.Now.ToString ("HH:mm:ss"), message);
Console.WriteLine (Longmessage); Client.
Callback.notice (Longmessage);
}
}
}
}
The Register method is used to add a client entity to a list, simulate the registration behavior, and the Clinet entity contains the user information and an instance object that implements the callback contract.
The unregister method is used to remove a client from the list and simulate the logoff behavior.
The SendMessage method is used to send messages, the first parameter is the ID of the sender, the second parameter is the ID of the message receiver, and the third parameter is the sending content, which prints the message first on the server. Then, the notice method of the instance object of the specific implementation class of the callback contract of the message receiver is then invoked to achieve the purpose of sending the message to the client by the server.
Host
Using Service;
Using Service.interface;
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.ServiceModel;
Using System.ServiceModel.Description;
Using System.Text;
Using System.Threading.Tasks;
Namespace Hosting
{
class program
{
static void Main (string[] args)
{
using ( ServiceHost host = new ServiceHost (typeof (Noticeoperator)))
{
host. AddServiceEndpoint (typeof (Inoticeoperator), New NetTcpBinding (), "Net.tcp://127.0.0.1:9527/noticeoperator");
Host. Opened + = (s, e) => Console.WriteLine ("Service is running ...");
Host. Open ();
Console.ReadLine ();}}}
The host is a console application that uses a binding type of nettcpbinding, and the port is the lifetime code of Huaan's Washington.
Client Code
Implementing the Callback Interface
Using Service.interface;
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Using System.Threading.Tasks;
Namespace Test
{
class callback:icallback
{public
void Notice (String message)
{
Console.WriteLine (message);}}
Simulate registration, send messages and logout
using Service.interface; using System; using System.Collections.Generic; using System .
Linq;
Using System.ServiceModel;
Using System.Text;
Using System.Threading.Tasks; Namespace Test {class Program {static void Main (string[] args) {InstanceContext context = new InstanceContext (new
CallBack ()); using (channelfactory<inoticeoperator> factory = new Duplexchannelfactory<inoticeoperator> (context, new NetTcpBinding (), "Net.tcp://127.0.0.1:9527/noticeoperator")) {Inoticeoperator proxy = factory.
CreateChannel ();
String Selfid = args[0];
String friendID = args[1]; Proxy.
Register (SELFID);
Console.WriteLine ("----------Register------------");
while (true) {a String message = Console.ReadLine (); if (message = = "Q") {proxy.
Unregister (SELFID);
Break } else {proxy.
SendMessage (Selfid, friendid, message); }
}
}
}
}
}
Run Test.exe in cmd Joey Ross says Joey is registering to send a message to his friend Ross; a process test.exe Ross Joey says Ross registers to send a message to his friend Joey. After the process is started, enter some characters to send to the other side, enter Q return sign off and exit the program. As shown in the following illustration:
Ross:
Joey:
Service side:
Resources
• Non-nonsense WCF Getting Started tutorial five [WCF communication modes]
• Colleague @ Macfon code
• "WCF Comprehensive analysis"
PostScript
This is just a demo, in the actual project if the number of people online at the same time, the performance of the feasibility of this will need to further the WCF duplex mode of work in-depth study.
Solution Download Address: Wcfdemo
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.