ASP.NET CORE combined with RabbitMq message queue

Viewed 127

I am using asp.net core to create a project, I want to combine rabbitmq to realize the sending and receiving of message queue.I installed rabbitmq using docker and it runs successfully.But the queue I registered in the project is not displayed on the RabbitMq management interface.

ConnectionFactory factory = new ConnectionFactory
{
    UserName = "guest",
    Password = "guest",
    HostName = "*******"
};


var connection = factory.CreateConnection();

var channel = connection.CreateModel();

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

Console.WriteLine("\nRabbitMQ enter exit to exit!");

string input;
do
{
    input = Console.ReadLine();

    var sendBytes = Encoding.UTF8.GetBytes(input);
    
    channel.BasicPublish("", "test", null, sendBytes);

} while (input.Trim().ToLower()!="exit");
channel.Close();
connection.Close();

The above is my message production code.But there is no display on my RabbitMq dashboard.

enter image description here

1 Answers

You did not specify the port number to connect to and the VirtualHost to connect to, I am not sure if you are caused by this. Do you have a specific error? Or your project can run normally. I have written a demo for sending messages here, you can refer to it, it may be useful to you.

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

            try
            {
                var factory = new ConnectionFactory();
                factory.VirtualHost = "/";
                factory.HostName = "localhost";
                factory.Port = 5672;
                factory.UserName = "guest";//Default username guest
                factory.Password = "guest";//Default password guest
                using (var connection = factory.CreateConnection())
                using (var channel = connection.CreateModel())
                {
                    channel.QueueDeclare(queue: "hello",
                                         durable: false,
                                         exclusive: false,
                                         autoDelete: false,
                                         arguments: null);

                    string message = "Hello World!";
                    var body = Encoding.UTF8.GetBytes(message);

                    channel.BasicPublish(exchange: "",
                                         routingKey: "hello",
                                         basicProperties: null,
                                         body: body);
                    Console.WriteLine(" [x] Sent {0}", message);
                }

                Console.WriteLine(" Press [enter] to exit.");
                Console.ReadLine();
            }
            catch (Exception ex)
            {
                Console.Write(string.Format("RabbitMQ connection error :{0}\n", ex.ToString()));
            }
                
    }
        }

Refer to the official document example:

https://www.rabbitmq.com/tutorials/tutorial-one-dotnet.html

If you run this code and get this error:

enter image description here

Check if your corresponding virtual hosts match, I set "/" here, if you are not, please add your hosts here:

enter image description here

Result:

console:

enter image description here

rabbitmq: enter image description here

Related