Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/230.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将消息从一个队列发送到其他队列_C#_Msmq - Fatal编程技术网

C# 将消息从一个队列发送到其他队列

C# 将消息从一个队列发送到其他队列,c#,msmq,C#,Msmq,我正在使用下面的代码将所有消息从一个队列中获取到一个数组中,并发送到数组中的其他队列,所发生的事情是它将每个消息发送到每个队列两次,我不明白为什么,有人能看到任何明显的情况吗 谢谢 public void SendToQs() { Code.Class1 c = new Code.Class1(); oInQueue = new MessageQueue(sInQueue); Message[] msgs = oInQueue.GetAllMessages();

我正在使用下面的代码将所有消息从一个队列中获取到一个数组中,并发送到数组中的其他队列,所发生的事情是它将每个消息发送到每个队列两次,我不明白为什么,有人能看到任何明显的情况吗

谢谢

public void SendToQs()
{
    Code.Class1 c = new Code.Class1();
    oInQueue = new MessageQueue(sInQueue);
    Message[] msgs = oInQueue.GetAllMessages();

    var queueArray = sOutQueues.Select(s => new MessageQueue(s)).ToArray();
    foreach (Message msg in msgs)
    {
        foreach (MessageQueue s in queueArray)
        {
            c.WriteMessage(s, msg, msg.Label);
        }

    }
    oInQueue.Purge();
}
书面信息:

public void WriteMessage(MessageQueue outputQueue, Message msg, string label)
{
    if (!outputQueue.Transactional)
    {
        try
        {
            outputQueue.Send(msg, label);
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
    }
    else
    {
        MessageQueueTransaction trans = new MessageQueueTransaction();
        try
        {
            trans.Begin();
            outputQueue.Send(msg, label, trans);
            trans.Commit();
        }
        catch (Exception ex)
        {
            Console.WriteLine("message Q exception" + ex.Message);
            trans.Abort();
        }
    }
}

我没有时间去测试这个,但是你可能想考虑一下。

如果一个队列的所有消息都被发送到其他队列,那么在遍历队列列表时,原始队列也会被发送此消息

foreach (Message msg in msgs)
{
   foreach (MessageQueue s in queueArray)
   {
      if (s.Id == oInQueue.Id) continue; // Skip if this is the originator
      c.WriteMessage(s, msg, msg.Label);
   }
}

我没有时间去测试这个,但是你可能想考虑一下。

如果一个队列的所有消息都被发送到其他队列,那么在遍历队列列表时,原始队列也会被发送此消息

foreach (Message msg in msgs)
{
   foreach (MessageQueue s in queueArray)
   {
      if (s.Id == oInQueue.Id) continue; // Skip if this is the originator
      c.WriteMessage(s, msg, msg.Label);
   }
}

明白了,这真是我所期待的愚蠢! 在我的void Main()中,我最初启动了一个流程,只是为了确保它能正常工作。 然后,我添加了一行代码,用这个进程启动一个新线程,但忘记了取出原来的线程,所以它运行了两次。

明白了,这真是我所期待的愚蠢! 在我的void Main()中,我最初启动了一个流程,只是为了确保它能正常工作。 然后,我添加了一行代码,用这个进程启动一个新线程,但忘记了取出原来的线程,所以它运行了两次。

所以WriteMessage被调用了X次,但发送了2X条消息?确切地说,它一直停留在一些控制台中。writelines,而且似乎是在调用时发生的。发了这封信,以防我盯着它看得太久了,有什么明显的错误,我看不出来……这个问题对你有帮助吗?所以WriteMessage被调用了X次,但发送了2X条消息?确切地说,它一直停留在一些控制台中。writelines,而且似乎是在调用时发生的。发了这封信,以防我盯着它看得太久了,有什么明显的错误,我看不出来……这个问题对你有帮助吗?