如何写入和读取从 Microsoft 消息队列在 Visual C#
注意:这篇文章是由无人工介入的微软自动的机器翻译软件翻译完成。微软很高兴能同时提供给您由人工翻译的和由机器翻译的文章, 以使您能使用您的语言访问所有的知识库文章。然而由机器翻译的文章并不总是完美的。它可能存在词汇,语法或文法的问题,就像是一个外国人在说中文时总是可能犯这样的错误。虽然我们经常升级机器翻译软件以提高翻译质量,但是我们不保证机器翻译的正确度,也不对由于内容的误译或者客户对它的错误使用所引起的任何直接的, 或间接的可能的问题负责。如果您发现了错误并希望帮助我们提高机器翻译技术,请完成文章末尾的在线调查。
查看原始的英语文章:815811
315698。
在此任务
概要
- 如何创建一条消息并将其发送给 Microsoft 消息队列在 Windows 应用程序中。
- 如何从专用队列读取和反序列化用于显示邮件的内容。
要求
下列项目描述了推荐使用的硬件、 软件、 网络基础结构、 技能和知识和所需的服务包︰
- 使用 Microsoft 消息队列安装下列操作系统之一 (它是作为四个操作系统上的一个选项)︰ Windows 2000 专业版 (或服务器),或 Windows XP 专业版 (或服务器)。
本文还假定您已熟悉以下主题︰
- Microsoft 消息队列
- 从命令提示符处使用工具
写入和读取从 Microsoft 消息队列
在 Microsoft.NET Framework 中的System.Messaging命名空间都有必须要对 Microsoft 消息队列中读取和写入的类。要创建小型模拟在线帐单支付系统的 Windows 应用程序,请执行以下步骤︰
- 打开 Microsoft Visual Studio.NET 或 Microsoft Visual Studio 2005年。
- 创建新的 Windows 应用程序中可视化 C#,并命名该MSMQ。
- 若要显示解决方案资源管理器中,如果它不出现,请按 CTRL + ALT + L。在解决方案资源管理器中右键单击引用,然后单击添加引用。
- 在上
.NET选项卡上,选择System.Messaging.dll文件中的.dll 文件的列表。单击选择,然后单击确定。注意:在 Visual Studio 2005 中,单击列表中的 Dll 的System.Messaging.dll文件,然后单击确定。
- Form1.cs 已在设计视图中打开。如果未打开,请双击解决方案资源管理器中的Form1.cs。
- 按下 CTRL + ALT + X 若要打开工具箱。在
工具箱,单击Windows 窗体选项卡。 - 从
工具箱中,将以下对象的中间
Form1:- 4 行每个标签和文本框(位于右侧的每个标签)。
- 在下面的标签和文本框,将两个按钮控件拖拖到Form1上。
- 用鼠标右键单击控件,请单击
属性,然后设置以下 (按顺序) 的标签的Text属性︰- 支付给︰
- 您的姓名:
- 数量︰
- 截止日期︰
- 在属性对话框中,将文本属性设置
发送付款、 和集button1
处理付款为button2的text属性。 - 此应用程序的工作必须首先在计算机管理控制台创建的专用队列。若要执行此操作,请执行以下步骤:
- 在桌面上右键单击我的电脑,然后单击
管理。 - 展开服务和应用程序节点,以查找消息队列。
注意:如果未找到消息队列,说明它未安装。
- 在桌面上右键单击我的电脑,然后单击
- 展开消息队列,用鼠标右键单击专用队列指向
新建,然后单击专用队列。 - 在队列名称框中,键入
billpay,然后单击确定。注意:不要选择事务性复选框。将计算机管理控制台打开,因为返回到它以后,若要查看邮件。
- 在 Form1 代码的顶部,添加两个 USING 语句以包含驻留在System.Messaging命名空间和System.Text命名空间中的其他类的类声明之前。( System.Text命名空间是使用StringBuilder类中,最好是在连接字符串时要使用的新的.NET Framework 类。)
using System.Messaging;
using System.Text;
- 创建一个包含变量以保存定义付款的数据结构。创建结构,请在Main过程之后添加以下代码︰
public struct Payment
{
public string Payor,Payee;
public int Amount;
public string DueDate;
} - 为button1的Click事件在下列步骤中添加的代码。
- 将结构的属性设置为窗体元素的值,如下所示︰
Payment myPayment;
myPayment.Payor = textBox1.Text;
myPayment.Payee = textBox2.Text;
myPayment.Amount = Convert.ToInt32(textBox3.Text);
myPayment.DueDate = textBox4.Text; - 创建邮件类的一个实例,然后将Body属性设置为的支付结构︰
System.Messaging.Message msg = new System.Messaging.Message();
msg.Body=myPayment;
- 若要将邮件发送到 Microsoft 消息队列、 创建MessageQueue类的实例并调用Send方法传递消息对象中。MessageQueue类是管理与 Microsoft 消息队列交互的包装。
请注意设置您在计算机管理控制台中创建专用队列的路径的语法。专用队列的形式
machinename\Private$ \queuename。本地主机机用圆点或句点 (显示为".") 引用。MessageQueue msgQ =new MessageQueue(".\\Private$\\billpay");
msgQ.Send(msg);
代码现在存在向 Microsoft 消息队列发送一条消息。.NET Framework 使用XMLMessageFormatter对象,自动序列化消息。发送消息时,隐式创建此对象。
- 将结构的属性设置为窗体元素的值,如下所示︰
- 为button2的Click事件在下列步骤中添加的代码。Button2_Click事件处理程序接收并处理付款中的button1事件处理程序发送的消息。
- 第一行代码是代码的相同的第一个事件处理程序中行︰
MessageQueue msgQ = new MessageQueue(".\\Private$\\billpay");
- 创建一个要传递给类型的数组
XMLMessageFormatter类。
注意:在接收消息时,必须显式创建此类。XMLMessageFormatter类的构造函数采用类型名称的字符串数组或,更最好,类型数组的类型︰Payment myPayment=new Payment();
Object o=new Object();
System.Type[] arrTypes=new System.Type [2];
arrTypes[0] = myPayment.GetType();
arrTypes[1] = o.GetType();
msgQ.Formatter = new XmlMessageFormatter(arrTypes);
myPayment=((Payment)msgQ.Receive().Body);这些类型告诉XMLMessageFormatter如何反序列化消息。
- 通过调用Receive方法接收消息。Body属性来读取邮件内容的访问。Body属性返回一个对象,因此具有对象强制转换为付款类型以检索可用的窗体中的内容︰
StringBuilder sb = new StringBuilder();
sb.Append("Payment paid to: " + myPayment.Payor);
sb.Append("\n");
sb.Append("Paid by: " + myPayment.Payee);
sb.Append("\n");
sb.Append("Amount: $" + myPayment.Amount.ToString());
sb.Append("\n");
sb.Append("Due Date: " + Convert.ToDateTime(myPayment.DueDate)); - 创建消息框中显示结果︰
MessageBox.Show(sb.ToString(), "Message Received!");
- 第一行代码是代码的相同的第一个事件处理程序中行︰
完整的代码列表 (Form1.cs)
using System.Messaging;using System.Text;
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace WindowsApplication1
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.TextBox textBox3;
private System.Windows.Forms.TextBox textBox4;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.label4 = new System.Windows.Forms.Label();
this.textBox1 = new System.Windows.Forms.TextBox();
this.textBox2 = new System.Windows.Forms.TextBox();
this.textBox3 = new System.Windows.Forms.TextBox();
this.textBox4 = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// label1
//
this.label1.Location = new System.Drawing.Point(8, 24);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(104, 32);
this.label1.TabIndex = 0;
this.label1.Text = "Pay To:";
//
// label2
//
this.label2.Location = new System.Drawing.Point(8, 80);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(104, 32);
this.label2.TabIndex = 1;
this.label2.Text = "Your Name:";
//
// label3
//
this.label3.Location = new System.Drawing.Point(8, 136);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(112, 32);
this.label3.TabIndex = 2;
this.label3.Text = "Amount:";
//
// label4
//
this.label4.Location = new System.Drawing.Point(8, 184);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(104, 40);
this.label4.TabIndex = 3;
this.label4.Text = "Due To:";
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(152, 24);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(128, 20);
this.textBox1.TabIndex = 4;
this.textBox1.Text = "textBox1";
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(160, 80);
this.textBox2.Name = "textBox2";
this.textBox2.TabIndex = 5;
this.textBox2.Text = "textBox2";
//
// textBox3
//
this.textBox3.Location = new System.Drawing.Point(160, 128);
this.textBox3.Name = "textBox3";
this.textBox3.Size = new System.Drawing.Size(112, 20);
this.textBox3.TabIndex = 6;
this.textBox3.Text = "textBox3";
//
// textBox4
//
this.textBox4.Location = new System.Drawing.Point(160, 184);
this.textBox4.Name = "textBox4";
this.textBox4.Size = new System.Drawing.Size(120, 20);
this.textBox4.TabIndex = 7;
this.textBox4.Text = "textBox4";
//
// button1
//
this.button1.Location = new System.Drawing.Point(8, 232);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(104, 40);
this.button1.TabIndex = 8;
this.button1.Text = "Send Payment";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(160, 232);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(120, 40);
this.button2.TabIndex = 9;
this.button2.Text = "Process Payment";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.textBox4);
this.Controls.Add(this.textBox3);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.label4);
this.Controls.Add(this.label3);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void button1_Click(object sender, System.EventArgs e)
{
Payment myPayment;
myPayment.Payor = textBox1.Text;
myPayment.Payee = textBox2.Text;
myPayment.Amount = Convert.ToInt32(textBox3.Text);
myPayment.DueDate = textBox4.Text;
System.Messaging.Message msg = new System.Messaging.Message();
msg.Body=myPayment;
MessageQueue msgQ =new MessageQueue(".\\Private$\\billpay");
msgQ.Send(msg);
}
private void button2_Click(object sender, System.EventArgs e)
{
MessageQueue msgQ = new MessageQueue(".\\Private$\\billpay");
Payment myPayment=new Payment();
Object o=new Object();
System.Type[] arrTypes=new System.Type [2];
arrTypes[0] = myPayment.GetType();
arrTypes[1] = o.GetType();
msgQ.Formatter = new XmlMessageFormatter(arrTypes);
myPayment=((Payment)msgQ.Receive().Body);
StringBuilder sb = new StringBuilder();
sb.Append("Payment paid to: " + myPayment.Payor);
sb.Append("\n");
sb.Append("Paid by: " + myPayment.Payee);
sb.Append("\n");
sb.Append("Amount: $" + myPayment.Amount.ToString());
sb.Append("\n");
sb.Append("Due Date: " + Convert.ToDateTime(myPayment.DueDate));
MessageBox.Show(sb.ToString(), "Message Received!");
}
public struct Payment
{
public string Payor,Payee;
public int Amount;
public string DueDate;
}
}
}
注意:应在 Visual Studio 2005年中更改代码。创建一个 Windows 窗体项目时,Visual C# 一个窗体向项目中添加默认情况。此窗体名为 Form1。Form1.cs 和 Form1.designer.cs 命名的两个文件的表示形式。在 Form1.cs 中编写代码。Designer.cs 文件是 Windows 窗体设计器编写代码实现所有操作您通过添加控件来执行。有关 Windows 窗体设计器在 Visual C# 2005年中的详细信息,请访问下面的 Microsoft 网站︰
验证代码
- 在调试菜单上,单击
启动。 - 在每个文本框中,键入值,然后单击发送付款。
- 返回到计算机管理控制台。单击下billpay,专用队列中的队列消息文件夹并验证,Microsoft 消息队列接收消息 (如信封图标所示)。
- 用鼠标右键单击邮件,单击属性,然后单击正文选项卡。您需注意的付款信息。
注意:付款邮件的内容都序列化为 XML。
- 返回到帐单支付 Windows 应用程序,然后单击处理付款按钮。您将看到一个消息框,确认收到的消息,并显示消息。
疑难解答
- 专用队列的缺乏通常是仅在 Windows 2000 专业版和 Windows XP 专业版的问题。Windows 2000 Server 和 Windows XP 服务器允许的公共队列的使用。
- 将正确的参数传递给XMLMessageFormatter()可能会比较棘手。在此示例中,如果付款类型或对象不包括类型数组传递到构造函数中引发异常。
参考资料
如何写入和读取从 Microsoft 消息队列在 Visual C#的更多相关文章
- 流处理与消息队列------《Designing Data-Intensive Applications》读书笔记16
上一篇聊了聊批处理的缺点,对于无界数据来说,流处理会是更好的选择,"流"指的是随着时间的推移逐步增加的数据.消息队列可以将这些流组织起来,快速的在应用程序中给予反馈.但是消息队列与 ...
- 详解linux进程间通信-消息队列
前言:前面讨论了信号.管道的进程间通信方式,接下来将讨论消息队列. 一.系统V IPC 三种系统V IPC:消息队列.信号量以及共享内存(共享存储器)之间有很多相似之处. 每个内核中的 I P C结构 ...
- C#内存映射文件消息队列实战演练(MMF—MQ)
一.课程介绍 本次分享课程属于<C#高级编程实战技能开发宝典课程系列>中的一部分,阿笨后续会计划将实际项目中的一些比较实用的关于C#高级编程的技巧分享出来给大家进行学习,不断的收集.整理和 ...
- Posix消息队列注意事项
随内核的持续性 读总是返回最高优先级的最早消息. 当往一个空队列放置一个消息时,允许产生一个信号或启动一个线程. 可认为是一个消息链表 队列中每个消息具有 1.一个无符号整数优先级 2.消息的数据部分 ...
- 【RabbitMQ学习记录】- 消息队列存储机制源码分析
本文来自 网易云社区 . RabbitMQ在金融系统,OpenStack内部组件通信和通信领域应用广泛,它部署简单,管理界面内容丰富使用十分方便.笔者最近在研究RabbitMQ部署运维和代码架构,本篇 ...
- linux 进程学习笔记-消息队列messagequeue
可以想象,如果两个进程都可以访问同一个队列:其中一个进程(sender)向其中写入结构化数据,另外一个进程(receiver)再从其中把结构化的数据读取出来.那么这两个进程就是在利用这个队列进行通信了 ...
- System V IPC(1)-消息队列
一.概述 System V三种IPC:消息队列,信号量,共享内存.这三种IPC最先出现在AT&am ...
- PHP结合memcacheq消息队列解决并发问题
在处理业务逻辑时有可能遇到高并发问题,例如商城秒杀.微博评论等.如果不做任何措施可能在高瞬间造成服务器瘫痪,如何解决这个问题呢?队列是个不错的选择.队列(Queue)又称先进先出(First In F ...
- 第十一章 企业项目开发--消息队列activemq
注意:本章代码基于 第十章 企业项目开发--分布式缓存Redis(2) 代码的github地址:https://github.com/zhaojigang/ssmm0 消息队列是分布式系统中实现RPC ...
随机推荐
- python装饰器的应用案例
目录 一.过程编程 二.面向装饰器和函数的编程 三.二的加强版 一.过程编程 (一)需求:打印菱形 1.空格.*号组成的菱形 2.输入菱形上半部分的行数即可打印 3.支持循环输入 (二)代码 from ...
- LeetCode——Palindrome Number
Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could negativ ...
- [WorldWind学习]20.修改ShapeFileLayer类及托管D3D文字绘制方法
PluginSDK\ShapeFileLayer.cs Line:1027char[] fieldDataChars = dbfReader.ReadChars(fieldHeaders[j].Fie ...
- Java系列介绍
Java系列目录 重新编写equals()方法,hashCode()方法,以及toString(),提供自定义的相等标准,以及自描述函数 Java 7新增功能 Java应用程序中System.out. ...
- [golang note] 接口使用
侵入式接口 √ 在其他一些编程语言中,接口主要是作为不同组件之间的契约存在,即规定双方交互的规约. √ 对契约的实现是强制的,即必须确保用户的确实现了该接口,而实现一个接口,需要从该接口继承. √ 如 ...
- testng生成报告ReportNG美化测试报告
testng生成报告ReportNG美化测试报告 testng生成报告ReportNG美化测试报告 ReportNG 是一个配合TestNG运行case后自动帮你在test-output文件内生成一个 ...
- C# Winform backgroundWorker组件使用
BackgroundWorker 组件用来执行诸如数据库事务.文件下载等耗时的异步操作. 开始 在应用程序中添加一个BackgroundWorker实例,如果用的是VS,可以从工具上直接拖到应用程序: ...
- Python 在字符串中处理html 和xml
问题: 想将HTML 或者XML 实体如&entity; 或&#code; 替换为对应的文本.再者,你需要转换文本中特定的字符(比如<, >, 或&). 解决方案: ...
- spring AOP的两种代理
本篇记录下spring AOP的两种代理,为下一篇AOP实现做下铺垫. 1.JDK动态代理 2.cglib代理 1.如果目标对象实现了接口,默认情况下会采用JDK的动态代理实现AOP2.如果目标对象 ...
- loj2163 / bzoj2212 / P3521 [POI2011]ROT-Tree Rotations(线段树合并)
P3521 [POI2011]ROT-Tree Rotations loj2163 [POI2011]ROT-Tree Rotations(数据加强) (loj的数据套了个fread优化才过...) ...