先看运行效果:在网页中发送消息【如图】,利用WCF的Duplex服务向Winform程序推送消息,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 { get; set; }
- }
基本原理是消息发送的页面将要发送的消息列表保存在全局缓存中,在WCF的Duplex服务中取得要发送的消息推送到Winform端,SendMessage.aspx的代码如下:
- protected void btnSend_Click(object sender, EventArgs e)
- {
- MessageEntity message = new MessageEntity();
- message.Content = txtMessageContent.Text;
- List<MessageEntity> messageList = HttpRuntime.Cache["MessageEntityList"] as List<MessageEntity>;
- if (messageList == null)
- {
- messageList = new List<MessageEntity>();
- messageList.Add(message);
- HttpRuntime.Cache.Add("MessageEntityList", messageList, null, System.Web.Caching.Cache.NoAbsoluteExpiration, System.Web.Caching.Cache.NoSlidingExpiration, System.Web.Caching.CacheItemPriority.Default, null);
- }
- else
- {
- messageList.Add(message);
- HttpRuntime.Cache["MessageEntityList"] = messageList;
- }
- lbCacheCount.Text = messageList.Count.ToString();
- }
- ISendMessageService.cs 用来定义消息接收接口和回调接口
- Code
- [ServiceContract(CallbackContract = typeof(ISendMessageServiceCallBack))]
- public interface ISendMessageService
- {
- [OperationContract(IsOneWay = true)]
- void GetMessage();
- }
- public interface ISendMessageServiceCallBack
- {
- [OperationContract(IsOneWay = true)]
- void ReceiveMessage(MessageEntity messageEntity);
- }
- SendMessageService.svc.cs 用来实现将缓存中的消息列表一个一个的推送出去,采用Timer类每2-5秒钟推送一次:
- Code
- [ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple)]
- public class SendMessageService : ISendMessageService
- {
- ISendMessageServiceCallBack callback;
- Timer heartTimer;
- Random random = new Random();
- #region ISendMessageService 成员
- public void GetMessage()
- {
- callback = OperationContext.Current.GetCallbackChannel<ISendMessageServiceCallBack>();
- heartTimer = new Timer(new TimerCallback(heartTimer_Elapsed), null, 3000, Timeout.Infinite);
- }
- #endregion
- private void heartTimer_Elapsed(object data)
- {
- List<MessageEntity> messageList = HttpRuntime.Cache["MessageEntityList"] as List<MessageEntity>;
- if (messageList != null && messageList.Count > 0)
- {
- MessageEntity message = messageList[0];
- messageList.Remove(message);
- HttpRuntime.Cache["MessageEntityList"] = messageList;
- callback.ReceiveMessage(message);
- }
- int interval = random.Next(2000, 5000);
- heartTimer.Change(interval, Timeout.Infinite);
- }
- }
记得修改Web.Config中EndPoint Binding 为wsDualHttpBinding ,这样才支持WCF的Duplex服务
- <service behaviorConfiguration="WebApp.SendMessageServiceBehavior" name="WebApp.SendMessageService">
- <endpoint address="" binding="wsDualHttpBinding" contract="WebApp.ISendMessageService">
- <identity>
- <dns value="localhost"/>
- </identity>
- </endpoint>
- <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
- </service>
WebApp项目的工作已经完成,若没有错误可在浏览器中看到该WCF的Duplex服务已创建,拷贝该服务的地址,例如在我本机上是:http://localhost:1407/SendMessageService.svc,并在WinApp项目中增加该服务的引用
GetMessageForm.cs 实现如下:
- public partial class GetMessageForm : Form,ISendMessageServiceCallback
- {
- SendMessageServiceClient client;
- public GetMessageForm()
- {
- InitializeComponent();
- client = new SendMessageServiceClient(new System.ServiceModel.InstanceContext(this));
- }
- private void btnStartGet_Click(object sender, EventArgs e)
- {
- client.GetMessage();
- }
- private delegate void UpdateListBoxDelegate(string Message);
- private void UpdateListBox(string message)
- {
- this.listbMessage.Items.Add(message);
- listbMessage.SelectedIndex = listbMessage.Items.Count - 1;
- }
- #region ISendMessageServiceCallback 成员
- public void ReceiveMessage(MessageEntity messageEntity)
- {
- if (true == listbMessage.InvokeRequired)
- {
- listbMessage.Invoke(new UpdateListBoxDelegate(UpdateListBox), messageEntity.Content);
- }
- else
- {
- UpdateListBox(messageEntity.Content);
- }
- }
- #endregion
- }
【编辑推荐】