• 贾君鹏你妈妈喊你回家吃饭利用WCF的Duplex推送消息


    有点标题党了,最近因工作需要,想利用WCF的Duplex服务向Winform程序推送消息,写了个示例,主要参考了artech的相关文章和其他一些利用WCF向SilverLight客户端推送消息的文章。
    先看运行效果:在网页中发送消息【如图】,Winform端接收到消息


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


    SendMessage.aspx 是发送消息的Web页面
    ISendMessageService.cs 和 SendMessageService.svc用来实现WCF的Duplex服务
    GetMessageForm.cs 是接收消息的Winform窗口
    当然,还需要建立一个消息实体文件:MessageEntity.cs,为简单起见,只给他定义一个属性。

     [DataContract]
        
    public class MessageEntity
        {
            [DataMember]
            
    public string Content { getset; }
        }

    基本原理是消息发送的页面将要发送的消息列表保存在全局缓存中,在WCF服务中取得要发送的消息推送

    到Winform端,SendMessage.aspx的代码如下:

    Code

    ISendMessageService.cs 用来定义消息接收接口和回调接口

    Code

    SendMessageService.svc.cs 用来实现将缓存中的消息列表一个一个的推送出去,采用Timer类每2-5秒钟推送一次:

    Code

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

    Code

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

    Code

     源代码打包下载

  • 相关阅读:
    EXT FileUploadField 文件上传失败
    CRM自定义页面
    The report server was unable to validate the integrity of encrypted data in the database. (rsCannotValidateEncryptedData) .
    sql存储过程与临时表
    MERGE INTO
    [LeetCode]Merge Two Sorted Lists
    [LeetCode]Search for a Range
    [LeetCode]Length of Last Word
    [LeetCode]Implement strStr()
    [LeetCode]Pascal's Triangle
  • 原文地址:https://www.cnblogs.com/xiaozhuang/p/1528712.html
Copyright © 2020-2023  润新知