Skip to content

Configure RabbitMQ Connection

RabbitMQ connectivity starts with a connection factory.

The connection is typically shared across multiple publishers and subscribers.


RabbitMQ.Client v6

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

using var connection =
    factory.CreateConnection();

RabbitMQ.Client v7

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

await using var connection =
    await factory.CreateConnectionAsync();

Recommended Lifetime

graph LR

    Application

    Connection

    Channel

    Application --> Connection

    Connection --> Channel

Recommended:

  • one connection per application;
  • multiple channels per connection.

Avoid creating connections per message.


Production Considerations

Typical production settings include:

  • TLS encryption;
  • heartbeat configuration;
  • automatic recovery;
  • connection monitoring.

These settings are transport-specific and should follow RabbitMQ best practices.