RabbitMQ无法从同一进程发送和接收

时间:2016-11-25 19:33:54

标签: c# rabbitmq

我正在使用RabbitMQ完成我的第一步,并询问为什么这不起作用。

基本教程(https://www.rabbitmq.com/tutorials/tutorial-one-dotnet.html)有一个可执行文件可以发送给代理,另一个可执行文件可以接收它。

我通过Visual Studio中的单个控制台应用程序运行此代码,但无法接收任何消息。

如果我接受“接收”代码并将其放入单独的控制台应用程序并打开它,我会收到消息(没有其他代码更改)。

有人可以解释为什么我不能在同一个过程中同时使用这两个过程吗?我认为连接工厂将相应地处理独立连接,无论它是否是相同的进程。

为了完整起见(虽然我怀疑它是必需的),这里的代码在我拿出“Receiver”代码并将其放入自己的控制台应用程序之前无效:

class Program
{
    static void Main(string[] args) {

        Receiver.Receive();
        Console.WriteLine("receiver set up");

        System.Threading.Thread.Sleep(5000);

        Console.WriteLine("sending...");
        Test.Send();

        // can also reverse order of send/receive methods, same result

        Console.ReadKey();

    }
}


public class Receiver
{
    public static void Receive() {

        var factory = new ConnectionFactory() { HostName = "localhost" };

        using (var connection = factory.CreateConnection()) {

            using (var channel = connection.CreateModel()) {

                channel.QueueDeclare("hello", false, false, false, null);

                var consumer = new EventingBasicConsumer(channel);
                consumer.Received += (model, ea) => {

                    var body = ea.Body;
                    var message = Encoding.UTF8.GetString(body);
                    System.Diagnostics.Debug.WriteLine("=====================");
                    System.Diagnostics.Debug.WriteLine(message);
                    System.Diagnostics.Debug.WriteLine("=====================");

                };

                channel.BasicConsume("hello", true, consumer);

            }

        }

    }

}

public class Test
{

    public static void Send() {

        var factory = new ConnectionFactory() { HostName = "localhost" };

        using (var connection = factory.CreateConnection()) {

            using (var channel = connection.CreateModel()) {

                channel.QueueDeclare("hello", false, false, false, null);

                string message = "Check it!";

                var body = Encoding.UTF8.GetBytes(message);

                channel.BasicPublish("", "hello", null, body);

            }
        }

    }

}

1 个答案:

答案 0 :(得分:1)

我认为代码存在问题。当您发送消息时,接收连接和通道已经死亡(已调用Dispose)。

RabbitMQ网站的标准代码:

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

        var consumer = new EventingBasicConsumer(channel);
        consumer.Received += (model, ea) =>
        {
            var body = ea.Body;
            var message = Encoding.UTF8.GetString(body);
            Console.WriteLine(" [x] Received {0}", message);
        };
        channel.BasicConsume(queue: "hello",
                             noAck: true,
                             consumer: consumer);

        Console.WriteLine(" Press [enter] to exit.");
        Console.ReadLine();
    }

connectionchannel在客户收到消息时尚未生效。