如何写入和读取从 Microsoft 消息队列在 Visual C#

注意:这篇文章是由无人工介入的微软自动的机器翻译软件翻译完成。微软很高兴能同时提供给您由人工翻译的和由机器翻译的文章, 以使您能使用您的语言访问所有的知识库文章。然而由机器翻译的文章并不总是完美的。它可能存在词汇,语法或文法的问题,就像是一个外国人在说中文时总是可能犯这样的错误。虽然我们经常升级机器翻译软件以提高翻译质量,但是我们不保证机器翻译的正确度,也不对由于内容的误译或者客户对它的错误使用所引起的任何直接的, 或间接的可能的问题负责。如果您发现了错误并希望帮助我们提高机器翻译技术,请完成文章末尾的在线调查。

查看原始的英语文章:815811

 
这篇文章的 Microsoft Visual Basic.NET 版本,请参见
315698 

在此任务

概要


本文介绍以下内容︰
  • 如何创建一条消息并将其发送给 Microsoft 消息队列在 Windows 应用程序中。
  • 如何从专用队列读取和反序列化用于显示邮件的内容。
返回页首

要求

下列项目描述了推荐使用的硬件、 软件、 网络基础结构、 技能和知识和所需的服务包︰
  • 使用 Microsoft 消息队列安装下列操作系统之一 (它是作为四个操作系统上的一个选项)︰ Windows 2000 专业版 (或服务器),或 Windows XP 专业版 (或服务器)。
本文还假定您已熟悉以下主题︰
  • Microsoft 消息队列
  • 从命令提示符处使用工具
返回页首

写入和读取从 Microsoft 消息队列


在 Microsoft.NET Framework 中的System.Messaging命名空间都有必须要对 Microsoft 消息队列中读取和写入的类。要创建小型模拟在线帐单支付系统的 Windows 应用程序,请执行以下步骤︰
  1. 打开 Microsoft Visual Studio.NET 或 Microsoft Visual Studio 2005年。
  2. 创建新的 Windows 应用程序中可视化 C#,并命名该MSMQ。
  3. 若要显示解决方案资源管理器中,如果它不出现,请按 CTRL + ALT + L。在解决方案资源管理器中右键单击引用,然后单击添加引用。
  4. 在上
    .NET选项卡上,选择System.Messaging.dll文件中的.dll 文件的列表。单击选择,然后单击确定。

    注意:在 Visual Studio 2005 中,单击列表中的 Dll 的System.Messaging.dll文件,然后单击确定。
  5. Form1.cs 已在设计视图中打开。如果未打开,请双击解决方案资源管理器中的Form1.cs。
  6. 按下 CTRL + ALT + X 若要打开工具箱。在
    工具箱,单击Windows 窗体选项卡。

  7. 工具箱中,将以下对象的中间
    Form1:
    • 4 行每个标签和文本框(位于右侧的每个标签)。
    • 在下面的标签和文本框,将两个按钮控件拖拖到Form1上。
  8. 用鼠标右键单击控件,请单击
    属性,然后设置以下 (按顺序) 的标签的Text属性︰
    • 支付给︰
    • 您的姓名:
    • 数量︰
    • 截止日期︰
  9. 属性对话框中,将文本属性设置
    发送付款、 和集button1
    处理付款为button2的text属性。
  10. 此应用程序的工作必须首先在计算机管理控制台创建的专用队列。若要执行此操作,请执行以下步骤:
    1. 在桌面上右键单击我的电脑,然后单击
      管理。
    2. 展开服务和应用程序节点,以查找消息队列。

      注意:如果未找到消息队列,说明它未安装。
  11. 展开消息队列,用鼠标右键单击专用队列指向
    新建,然后单击专用队列。
  12. 队列名称框中,键入
    billpay,然后单击确定。

    注意:不要选择事务性复选框。将计算机管理控制台打开,因为返回到它以后,若要查看邮件。
  13. 在 Form1 代码的顶部,添加两个 USING 语句以包含驻留在System.Messaging命名空间和System.Text命名空间中的其他类的类声明之前。( System.Text命名空间是使用StringBuilder类中,最好是在连接字符串时要使用的新的.NET Framework 类。)
    using System.Messaging;using System.Text;

  14. 创建一个包含变量以保存定义付款的数据结构。创建结构,请在Main过程之后添加以下代码︰
    public struct Payment{
    public string Payor,Payee;
    public int Amount;
    public string DueDate;
    }
  15. button1的Click事件在下列步骤中添加的代码。
    1. 将结构的属性设置为窗体元素的值,如下所示︰
      Payment myPayment;  myPayment.Payor = textBox1.Text;
      myPayment.Payee = textBox2.Text;
      myPayment.Amount = Convert.ToInt32(textBox3.Text);
      myPayment.DueDate = textBox4.Text;
    2. 创建邮件类的一个实例,然后将Body属性设置为的支付结构︰
      System.Messaging.Message msg = new System.Messaging.Message();msg.Body=myPayment;
    3. 若要将邮件发送到 Microsoft 消息队列、 创建MessageQueue类的实例并调用Send方法传递消息对象中。MessageQueue类是管理与 Microsoft 消息队列交互的包装。

      请注意设置您在计算机管理控制台中创建专用队列的路径的语法。专用队列的形式
      machinenamePrivate$ queuename。本地主机机用圆点或句点 (显示为".") 引用。
      MessageQueue msgQ =new MessageQueue(".\Private$\billpay");msgQ.Send(msg);

      代码现在存在向 Microsoft 消息队列发送一条消息。.NET Framework 使用XMLMessageFormatter对象,自动序列化消息。发送消息时,隐式创建此对象。
  16. button2的Click事件在下列步骤中添加的代码。Button2_Click事件处理程序接收并处理付款中的button1事件处理程序发送的消息。
    1. 第一行代码是代码的相同的第一个事件处理程序中行︰
      MessageQueue msgQ = new MessageQueue(".\Private$\billpay");
    2. 创建一个要传递给类型的数组
      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如何反序列化消息。
    3. 通过调用Receive方法接收消息。Body属性来读取邮件内容的访问。Body属性返回一个对象,因此具有对象强制转换为付款类型以检索可用的窗体中的内容︰
      StringBuilder sb = new StringBuilder();sb.Append("Payment paid to: " + myPayment.Payor);
      sb.Append(" ");
      sb.Append("Paid by: " + myPayment.Payee);
      sb.Append(" ");
      sb.Append("Amount: $" + myPayment.Amount.ToString());
      sb.Append(" ");
      sb.Append("Due Date: " + Convert.ToDateTime(myPayment.DueDate));
    4. 创建消息框中显示结果︰
      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(" ");
sb.Append("Paid by: " + myPayment.Payee);
sb.Append(" ");
sb.Append("Amount: $" + myPayment.Amount.ToString());
sb.Append(" ");
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 网站︰ 返回页首

验证代码

  1. 调试菜单上,单击
    启动。
  2. 在每个文本框中,键入值,然后单击发送付款。
  3. 返回到计算机管理控制台。单击下billpay,专用队列中的队列消息文件夹并验证,Microsoft 消息队列接收消息 (如信封图标所示)。
  4. 用鼠标右键单击邮件,单击属性,然后单击正文选项卡。您需注意的付款信息。

    注意:付款邮件的内容都序列化为 XML。
  5. 返回到帐单支付 Windows 应用程序,然后单击处理付款按钮。您将看到一个消息框,确认收到的消息,并显示消息。
返回页首

疑难解答

  • 专用队列的缺乏通常是仅在 Windows 2000 专业版和 Windows XP 专业版的问题。Windows 2000 Server 和 Windows XP 服务器允许的公共队列的使用。
  • 将正确的参数传递给XMLMessageFormatter()可能会比较棘手。在此示例中,如果付款类型或对象不包括类型数组传递到构造函数中引发异常。
返回页首

参考资料


有关 Microsoft 消息队列的详细信息,请访问下面的 Microsoft 网站︰
原文地址:https://www.cnblogs.com/zxtceq/p/8556212.html