惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

P
Proofpoint News Feed
Martin Fowler
Martin Fowler
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
C
Cisco Blogs
Last Week in AI
Last Week in AI
T
The Blog of Author Tim Ferriss
The GitHub Blog
The GitHub Blog
T
Tenable Blog
A
Arctic Wolf
小众软件
小众软件
Google DeepMind News
Google DeepMind News
aimingoo的专栏
aimingoo的专栏
PCI Perspectives
PCI Perspectives
博客园 - 司徒正美
The Last Watchdog
The Last Watchdog
H
Hacker News: Front Page
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Stack Overflow Blog
Stack Overflow Blog
N
News and Events Feed by Topic
Security Archives - TechRepublic
Security Archives - TechRepublic
博客园 - 【当耐特】
S
Security @ Cisco Blogs
P
Proofpoint News Feed
Cloudbric
Cloudbric
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Jina AI
Jina AI
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
月光博客
月光博客
Schneier on Security
Schneier on Security
Hacker News: Ask HN
Hacker News: Ask HN
V
Visual Studio Blog
D
DataBreaches.Net
H
Help Net Security
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Project Zero
Project Zero
阮一峰的网络日志
阮一峰的网络日志
Cyberwarzone
Cyberwarzone
博客园 - Franky
Y
Y Combinator Blog
Spread Privacy
Spread Privacy
N
News and Events Feed by Topic
The Cloudflare Blog
Simon Willison's Weblog
Simon Willison's Weblog
S
SegmentFault 最新的问题
W
WeLiveSecurity
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
I
Intezer
Hugging Face - Blog
Hugging Face - Blog
Attack and Defense Labs
Attack and Defense Labs

博客园 - 雷明

Ado.Net读取Excel常见问题总结 Win7 安装.net framework 4.0 失败,错误HRESULT 0xc8000222解决办法 Excel中删除链接 ipad 3.2.2 IPAD 越狱教程 ASP.NET 链接 Access 数据库路径问题最终解决方案 代理上网后localhost使用不了,只能使用127.0.0.1解决 Windows API函数大全 一个技术转销售人员的感悟--深刻(转) 两个Excel表联动 orcale数据库的导入导出 ORACLE 创建表空间 销售七字真诀 十年创业的一封信 业务员成长计划(转载业务员网) Nokia5230刷机呕心历程 开启SHAREPOINT 2007匿名用户搜索功能及moss搜索界面样式更换 MOSS爬网-搜索自定义带参数信息 WCF创建Rest服务(附:.net2.0创建Rest服务) Hosting WCF in SharePoint 2007 (Part 1) 基本部署(转)
.net 消息队列简单实例
雷明 · 2010-09-28 · via 博客园 - 雷明

刚学习微软的消息队列,通过查找资料做了一个简单的实例,大概步骤和代码如下:

1、首页在操作系统上安装消息队列

控制面板—>添加安装程序:在IIS(window2003应用程序服务)下面,钩选"消息队列"

图一:

/Files/lmjob/clip_image002_thumb.jpg

2、新建ASP.NET Web应用程序,并添加引用"System.Messaging"类

3、创建消息、发送消息、接收消息代码:

/// <summary>
        
/// 创建消息队列
        
/// </summary>
        
/// <param name="name">消息队列名称</param>
        
/// <returns></returns>
        public static string CreateNewQueue(string name)
        {
            
if (!System.Messaging.MessageQueue.Exists(".\\private$\\" + name))//检查是否已经存在同名的消息队列
            {
                System.Messaging.MessageQueue mq 
= System.Messaging.MessageQueue.Create(".\\private$\\" + name);
                mq.Label 
= "test";
                
return "ok";
            }
            
else
            {
                
return "已经存在";
            }
        }
/// <summary>
        
/// 同步发送消息
        
/// </summary>
        
/// <param name="input">发送的内容</param>
        
/// <param name="queueName">发送哪个消息队列中</param>
        public static void SendMsg(string input, string queueName)
        {
            
// System.Messaging.MessageQueue.Create(".\\private$\\test2");
            System.Messaging.MessageQueue mq = new MessageQueue(".\\private$\\" + queueName);
            System.Messaging.Message message 
= new System.Messaging.Message();
            message.Body 
= input;
            message.Formatter 
= new System.Messaging.XmlMessageFormatter(new Type[] { typeof(string) });
            mq.Send(message);

        }

/// <summary>
        
/// 同步发送消息
        
/// </summary>
        
/// <param name="input">发送的内容</param>
        
/// <param name="queueName">发送哪个消息队列中</param>
        public static void SendMsg(string[] strList, string queueName)
        { 
            System.Messaging.MessageQueue mq 
= new MessageQueue(".\\private$\\" + queueName);
            System.Messaging.Message message 
= new System.Messaging.Message();
            message.Body 
= strList;
            message.Formatter 
= new System.Messaging.XmlMessageFormatter(new Type[] { typeof(string) });
            mq.Send(message);

        }

/// <summary>
        
/// 同步接收消息(消息为字符串)
        
/// </summary>
        
/// <param name="queueName">从哪个消息队列接受消息</param>
        
/// <returns></returns>
        public static string ReceiveMsg(string queueName)
        {
            System.Messaging.MessageQueue mq 
= new MessageQueue(".\\private$\\" + queueName);if (mq.GetAllMessages().Length == 0)//判断是否还有消息,如果没有消息的话Receive方法会一直独占进程直到有消息为止,所以必须判断
            {
                
return "没有消息了";
            }
            
else
            {
                Message m 
= mq.Receive();
                m.Formatter 
= new System.Messaging.XmlMessageFormatter(new Type[] { typeof(string) });
                
return m.Body.ToString();
            }
        }
/// <summary>
        
/// 同步接收消息(消息为数组对象)
        
/// </summary>
        
/// <param name="queueName">从哪个消息队列接受消息</param>
        
/// <returns></returns>
        public static string[] ReceiveMsgList(string queueName)
        {
            System.Messaging.MessageQueue mq 
= new MessageQueue(".\\private$\\" + queueName);if (mq.GetAllMessages().Length == 0)//判断是否还有消息,如果没有消息的话Receive方法会一直独占进程直到有消息为止,所以必须判断
            {
                
return null;
            }
            
else
            {
                Message m 
= mq.Receive();
                m.Formatter 
= new System.Messaging.XmlMessageFormatter(new Type[] { typeof(string[]) });
                
return (string[])m.Body;
            }
        }
#region 发送消息事件
        
protected void btn_Send_Click(object sender, EventArgs e)
        {
            SendMsg(
this.txt_Message.Text, "LmjobMsg"); //消息为数组对象
            
//string[] list = new string[] { "王一", "王二", "王三", "王四" };
            
//SendMsg(list, "LmjobMsg"); 
        }
        
#endregion#region 接收消息事件
        
protected void btn_Accept_Click(object sender, EventArgs e)
        {
            
string messageBody = ReceiveMsg("LmjobMsg");
            
this.txt_Accept.Text = messageBody;////如果消息为数组对象
            //string[] messageBody = ReceiveMsgList("LmjobMsg");//string messageValue = "";
            
//if (null != messageBody)
            
//{
            
//    foreach (string a in messageBody)
            
//    {
            
//        messageValue += a+",";
            
//    }
            
//}
            
//this.txt_Accept.Text = messageValue;  
        }
        
#endregion

经测试发现:1、如果接收到消息后,消息管理器在消息队列中立即删除该消息。2、消息队列可以传输多种类型数据(例:字符串、整形、对象等)

附:项目代码  

/Files/lmjob/MSMQTest.rar