本文测试环境:
wcf宿主在控制台上
silverlight和wcf之间的通信使用tcp协议
定义契约依然使用上一篇的例子,我们需要另外定义一个契约及其实现
服务契约:
[ServiceContract(CallbackContract=typeof(IChatClIEnt))] public interface IChatService { [OperationContract] voID SendMessage(WCFModel.Message message); } [ServiceContract] public interface IChatClIEnt { [OperationContract(IsOneWay=true)] voID GetMessage(WCFModel.Message message); }
数据契约:
[DataContract] public class Message { [DataMember] public string To { get; set; } [DataMember] public string From { get; set; } [DataMember] public string Content { get; set; } }实现契约
这里的实现使用一个集合存储了所有连接客户端的回调句柄及每个客户端的标识,用一个类来存放
public class ClIEntHandler { public string name { set; get; } public IChatClIEnt ClIEnt { set; get; } }
下面是实现的具体代码,基本逻辑为只要有用户发送消息到服务端,遍历整个客户端集合,触发回调句柄将消息发送出去,并删除消息。
public class ChatService:IChatService { static List<WCFModel.Message> listofMessages = new List<WCFModel.Message>(); IChatClIEnt clIEnt; static List<ClIEntHandler> listofClIEnt = new List<ClIEntHandler>(); public voID SendMessage(WCFModel.Message message) { listofMessages.Add(message); clIEnt = OperationContext.Current.GetCallbackChannel<IChatClIEnt>(); if(listofClIEnt.Where(m=>m.name==message.From).Count()==0) { listofClIEnt.Add(new ClIEntHandler() { name=message.From,ClIEnt=clIEnt }); } foreach(var item in listofClIEnt) { if (listofMessages.Where(m => m.To == item.name).Count() > 0) { var msg = listofMessages.Where(m => m.To == item.name).FirstOrDefault(); item.ClIEnt.GetMessage(msg); listofMessages.RemoveAll(m=>m.To==item.name); } } } }服务寄宿
然后将此服务寄宿到控制台
class Program { static voID Main(string[] args) { MyHost.open(); System.Console.Writeline("服务已经启动... 敲回车键停止服务"); System.Console.Readline(); MyHost.Close(); } } public class MyHost { static ServiceHost host = null; public static voID open() { host = new ServiceHost(typeof(WCFlibrary.ChatService)); host.open(); } }配置文件
<service behaviorConfiguration="WCFlibrary.UpdateUserBehavior" name="WCFlibrary.ChatService"> <host> <baseAddresses> <add baseAddress="net.tcp://localhost:4506/ChatService"/> </baseAddresses> </host> <endpoint address="" binding="netTcpBinding" contract="WCFlibrary.IChatService" bindingConfiguration="netTcpBindConfig"></endpoint> <endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange" ></endpoint> </service>客户端访问
silverlight作为客户端先引用服务以便自动生成客户端代理
然后编写代码来访问服务
public partial class MainPage : UserControl { ChatService.ChatServiceClIEnt proxy; public MainPage() { InitializeComponent(); } private voID txtSend_Click(object sender,RoutedEventArgs e) { proxy = new ChatService.ChatServiceClIEnt(); proxy.GetMessageReceived += (obj,arg) => { if (arg.Error == null) { this.txtChatArea.Text += arg.message.Content + Environment.Newline; } }; proxy.SendMessageCompleted += (obj,arg) => { if (arg.Error == null) { this.txtChatArea.Text += "发¢送í成é功|!?" + Environment.Newline; } }; proxy.SendMessageAsync(new ChatService.Message() { From=this.txtFrom.Text,To=this.txtTo.Text,Content=this.txtContent.Text }); } }
整体的运行结果
注意事项
本文最重要的地方,在于客户端请求服务器后,在服务器端保存了客户端的回调句柄,在需要的时候进行回调。
static List<ClIEntHandler> listofClIEnt = new List<ClIEntHandler>();
clIEnt = OperationContext.Current.GetCallbackChannel<IChatClIEnt>(); if(listofClIEnt.Where(m=>m.name==message.From).Count()==0) { listofClIEnt.Add(new ClIEntHandler() { name=message.From,ClIEnt=clIEnt }); }
而在客户端呢,需要绑定这个回调事件
proxy = new ChatService.ChatServiceClIEnt(); proxy.GetMessageReceived += (obj,arg) => { if (arg.Error == null) { this.txtChatArea.Text += arg.message.Content + Environment.Newline; } };总结
以上是内存溢出为你收集整理的WCF双工通信研究之与silverlight借助tcp协议通信全部内容,希望文章能够帮你解决WCF双工通信研究之与silverlight借助tcp协议通信所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)