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

查看原始的英语文章: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 消息队列交互的包装。

      请注意设置您在计算机管理控制台中创建专用队列的路径的语法。专用队列的形式
      machinename\Private$ \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("\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));
    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("\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 网站︰

返回页首

验证代码

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

    注意:付款邮件的内容都序列化为 XML。

  5. 返回到帐单支付 Windows 应用程序,然后单击处理付款按钮。您将看到一个消息框,确认收到的消息,并显示消息。

返回页首

疑难解答

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

返回页首

参考资料


有关 Microsoft 消息队列的详细信息,请访问下面的 Microsoft 网站︰

如何写入和读取从 Microsoft 消息队列在 Visual C#的更多相关文章

  1. 流处理与消息队列------《Designing Data-Intensive Applications》读书笔记16

    上一篇聊了聊批处理的缺点,对于无界数据来说,流处理会是更好的选择,"流"指的是随着时间的推移逐步增加的数据.消息队列可以将这些流组织起来,快速的在应用程序中给予反馈.但是消息队列与 ...

  2. 详解linux进程间通信-消息队列

    前言:前面讨论了信号.管道的进程间通信方式,接下来将讨论消息队列. 一.系统V IPC 三种系统V IPC:消息队列.信号量以及共享内存(共享存储器)之间有很多相似之处. 每个内核中的 I P C结构 ...

  3. C#内存映射文件消息队列实战演练(MMF—MQ)

    一.课程介绍 本次分享课程属于<C#高级编程实战技能开发宝典课程系列>中的一部分,阿笨后续会计划将实际项目中的一些比较实用的关于C#高级编程的技巧分享出来给大家进行学习,不断的收集.整理和 ...

  4. Posix消息队列注意事项

    随内核的持续性 读总是返回最高优先级的最早消息. 当往一个空队列放置一个消息时,允许产生一个信号或启动一个线程. 可认为是一个消息链表 队列中每个消息具有 1.一个无符号整数优先级 2.消息的数据部分 ...

  5. 【RabbitMQ学习记录】- 消息队列存储机制源码分析

    本文来自 网易云社区 . RabbitMQ在金融系统,OpenStack内部组件通信和通信领域应用广泛,它部署简单,管理界面内容丰富使用十分方便.笔者最近在研究RabbitMQ部署运维和代码架构,本篇 ...

  6. linux 进程学习笔记-消息队列messagequeue

    可以想象,如果两个进程都可以访问同一个队列:其中一个进程(sender)向其中写入结构化数据,另外一个进程(receiver)再从其中把结构化的数据读取出来.那么这两个进程就是在利用这个队列进行通信了 ...

  7. System V IPC(1)-消息队列

    一.概述                                                    System V三种IPC:消息队列,信号量,共享内存.这三种IPC最先出现在AT&am ...

  8. PHP结合memcacheq消息队列解决并发问题

    在处理业务逻辑时有可能遇到高并发问题,例如商城秒杀.微博评论等.如果不做任何措施可能在高瞬间造成服务器瘫痪,如何解决这个问题呢?队列是个不错的选择.队列(Queue)又称先进先出(First In F ...

  9. 第十一章 企业项目开发--消息队列activemq

    注意:本章代码基于 第十章 企业项目开发--分布式缓存Redis(2) 代码的github地址:https://github.com/zhaojigang/ssmm0 消息队列是分布式系统中实现RPC ...

随机推荐

  1. 洛谷P1373 小a和uim之大逃离 dp

    正解:dp 解题报告: 传送门! 同样是看到列表发的题解就想着跟着做下dp的题目趴 然后发现还挺难的,,,反正我只大概想到怎么转移但是初始化什么的都不会TT 所以还是大概说下QAQ 首先可以想到设f[ ...

  2. telnet到RedHat Linux失败--解决办法

    失败原因: 1.telnet包未安装,检查telnet包是否安装: [root@vm-rhel root]# rpm -qa telnet telnet-0.17-25 表示已安装 2.telnet包 ...

  3. EF Code First 学习笔记:表映射(转)

      多个实体映射到一张表 Code First允许将多个实体映射到同一张表上,实体必须遵循如下规则: 实体必须是一对一关系 实体必须共享一个公共键 观察下面两个实体: public class Per ...

  4. python实现指定目录下批量文件的单词计数:并发版本

    在 文章 <python实现指定目录下批量文件的单词计数:串行版本>中, 总体思路是: A. 一次性获取指定目录下的所有符合条件的文件 -> B. 一次性获取所有文件的所有文件行 - ...

  5. linux下高并发网络应用注意事项

    本文转自:http://www.blogjava.net/bacoo/archive/2012/06/11/380500.html linux下高并发网络应用注意事项 vi /etc/sysctl.c ...

  6. ThinkPHP5 快速入门文档

    一. 5.0版本采用模块化的设计架构,默认的应用目录下面只有一个index模块目录,如果我要添加新的模块可以使用控制台命令来生成. 切换到命令行模式下,进入到应用根目录并执行如下指令: php thi ...

  7. 20145325张梓靖 《Java程序设计》第7周学习总结

    20145325张梓靖 <Java程序设计>第7周学习总结 教材学习内容总结 时间的度量 格林威治时间,简称GMT时间,由观察太阳而得来:世界时,UT:国际原子时,TAI:世界协调时间,U ...

  8. 防止putty的鼠标右键错误粘贴

    一.环境 发行版:Ubuntu 18.04.1 LTS 代号:bionic 内核版本:4.15.0-30-generic 二.背景 每次从putty复制时,会单击鼠标右击,以便复制出终端的内容,但是一 ...

  9. 2017NOIP模拟赛-科普基地

    今天回来打的第一场NOIP难度的试题,结果惨不忍睹.写一下每道题的做法,然后每道题犯的__弱智__错误 UPD:2018.9.15 突然这篇题解就变成很多大佬要看的了,因为之前是写给自己看的,所以写的 ...

  10. Android -- 加载大图片到内存,从gallery获取图片,获取图片exif信息

    1. 加载大图片到内存,从gallery获取图片 android默认的最大堆栈只有16M, 图片像素太高会导致内存不足的异常, 需要将图片等比例缩小到适合手机屏幕分辨率, 再加载. 从gallery ...