jdl134679 jdl134679 - 9 days ago 5
C# Question

RabbitMQ Can't Send and Receive from same process

I'm working through my first steps with RabbitMQ and have a question on why this didn't work.

The basic tutorial (https://www.rabbitmq.com/tutorials/tutorial-one-dotnet.html) has one executable to send to the broker and another to receive it.

I ran this code through a single console app in Visual Studio and couldn't receive any messages.

If I take the "Receive" code and put it into a separate console app and open that, I get the message (no other code changes).

Can someone explain why I'm not able to have both in the same process? I had figured that the connection factory would handle independent connections accordingly regardless of whether it was the same process or not.




For the sake of completeness (though I doubt it's required), here's the code that didn't work until I pulled out the "Receiver" code and put it into it's own console app:

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);

}
}

}

}

Answer

I think code has a problem. When you send message then Receive connection and channel already dead (Dispose called).

Standard code from RabbitMQ site:

    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();
    }

connection and channel are yet alive when client received message.

Comments