在这篇博客中,我们将就如何实现System.Messaging类发送和接收的XML消息发送从MSMQ队列,你可能会遇到接收的XML消息的一些问题。

我们将首先加入参考System.Messaging DLL。 DLL的路径是:
C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\Profile\Client\

发送消息到MSMQ队列

Load XML message content:
XmlDocument xd = new XmlDocument();
xd.Load(@"c:\temp\myxml.xml"); 
Create a new message using the following code:
System.Messaging.Message msg = new System.Messaging.Message();

Set the message label and message body:
msg.Label = "TestMessage"; msg.Body = xd.InnerXml;

msg.UseDeadLetterQueue = true;  // to send the message to the dead letter queue in case if there is some issue while sending.

Create an object of the queue to which you want to send the message:
MessageQueue msgQ = new MessageQueue(".\\private$\\QueueName");

Use send() function to send the message msgQ.Send(msg);

The corresponding code in VB is as follows:
Dim xd As New XmlDocument() xd.Load("c:\temp\myxml.xml")
Dim msg As New System.Messaging.Message()
msg.Label = "TestMessage"
msg.Body = xd.InnerXml
msg.UseDeadLetterQueue = True
Dim msgQ As New MessageQueue(".\private$\QueueName ")
msgQ.Send(msg)

您将看到消息队列

注:

<String> Wrapper and <?xml version="1.0"?>

MSMQ接收消息

创建消息并从要接收消息队列的对象:

MessageQueue msgQ = new MessageQueue(".\\private$\\QueueName");
System.Messaging.Message m = new System.Messaging.Message();

Use receive() function to receive the message:
m = msgQ.Receive();

Use XMLMessageFormatter to receive the message without the string wrapper.

m.Formatter = new XmlMessageFormatter(new String[] {"System.String,mscorlib"});
string text = m.Body.ToString();

Save the message to the file location to test:
XmlDocument xml = new XmlDocument();
xml.LoadXml(text);
xml.Save(@"c:\temp\myxml.xml");

在VB对应的代码如下:

Dim m As New System.Messaging.Message()
Dim msgQ As New MessageQueue(".\private$\Testpgm")
m = msgQ.Receive()
m.Formatter = New XmlMessageFormatter(New [String]() {"System.String,mscorlib"})
Dim text As String = m.Body.ToString()
Dim xml As New XmlDocument()
xml.LoadXml(text)
xml.Save("c:\temp\myxml.xml")

获取包装信息:

为了获得给XMLMessageFormatter的不具有一个<刺>包装,使用ActiveXMessageFormatter(消息),而不是()。

我附上供参考样品溶液。

样品片段发送\接收信息到远程MSMQ队列:

//发送到公众非事务性队列

公共无效SendPublicNonTx()
{
MessageQueue myQueue中=新MessageQueue();
myQueue.Path =“计算机名\\ testntx”;
myQueue.Send(“队列格式名称。”);
返回;

}

//Sending to Public Non-Transactional queue

public void SendPublicNonTx()
        { 
MessageQueue myQueue = new MessageQueue();         
myQueue.Path = "machinename\\testntx";                       
myQueue.Send("Queue by format name.");           
return;

}

//Sending to Public Non-Transactional queue

public void SendPublicNonTx()
        {           
MessageQueue myQueue = new MessageQueue("FormatName:Public=8FC22E24-C378-4F9A-91FC-550785FC495E");           
myQueue.Send("Queue by format name.");           
return;
        }
//Sending to Private Transactional queue
public void SendPrivateTx() 
        {           
MessageQueue rmQ = new MessageQueue("FormatName:Direct=OS:machinename\\private$\\testq");
            rmQ.Send("message", MessageQueueTransactionType.Single);
        }

//Sending to Private Non-Transactional queue
        public void SendPrivateNonTx()
        { 
            MessageQueue myQueue = new MessageQueue(@"FormatName:Direct=OS:machinename\private$\testNontx");
            myQueue.Send("my message");
            return;
        }

//Sending to Public Transactional queue
        public void SendPublicTx()
        { 
MessageQueue myQueue = new MessageQueue("FormatName:Public= 09BA7806-2FBA-443C-880A-A60D525A0F53"); // we are using the guid to identify the queue.           
myQueue.Send("Queue by format name.", MessageQueueTransactionType.Single);           
return;
        }
//Reading from Remote Public queue:
private void GetFromPublicQueue()
        {           
            MessageQueue mQ = new MessageQueue("FormatName:Public=8FC22E24-C378-4F9A-91FC-550785FC495E"); 
            mQ.Formatter = new XmlMessageFormatter(new Type[] { typeof(string) });
            System.Messaging.Message msg = mQ.Receive();
            MessageBox.Show(msg.Body.ToString());           
            MessageQueue rmTxQ = new MessageQueue("FormatName:Public= 09BA7806-2FBA-443C-880A-A60D525A0F53");
            mTxQ.Formatter = new XmlMessageFormatter(new Type[] { typeof(string) });
            System.Messaging.Message msgTx = mTxQ.Receive(MessageQueueTransactionType.Single);           
            MessageBox.Show(msgTx.Body.ToString());

//Reading from Remote Private queue:
private void GetFromQueue() 
        {            
            MessageQueue mQ = new MessageQueue("FormatName:Direct=OS:machinename\\private$\\testNontx");
            mQ.Formatter = new XmlMessageFormatter(new Type[] { typeof(string) });            
            System.Messaging.Message msg = mQ.Receive();            
            MessageBox.Show(msg.Body.ToString());           
            MessageQueue mTxQ = new MessageQueue("FormatName:Direct=OS:machinename\\private$\\testq");
            mTxQ.Formatter = new XmlMessageFormatter(new Type[] { typeof(string) });
            System.Messaging.Message msgTx = mTxQ.Receive(MessageQueueTransactionType.Single);
            MessageBox.Show(msgTx.Body.ToString());

MessageQueue mTxQ = new MessageQueue("FormatName:Direct=OS:machinename\\private$\\testq");
            mTxQ.Formatter = new XmlMessageFormatter(new Type[] { typeof(string) }); 
            System.Messaging.Message msgTx = mTxQ.Receive(MessageQueueTransactionType.Single); 
            MessageBox.Show(msgTx.Body.ToString());

如何使用C#和VB发送和接收MSMQ消息的更多相关文章

  1. 2.技巧: 用 JAXM 发送和接收 SOAP 消息—Java API 使许多手工生成和发送消息方面必需的步骤自动化

    转自:https://www.cnblogs.com/chenying99/archive/2013/05/23/3094128.html 技巧: 用 JAXM 发送和接收 SOAP 消息—Java ...

  2. (七)发送、接收SOAP消息(以HttpClient方式)(2)

    一.为什么要用soap 原本我们使用web服务都是根据wsdl生成客户端(生成一堆java文件)然后再调用,本章节讲解如何用soap消息来替代这种方式. 二.SOAP消息格式 SOAP(简单对象访问协 ...

  3. XMPP系列(四)---发送和接收文字消息,获取历史消息功能

    今天开始做到最主要的功能发送和接收消息.获取本地历史数据. 先上到目前为止的效果图:              首先是要在XMPPFramework.h中引入数据存储模块: //聊天记录模块的导入 # ...

  4. 解决Springboot整合ActiveMQ发送和接收topic消息的问题

    环境搭建 1.创建maven项目(jar) 2.pom.xml添加依赖 <parent> <groupId>org.springframework.boot</group ...

  5. (六)发送、接收SOAP消息(1)

    一.为什么要用soap 原本我们使用web服务都是根据wsdl生成客户端(生成一堆java文件)然后再调用,本章节讲解如何用soap消息来替代这种方式. 二.SOAP消息格式 SOAP(简单对象访问协 ...

  6. Java Socket发送与接收HTTP消息简单实现

    在上次Java Socket现实简单的HTTP服务我 们实现了简单的HTTP服务,它可以用来模拟HTTP服务,用它可以截获HTTP请求的原始码流,让我们很清楚的了解到我们向服务发的HTTP消息的结 构 ...

  7. Linux系统下UDP发送和接收广播消息小例子

    // 发送端 #include <iostream> #include <stdio.h> #include <sys/socket.h> #include < ...

  8. Linux系统下UDP发送和接收广播消息小样例

    [cpp] view plaincopy // 发送端 #include <iostream> #include <stdio.h> #include <sys/sock ...

  9. 异步接收MSMQ消息

    在这部分,我们将使用ThreadPool 和MSMQ 进行消息收发.MSMQ 是一个分布式队列,通过MSMQ 一个应用程序可以异步地与另外一个应用程序通信. 在一个典型的场景中,我们要向维护一个队列的 ...

随机推荐

  1. The preview is empty because of the setting.Check the generation option.

    前些日子在pd中添加存储过程, 参考:深蓝居的博文 http://www.cnblogs.com/studyzy/archive/2009/12/18/1627334.html 创建视图的时候,会在属 ...

  2. javascript进阶——分离式DOM脚本编程

    编写分离式(unobstrusive)代码意味着对HTML内容的完全分离:数据来自服务器端,javascript代码用来动态化和交互.这种分离的好处是在不同浏览器之间使用是可以完全降级或升级运行,对于 ...

  3. poj 1348 Computing (四个数的加减乘除四则运算)

    http://poj.org/problem?id=1348 Computing Time Limit: 1000MS   Memory Limit: 10000K Total Submissions ...

  4. MUH and Cube Walls

    Codeforces Round #269 (Div. 2) D:http://codeforces.com/problemset/problem/471/D 题意:给定两个序列a ,b, 如果在a中 ...

  5. ASP.NET MVC中HttpContext, HttpContextBase, HttpContextWrapper联系

    ttpContext HttpContext是最原始的ASP.NET Context. MVC的目的之一是能够单元测试.HttpContext没有base class,并且不是virtual,所以不能 ...

  6. CHANGE DEFAULT FTP PORT FOR VSFTP

    http://twincreations.co.uk/change-default-ftp-port-for-vsftp/ http://www.cnblogs.com/kuliuheng/p/320 ...

  7. cocos-html5 Json 灵活 遍历方式 不同方式的缺陷,优点总结

    1,四种解析Json的方式:Part 1 var list1 = [1,3,4]; alert(list1[1]); var list2 = [{"name":"leam ...

  8. QT带OpenGL与不带的区别,QT5是一个伟大的框架,短时期内根本不会有替代者

    你好 , 我Qt的初学者 , 我在官网下载Qt时感觉很迷茫 , 不知道要下载哪个, 麻烦你写他们之间的不同点:Qt 5.2.0 for Windows 32-bit (MinGW 4.8, OpenG ...

  9. [译]GotW #4 Class Mechanics

    你对写一个类的细节有多在行?这条款不仅注重公然的错误,更多的是一种专业的风格.了解这些原则将会帮助你设计易于使用和易于管理的类. JG Question 1. 什么使得接口“容易正确使用,错误使用却很 ...

  10. 如何用C#获得文件信息以及扩展信息

    在C#中获得文件信息很容易,只需要用FileInfo类或者FileVersionInfo类就可以获得,但是如果想要获得文件的扩展信息,则无法从这两类来获得.不过在C#中,这也不是件难事,只要引入“Mi ...