Sandy
Sandy

Reputation: 275

Connecting RabbitMQ Using C#

I am using the sample code available at internet but I am getting an exception and I am not able to solve this error.

I am getting this exception

BrokerUnreachableExceptionCaught None of the specified endpoints were reachable

No idea how to resolve this error. There are so many links that have posted the encounter of error but none of them have it's resolution. Please help me regarding this. Your suggestions will be helpful to me.. Please help as soon as possible.

Some links

Code:

try
{
    ConnectionFactory factory = new ConnectionFactory();
    factory.UserName = "user";
    factory.Password = "password";
    factory.VirtualHost = "/";
    factory.Protocol = Protocols.FromEnvironment();
    factory.HostName = "localhost";
    factory.Port = AmqpTcpEndpoint.UseDefaultPort;
    IConnection conn = factory.CreateConnection();

    //using (var connection = factory.CreateConnection())
    //{
    //    using (var channel = connection.CreateModel())
    //    {
    //        channel.QueueDeclare("hello", false, false, false, null);
    //        string message = "Hello World!";
    //        var body = Encoding.UTF8.GetBytes(message);

    //        channel.BasicPublish("", "hello", null, body);
    //        Console.WriteLine(" [x] Sent {0}", message);
    //    }
    //}
}
catch
{
}

Upvotes: 2

Views: 15118

Answers (2)

GirishBabuC
GirishBabuC

Reputation: 1379

RabbitMQ localhost connection in Asp.net core, in Nugget Package, browse RabbitMQ.Client

//localhost connection, these both worked for me.

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

or

var factory = new ConnectionFactory();

using (var connection = factory.CreateConnection())
        {
            using (var channel = connection.CreateModel())
            {
                channel.QueueDeclare(queue: "HelloNewWorld",
                             durable: false,
                             exclusive: false,
                             autoDelete: false,
                             arguments: null);
                             
                             string message = "Hello World!";
        var body = Encoding.UTF8.GetBytes(message);

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

//default localhost for rabbitmq
http://localhost:15672/queues

enter image description here

Asp.NetCore #RabbitMQ

Upvotes: 1

Daniel James Bryars
Daniel James Bryars

Reputation: 4621

The BrokerUnreachableException thrown has the following useful properties:

ConnectionAttempts ConnectionErrors

Take a look at these to see if there's any extra information (for example, perhaps the password is incorrect.)

Upvotes: 3

Related Questions