WCF双工,实现简单的聊天程序,开他娘的源!!(依稀记得LinqToAccess开他娘的源)
wcf DLL的接口
using System;using System.Collections.Generic;using System.Linq;using System.Runtime.Serialization;using System.ServiceModel;using System.Text;namespace CoreDll{ // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together. [ServiceContract(SessionMode = SessionMode.Required, CallbackContract = typeof(IQQSback))] public interface IQQS { [OperationContract(IsOneWay = true, IsInitiating = true, IsTerminating = false)] void Speak(string value); [OperationContract(IsOneWay = true, IsInitiating = true, IsTerminating = false)] void Whisper(string name, string value); [OperationContract(IsOneWay = true, IsInitiating = true, IsTerminating = false)] void Leave(); [OperationContract(IsOneWay = false, IsInitiating = true, IsTerminating = false)] List<string> join(string value); } interface IQQSback { [OperationContract(IsOneWay = true)] void Receive(string senderName, string message); [OperationContract(IsOneWay = true)] void ReceiveWhisper(string senderName, string message); [OperationContract(IsOneWay = true)] void UserEnter(List<string> name); [OperationContract(IsOneWay = true)] void UserLeave(string name); }}
using System;using System.Collections.Generic;using System.Linq;using System.Runtime.Serialization;using System.ServiceModel;using System.Text;namespace CoreDll{ [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession, ConcurrencyMode = ConcurrencyMode.Multiple)] public class Service1 : IQQS { IQQSback QQback = OperationContext.Current.GetCallbackChannel<IQQSback>(); delegate void userenter(List<string> username); static event userenter UserEnterEvent; delegate void userspeak(string name, string content); static event userspeak UserSpeakEvent; delegate void userWhisper(string name, string content); static SortedList<string, userWhisper> Userspeaklist = new SortedList<string, userWhisper>(); string Name; public List<string> join(string value) { if (Userspeaklist.Keys.Contains(value)) { return new string[] { "已经有此用户" }.ToList(); } this.Name = value; UserSpeakEvent += new userspeak(UserSpeakEventFunction); if (UserEnterEvent != null) { UserEnterEvent(new string[] { value }.ToList()); } UserEnterEvent += new userenter(UserEnterEventFunction); Userspeaklist.Add(value, UserWhisperFunction); return Userspeaklist.Keys.ToList(); } public void Speak(string value) { if (UserSpeakEvent != null) { UserSpeakEvent(this.Name, value); } } public void Whisper(string name, string value) { if (!Userspeaklist.Keys.Contains(name)) { return; } Userspeaklist[name](name,value); } public void Leave() { UserEnterEvent -= UserEnterEventFunction; UserSpeakEvent -= UserSpeakEventFunction; Userspeaklist.Remove(this.Name); //退出的地方没实现,但是方法跟添加,说话一样。 QQback.UserLeave(this.Name); } void UserEnterEventFunction(List<string> username) { this.QQback.UserEnter(username); } void UserSpeakEventFunction(string name, string content) { QQback.Receive(name, content); } void UserWhisperFunction(string name, string content) { QQback.ReceiveWhisper(name, content); } }}
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.ServiceModel;namespace WCFsuzhu{ class Program { static void Main(string[] args) { Console.WriteLine("正在启动服务"); ServiceHost sh = new ServiceHost(typeof(CoreDll.Service1)); sh.Open(); Console.WriteLine("启动服务成功!回车键退出。"); Console.ReadLine(); sh.Abort(); sh.Close(); } }}