利用WCF的Duplex服务向Winform程序推送消息

开发 开发工具
最近因工作需要,想利用WCF的Duplex服务向Winform程序推送消息,写了个示例,主要参考了artech的相关文章和其他一些利用WCF向SilverLight客户端推送消息的文章。

先看运行效果:在网页中发送消息【如图】,利用WCF的Duplex服务向Winform程序推送消息,Winform端接收到消息,

Winform端接收到消息

在网页中发送消息

先建立两个项目,一个WebForm 项目和一个WinForm项目,并在项目下建立好各自需要的文件

WCF的Duplex服务

SendMessage.aspx 是发送消息的Web页面

ISendMessageService.cs 和 SendMessageService.svc用来实现WCF的Duplex服务

GetMessageForm.cs 是接收消息的Winform窗口

当然,还需要建立一个消息实体文件:MessageEntity.cs,为简单起见,只给他定义一个属性。

  1. [DataContract]  
  2.     public class MessageEntity  
  3.     {  
  4.         [DataMember]  
  5.         public string Content { get; set; }  
  6.     } 

基本原理是消息发送的页面将要发送的消息列表保存在全局缓存中,在WCF的Duplex服务中取得要发送的消息推送到Winform端,SendMessage.aspx的代码如下:

  1.  protected void btnSend_Click(object sender, EventArgs e)  
  2.         {  
  3.             MessageEntity message = new MessageEntity();  
  4.             message.Content = txtMessageContent.Text;  
  5.  
  6.             List<MessageEntity> messageList = HttpRuntime.Cache["MessageEntityList"] as List<MessageEntity>;  
  7.             if (messageList == null)  
  8.             {  
  9.                 messageList = new List<MessageEntity>();  
  10.                 messageList.Add(message);  
  11.                 HttpRuntime.Cache.Add("MessageEntityList", messageList, null, System.Web.Caching.Cache.NoAbsoluteExpiration, System.Web.Caching.Cache.NoSlidingExpiration, System.Web.Caching.CacheItemPriority.Default, null);  
  12.             }  
  13.             else 
  14.             {  
  15.                 messageList.Add(message);  
  16.                 HttpRuntime.Cache["MessageEntityList"] = messageList;  
  17.             }  
  18.  
  19.             lbCacheCount.Text = messageList.Count.ToString();  
  20.         }  
  21. ISendMessageService.cs 用来定义消息接收接口和回调接口  
  22.  
  23.    
  24.  
  25. Code  
  26. [ServiceContract(CallbackContract = typeof(ISendMessageServiceCallBack))]  
  27.     public interface ISendMessageService  
  28.     {  
  29.         [OperationContract(IsOneWay = true)]  
  30.         void GetMessage();  
  31.     }  
  32.  
  33.     public interface ISendMessageServiceCallBack  
  34.     {  
  35.         [OperationContract(IsOneWay = true)]  
  36.         void ReceiveMessage(MessageEntity messageEntity);  
  37.     }  
  38. SendMessageService.svc.cs 用来实现将缓存中的消息列表一个一个的推送出去,采用Timer类每2-5秒钟推送一次:  
  39.  
  40.    
  41.  
  42. Code  
  43.  [ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple)]  
  44.     public class SendMessageService : ISendMessageService  
  45.     {  
  46.         ISendMessageServiceCallBack callback;  
  47.         Timer heartTimer;  
  48.         Random random = new Random();  
  49.  
  50.         #region ISendMessageService 成员  
  51.  
  52.         public void GetMessage()  
  53.         {  
  54.             callback = OperationContext.Current.GetCallbackChannel<ISendMessageServiceCallBack>();  
  55.             heartTimer = new Timer(new TimerCallback(heartTimer_Elapsed), null3000, Timeout.Infinite);  
  56.         }  
  57.  
  58.         #endregion  
  59.  
  60.         private void heartTimer_Elapsed(object data)  
  61.         {  
  62.             List<MessageEntity> messageList = HttpRuntime.Cache["MessageEntityList"] as List<MessageEntity>;  
  63.             if (messageList != null && messageList.Count > 0)  
  64.             {  
  65.                 MessageEntity message = messageList[0];  
  66.                 messageList.Remove(message);  
  67.                 HttpRuntime.Cache["MessageEntityList"] = messageList;  
  68.                 callback.ReceiveMessage(message);  
  69.             }  
  70.             int interval = random.Next(20005000);  
  71.             heartTimer.Change(interval, Timeout.Infinite);  
  72.         }  
  73.     } 

记得修改Web.Config中EndPoint Binding 为wsDualHttpBinding ,这样才支持WCF的Duplex服务

  1. <service behaviorConfiguration="WebApp.SendMessageServiceBehavior" name="WebApp.SendMessageService"> 
  2.                 <endpoint address="" binding="wsDualHttpBinding" contract="WebApp.ISendMessageService"> 
  3.                     <identity> 
  4.                         <dns value="localhost"/> 
  5.                     </identity> 
  6.                 </endpoint> 
  7.                 <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/> 
  8.             </service> 

WebApp项目的工作已经完成,若没有错误可在浏览器中看到该WCF的Duplex服务已创建,拷贝该服务的地址,例如在我本机上是:http://localhost:1407/SendMessageService.svc,并在WinApp项目中增加该服务的引用
GetMessageForm.cs 实现如下:

  1. public partial class GetMessageForm : Form,ISendMessageServiceCallback  
  2.     {  
  3.         SendMessageServiceClient client;  
  4.  
  5.         public GetMessageForm()  
  6.         {  
  7.             InitializeComponent();  
  8.             client = new SendMessageServiceClient(new System.ServiceModel.InstanceContext(this));  
  9.         }  
  10.  
  11.         private void btnStartGet_Click(object sender, EventArgs e)  
  12.         {  
  13.             client.GetMessage();  
  14.         }  
  15.  
  16.         private delegate void UpdateListBoxDelegate(string Message);  
  17.         private void UpdateListBox(string message)  
  18.         {  
  19.             this.listbMessage.Items.Add(message);  
  20.             listbMessage.SelectedIndex = listbMessage.Items.Count - 1;  
  21.         }  
  22.         #region ISendMessageServiceCallback 成员  
  23.  
  24.         public void ReceiveMessage(MessageEntity messageEntity)  
  25.         {  
  26.             if (true == listbMessage.InvokeRequired)  
  27.             {  
  28.                 listbMessage.Invoke(new UpdateListBoxDelegate(UpdateListBox), messageEntity.Content);  
  29.             }  
  30.             else 
  31.             {  
  32.                 UpdateListBox(messageEntity.Content);  
  33.             }  
  34.         }  
  35.  
  36.         #endregion  
  37.  
  38.     } 

【编辑推荐】

  1. 使用ASP.NET AJAX调用WCF服务项模板
  2. 详解自定义托管宿主WCF解决方案开发配置过程
  3. 详解WCF可扩展框架中的行为扩展
  4. WCF中通过Dispose有效实现重用
  5. WCF开发基础
责任编辑:彭凡 来源: cnblogs
相关推荐

2017-09-05 15:30:00

JavascriptSocket.ioNode.js

2010-03-02 09:32:54

WCF服务消息

2023-12-06 19:04:31

多平台消息推送

2009-11-02 12:46:15

Winform

2009-07-27 17:46:42

WCF服务ASP.NET应用程序

2009-11-06 12:49:11

WCF面向服务

2009-12-21 13:37:43

WCF消息交换

2010-02-22 10:01:11

WCF消息处理

2009-12-07 09:23:05

2009-11-09 11:15:06

WCF消息队列

2010-11-25 10:05:22

Visual StudSilverlightWCF

2009-12-08 16:15:13

WCF服务

2009-12-08 16:09:02

WCF消息

2010-02-24 09:18:49

WCF Adapter

2022-12-25 10:47:52

2009-11-09 14:02:31

WCF传输数据

2010-02-22 17:21:02

WCF消息交换

2009-12-21 15:12:40

WCF操作Stream

2010-02-25 09:50:30

WCF路由截获消息

2010-02-25 15:49:05

WCF消息模式
点赞
收藏

51CTO技术栈公众号