Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/339.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# 与Console.ReadLine()相关的RabbitMQ BasicConsume和事件驱动问题_C#_Events_Rabbitmq_Listener_Console.readline - Fatal编程技术网

C# 与Console.ReadLine()相关的RabbitMQ BasicConsume和事件驱动问题

C# 与Console.ReadLine()相关的RabbitMQ BasicConsume和事件驱动问题,c#,events,rabbitmq,listener,console.readline,C#,Events,Rabbitmq,Listener,Console.readline,下面的程序基本上是C#Rabbit MQ教程中的Receiver/Worker程序:(添加了一个计数器) 有两三件事让我感到困惑: 1) 如果我注释掉“Console.ReadLine()”,它将使用队列中的消息并显示: 在最初的几次测试中,我不知道发生了什么 2) 这一行永远不会出现在输出中:Console.WriteLine(“按[enter]退出”);。大概是因为它在“Console.ReadLine();”之前,但是为什么呢?ReadLine事件和BasicConsumer之间的相互作用

下面的程序基本上是C#Rabbit MQ教程中的Receiver/Worker程序:(添加了一个计数器)

有两三件事让我感到困惑:

1) 如果我注释掉“Console.ReadLine()”,它将使用队列中的消息并显示:

在最初的几次测试中,我不知道发生了什么

2) 这一行永远不会出现在输出中:Console.WriteLine(“按[enter]退出”);。大概是因为它在“Console.ReadLine();”之前,但是为什么呢?ReadLine事件和BasicConsumer之间的相互作用是什么

3) MQ教程页面说使用CNTL-C停止“侦听器”进程,但我发现只需按enter键也同样有效

我以前为MQSeries编写过线程侦听器,我可能更喜欢线程,但我只是想了解提供的基本教程

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using RabbitMQ.Client;
using RabbitMQ.Client.Events;

namespace RabbitMQReceiver
{
    class Receive
    {
        public static void Main(string[] args)
        {
            var factory = new ConnectionFactory() { HostName = "localhost" };
            var myQueuename = "MyQueueName1";
            Console.WriteLine("My Start");


            using (var connection = factory.CreateConnection())
            using (var channel = connection.CreateModel())
            {
                channel.QueueDeclare(queue: myQueuename,
                                     durable: false,
                                     exclusive: false,
                                     autoDelete: false,
                                     arguments: null);

                var consumer = new EventingBasicConsumer(channel);
                int countMessagesProcessed = 0;

                // this chunk of code is passed as parm/variable to BasicConsume Method below to process each item pulled of the Queue 
                consumer.Received += (model, ea) =>
                {
                    var body = ea.Body;
                    var message = Encoding.UTF8.GetString(body);
                    countMessagesProcessed++;
                    Console.WriteLine(" [x] Received {0}", message);
                }

                channel.BasicConsume(queue: myQueuename,
                                     noAck: true,
                                     consumer: consumer);

                Console.WriteLine(" Press [enter] to exit.");  // this line never shows up in output 
                Console.ReadLine();    // if this line is commented out the message are consumed, but no Console.WriteLines appear at all. 
                Console.WriteLine("My End - CountMessagesProcessed=" + countMessagesProcessed);

            }
        }
    }
}
Console.ReadLine()
在等待输入时暂停程序的执行,这允许RabbitMQ使用的线程在此期间运行。注释掉后,程序执行一直运行到结束并退出,包括RabbitMQ线程


是的,您可以键入任何内容,它将停止程序的执行;一旦按下键,程序将继续执行并运行到最后。

使用Thread.Sleep(Timeout.Infinite)而不是Console.ReadLine(),这样主程序(或主线程)就不会立即退出。在Linux上运行netcore rabbitmq对我来说也很有效

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using RabbitMQ.Client;
using RabbitMQ.Client.Events;

namespace RabbitMQReceiver
{
    class Receive
    {
        public static void Main(string[] args)
        {
            var factory = new ConnectionFactory() { HostName = "localhost" };
            var myQueuename = "MyQueueName1";
            Console.WriteLine("My Start");


            using (var connection = factory.CreateConnection())
            using (var channel = connection.CreateModel())
            {
                channel.QueueDeclare(queue: myQueuename,
                                     durable: false,
                                     exclusive: false,
                                     autoDelete: false,
                                     arguments: null);

                var consumer = new EventingBasicConsumer(channel);
                int countMessagesProcessed = 0;

                // this chunk of code is passed as parm/variable to BasicConsume Method below to process each item pulled of the Queue 
                consumer.Received += (model, ea) =>
                {
                    var body = ea.Body;
                    var message = Encoding.UTF8.GetString(body);
                    countMessagesProcessed++;
                    Console.WriteLine(" [x] Received {0}", message);
                }

                channel.BasicConsume(queue: myQueuename,
                                     noAck: true,
                                     consumer: consumer);

                Console.WriteLine(" Press [enter] to exit.");  // this line never shows up in output 
                Console.ReadLine();    // if this line is commented out the message are consumed, but no Console.WriteLines appear at all. 
                Console.WriteLine("My End - CountMessagesProcessed=" + countMessagesProcessed);

            }
        }
    }
}