简体   繁体   English

2个本地应用之间的C#MSMQ未接收到所有已发送的消息

[英]c# MSMQ between 2 local apps not receiving all messages sent

I have created my own MSMQ wrapper class like this: 我创建了自己的MSMQ包装器类,如下所示:

public class MsgQueue
{
    private MessageQueue messageQueue;

    public delegate void ReadMessageDelegate(string message);
    public event ReadMessageDelegate NewMessageAvailable;

    public MsgQueue(string queueName)
    {
        var queuePath = @".\Private$\" + queueName;

        if (!MessageQueue.Exists(queuePath)) MessageQueue.Create(queuePath);

        messageQueue = new MessageQueue(queuePath);
        messageQueue.Label = queueName;
        messageQueue.Formatter = new XmlMessageFormatter(new Type[] { typeof(string)});

        messageQueue.ReceiveCompleted += new ReceiveCompletedEventHandler(MsgReceivedHandler);
        messageQueue.BeginReceive();
    }

    private void MsgReceivedHandler(object sender, ReceiveCompletedEventArgs e)
    {
        try
        {
            MessageQueue mq = (MessageQueue)sender;
            var message = mq.EndReceive(e.AsyncResult);

            NewMessageAvailable(message.Body.ToString());

            mq.BeginReceive();
        }
        catch (MessageQueueException)
        {
            // Handle sources of MessageQueueException.
        }

        return;
    }

    public void SendMessage(string message)
    {
        messageQueue.Send(message);
    }
}

I tested it on two separate WinForms applications. 我在两个独立的WinForms应用程序上进行了测试。

First app sends a text message when button is clicked: 单击按钮时,第一个应用程序发送文本消息:

private void btn_Click(object sender, EventArgs e)
{          
   var queue = new MsgQueue.MsgQueue("GBMqueue");
   queue.SendMessage("some message text");
}

Second app is listening for any incoming messages and then tries to process it: 第二个应用正在侦听所有传入消息,然后尝试对其进行处理:

// declaration
private MsgQueue queue;

// preparation of the queue
private void Form1_Load(object sender, EventArgs e)
{
    queue = new MsgQueue.MsgQueue("GBMqueue");
    queue.NewMessageAvailable += Queue_NewMessageAvailable;
}

// Handler for the incoming messages
private void Queue_NewMessageAvailable(string message)
{
     // Hits here very rarely!!!
}

The problem is that I can send the message from App1 several times, but the Queue_NewMessageAvailable handler catches only one random message, not the first one - just one of those which were sent. 问题是我可以多次从App1发送消息,但是Queue_NewMessageAvailable处理程序仅捕获一条随机消息,而不是第一条-仅发送了一条消息。

No exception is thrown, it just does not catch the incoming messages. 不会引发任何异常,只是不会捕获传入的消息。

What am I doing wrong here? 我在这里做错了什么?

I think the first App should not listen for new messages. 我认为第一个应用程序不应监听新消息。 It's possible that it takes away the messages for the second App. 它可能带走了第二个App的消息。 It should only send messages. 它应该只发送消息。

When you split the functionality it should work. 拆分功能时,它应该可以工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM