I am preparing a simple ASP.NET Core MVC web application.
I have installed RabbitMQ server to my laptop. RabbitMQ Management UI is running on localhost:15672.
Rabbit MQ cluster name is like: rabbit@CR00001.ABC.COM.LOCAL
I am trying to send message to rabbitmq in controller. But I am getting None of the specified endpoints were reachable error.
If I use 'localhost' as host name, I get Connection refused 127.0.0.1:5672 in inner exceptions.
If I use rabbit as host name, I get Name or service not known
I've tried to solve the problem according to other StackOverflow questions, however, none of them could solved my problem.
Home controller:
[HttpPost]
public void SendMessage([FromBody]Message message)
{
try
{
var factory = new ConnectionFactory()
{
UserName = _username,
Password = _password,
HostName = _hostname,
VirtualHost = "/",
Port = _port,
};
using (var connection = factory.CreateConnection())
using (var channel = connection.CreateModel())
{
channel.QueueDeclare(queue: _queueName,
durable: false,
exclusive: false,
autoDelete: false,
arguments: null);
var body = Encoding.UTF8.GetBytes(message.Text);
channel.BasicPublish(exchange: "",
routingKey: _queueName,
basicProperties: null,
body: body);
}
}
catch (Exception ex)
{
}
}
appsettings.json
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*",
"RabbitMq": {
"Hostname": "localhost",
"QueueName": "WordQueue",
"UserName": "test",
"Password": "test",
"Port": 5672
}
}

