zoukankan      html  css  js  c++  java
  • 如何使用C#和VB发送和接收MSMQ消息

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

    我们将首先加入参考System.Messaging DLL。 DLL的路径是:
    C:Program Files (x86)Reference AssembliesMicrosoftFramework.NETFrameworkv4.0ProfileClient

    发送消息到MSMQ队列

    Load XML message content:
    XmlDocument xd = new XmlDocument();
    xd.Load(@"c: empmyxml.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: empmyxml.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)

    您将看到消息队列
    clip_image001
    clip_image002

    注:

    <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: empmyxml.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: empmyxml.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:machinenameprivate$ estNontx");
                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());

  • 相关阅读:
    华为全联接大会2019,共创智能新高度
    CTDC2019首席技术官领袖峰会,AI赋能 智享5G
    2019全球体验设计峰会:体验赋能商业,创造更好体验
    全球闪存峰会旨在深化技术创新,增进闪存产业链上下游
    PyCon 2019火热来袭,与大数据、人工智能等专家一起探讨Python语言
    PHPConChina 2019 PHP开发者大会将于8月在上海举办!
    2019腾讯Live开发者大会(TLC),引领技术新趋势
    2019 HTML5深度应用开发实践
    2019年5G物联网关键技术与应用培训,了解5G网络发展现状及进展
    2019第二届企业云服务大会 -- 企业智变,云化未来
  • 原文地址:https://www.cnblogs.com/sennly/p/4179454.html
Copyright © 2011-2022 走看看