利用message queue实现aspx与winform通信, 并附完整示例

先在本机(本文中的计算机名为jimmyibm)安装message queue(消息队列),别告诉我你不会安装.(就跟安装IIS差不多的)

安装好后,打开 "计算机管理-->服务和应用程序-->消息队列-->专用队列",新一个专用队列,命名为msmqdemo

建好后,在msmqdemo上右击,查看属性,可以看到"jimmyibm\private$\msmqdemo"这就是该队列的完整标识

好了,开始写代码

建一个解决方案,在解决方案中加一个winform的项目,添加System.Messgeing的引用

winform的主要代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Messaging;


namespace QueueStudy
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnSend_Click(object sender, EventArgs e)
        {
            if (txtSend.Text.Trim() == "")
            {
                MessageBox.Show("请输入要发送的message!");
                txtSend.Focus();
                return;
            }
            //完整队列格式为:  计算机名\private$\队列名称 (专用队列)
            MessageQueue mq = new MessageQueue("jimmyibm\\private$\\MSMQDemo");
            System.Messaging.Message msg = new System.Messaging.Message();
            msg.Body = txtSend.Text.Trim();
            msg.Formatter = new System.Messaging.XmlMessageFormatter(new Type[] { typeof(string) });
            mq.Send(msg);

           
        }

        private void btnReceive_Click(object sender, EventArgs e)
        {          
            MessageQueue mq = new System.Messaging.MessageQueue("jimmyibm\\private$\\MSMQDemo");
            //同步接收,直到得到一条消息为止,如果消息队列为空,会一直阻塞
            System.Messaging.Message msg = mq.Receive();
            msg.Formatter = new System.Messaging.XmlMessageFormatter(new Type[] { typeof(string) });
            this.txtReceive.Text = msg.Body.ToString();

        }
    }
}


测试一下,在Send Message文本框中随便一个Hello,点击Send Message按钮,再点击下面的Receive Message按钮,正常的话,下面的文本框会显示出Hello


然后在该解决方案中,再加一个web项目

default.aspx前端代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="webDemo._Default" %>
<%@ Register Assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
    Namespace="System.Web.UI" TagPrefix="asp" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>无标题页</title>
</head>
<body>
    <form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
  
        Send Message:<asp:TextBox ID="txtSend" runat="server"></asp:TextBox>
        <asp:Button ID="btnSend" runat="server" OnClick="btnSend_Click" Text="btnSend" OnClientClick="return CheckData()" />
       
        <script type="text/javascript">
            function CheckData(){
                var oSend = $get("<%=txtSend.ClientID%>");
                if (oSend.value==""){
                    alert("请输入要发送的Message");
                    oSend.focus();
                    oSend.style.backgroundColor="#ffff99";
                    return false;
                }
            }
        </script>
    </form>
</body>
</html>

default.aspx.cs后端代码:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Messaging;

namespace webDemo
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnSend_Click(object sender, EventArgs e)
        {
            //完整队列格式为:  计算机名\private$\队列名称 (专用队列)
            MessageQueue mq = new MessageQueue("jimmyibm\\private$\\MSMQDemo");
            System.Messaging.Message msg = new System.Messaging.Message();
            msg.Body = txtSend.Text.Trim();
            msg.Formatter = new System.Messaging.XmlMessageFormatter(new Type[] { typeof(string) });
            mq.Send(msg);
        }
    }
}

几乎跟winform完全一样,运行这个页面,在文本框里打几个字,点击Send按钮,这时消息已经发送到消息队列里了,在计算机管理的消息队列中也可查看得到

关闭页面,重新运行刚的winform程序,直接点击Receive Button,正常的话,就可以接收到刚才在aspx页面里发送的消息

点击下载此文件

posted @ 2007-11-28 12:31  菩提树下的杨过  阅读(2029)  评论(5编辑  收藏  举报