admin 管理员组文章数量: 1086019
I'm upgrading dotnet RabbitMQ.Client from 6 to 7. 7 only has asynchronous functions.
The RabbitMQ.Client version 6 code. Same credentials. This works fine
private IModel connect(string vhost)
{
factory = new ConnectionFactory
{
HostName = bluePrint.HostName,
UserName = bluePrint.UserName,
Password = bluePrint.Password,
Port = bluePrint.Port,
VirtualHost = vhost
};
var connection = factory.CreateConnection();
return connection.CreateModel();
}
Then the async RabbitMQ.Client version 7.
private static async Task<IChannel> connect(string vhost)
{
try
{
var factory = new ConnectionFactory()
{
HostName = "k8srmq",
UserName = "user",
Password = ""
Port = 32002,
VirtualHost = vhost
};
/*---> killed here */
var connection = await factory.CreateConnectionAsync();
return await connection.CreateChannelAsync();
}
catch (Exception ex)
{
throw;
}
}
the process is just killed on the CreateConnectionAsync call. No exception is thrown the catch block is never reached. The process just gets killed.
How do I fix this?
I'm upgrading dotnet RabbitMQ.Client from 6 to 7. 7 only has asynchronous functions.
The RabbitMQ.Client version 6 code. Same credentials. This works fine
private IModel connect(string vhost)
{
factory = new ConnectionFactory
{
HostName = bluePrint.HostName,
UserName = bluePrint.UserName,
Password = bluePrint.Password,
Port = bluePrint.Port,
VirtualHost = vhost
};
var connection = factory.CreateConnection();
return connection.CreateModel();
}
Then the async RabbitMQ.Client version 7.
private static async Task<IChannel> connect(string vhost)
{
try
{
var factory = new ConnectionFactory()
{
HostName = "k8srmq",
UserName = "user",
Password = ""
Port = 32002,
VirtualHost = vhost
};
/*---> killed here */
var connection = await factory.CreateConnectionAsync();
return await connection.CreateChannelAsync();
}
catch (Exception ex)
{
throw;
}
}
the process is just killed on the CreateConnectionAsync call. No exception is thrown the catch block is never reached. The process just gets killed.
How do I fix this?
Share Improve this question edited Mar 27 at 13:54 Serve Laurijssen asked Mar 27 at 13:46 Serve LaurijssenServe Laurijssen 9,7936 gold badges53 silver badges105 bronze badges2 Answers
Reset to default 1Make sure the RabbitMQ server is up and ready to listen.
If the RabbitMQ server is in a docker container make sure the application container can communicate with the RabbitMQ container, also if you're using a docker compose file, keep in mind that RabbitMQ takes time to warm up even if you set the config "depends on" it'll still take time. if this is the case, try to run the RabbitMQ server separately make sure it is completely up and listening and see if the connection is established in the application.
I implemented this code which is slightly different than yours, I have a class RabbitMQConnection which implements a method of type IConnectionFactory? and returns the factory using the Uri.
It might be not very optimized but it works. you can base on this code and anize it as you want
public IConnectionFactory? Connection()
{
try
{
var rabbitUserName = Environment.GetEnvironmentVariable("RABBITMQ_USERNAME") ?? string.Empty;
var rabbitPassword = Environment.GetEnvironmentVariable("RABBITMQ_PASSWORD") ?? string.Empty;
if (string.IsNullOrEmpty(rabbitPassword))
throw new InvalidOperationException("Invalid configuration, rabbitMQ password missing or empty.");
var uri = $"amqp://{rabbitUserName}:{rabbitPassword}@{_rabbitMQconfiguration.HostName}:{_rabbitMQconfiguration.Port}";
Console.WriteLine(uri);
var factory = new ConnectionFactory
{
Uri = new Uri(uri)
};
return factory;
}
catch (Exception e)
{
Console.WriteLine($"RabbitMQ connection failed : {e}");
return null;
}
}
I call the factory wrapped in the Connection() method.
var connectionFactory = _rabbitMQConnectionFactory.Connection()
?? throw new InvalidOperationException("RabbitMQ connection factory is not initialized.");
using var connection = await connectionFactory.CreateConnectionAsync();
using var channel = await connection.CreateChannelAsync();
await channel.QueueDeclareAsync(queue: "somequeue", durable: false, exclusive: false, autoDelete: false,
arguments: null);
//Consume or publish goes here
It was not about the code itself but more an issue of where the code was called, namely in the GUI thread of an android application. (OnReceive of a BroadcastReceiver). Any await call would block the main thread.
Running the async code in Task.Run solved the problem.
public override void OnReceive(Context? context, Intent? intent)
{
Task.Run(async () =>
{
var channel = await connect("test");
await channel.BasicPublishAsync("fanout", "", decodedData);
});
}
private static async Task<IChannel> connect(string vhost)
{
var factory = new ConnectionFactory { .... };
var connection = await factory.CreateConnectionAsync();
return await connection.CreateChannelAsync();
}
本文标签: cRabbitMQClient version 7 killed on connectionStack Overflow
版权声明:本文标题:c# - RabbitMQ.Client version 7 killed on connection - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://roclinux.cn/p/1744084606a2530885.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论