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 badges
Add a comment  | 

2 Answers 2

Reset to default 1

Make 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