Skip to content

Create Publisher

This page shows how to create a RabbitMQ publisher manually.

The concrete client is:

PublisherBound

It requires:

  • PublisherBoundDescriptor
  • IDataEncoder
  • RabbitMQ channel

Create Connection And Channel

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

using var connection =
    factory.CreateConnection("publisher-demo");

using var channel =
    connection.CreateChannel();

For the RabbitMQ.Client v7 provider, CreateChannel() supports an optional delivery confirmation parameter used to enable publisher confirms during channel creation.


Create Formatter

This guide assumes that an IDataFormatter instance has already been created.

See:

  • Getting Started → Create Formatter
IDataFormatter formatter = ...;

Create Descriptor

var descriptor = new PublisherBoundDescriptor
{
    EnableChannelEvents = true,

    Exchanges =
    {
        new ExchangeBoundDescriptor
        {
            Name = "orders",
            Type = ExchangeTypes.Direct,
            Durable = true,
            AutoDelete = false,

            Queues =
            {
                new QueueBoundDescriptor
                {
                    Name = "orders.created",
                    Durable = true,
                    AutoDelete = false,

                    Binder = new BinderDescriptor
                    {
                        RoutingKey = "orders.created"
                    }
                }
            }
        }
    }
};

Create Publisher

await using var publisher =
    new PublisherBound(
        descriptor,
        formatter,
        channel);

Publish A Message

var message =
    new Message<OrderCreated>
    {
        Created = DateTime.UtcNow,
        Route = "orders.created",

        Data = new OrderCreated
        {
            OrderId = Guid.NewGuid(),
            CreatedAtUtc = DateTime.UtcNow
        }
    };

await publisher.SendAsync(message);

Complete Example

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

using var connection =
    factory.CreateConnection("publisher-demo");

using var channel =
    connection.CreateChannel();

IDataFormatter formatter = ...;

var descriptor = new PublisherBoundDescriptor
{
    EnableChannelEvents = true,

    Exchanges =
    {
        new ExchangeBoundDescriptor
        {
            Name = "orders",
            Type = ExchangeTypes.Direct,
            Durable = true,

            Queues =
            {
                new QueueBoundDescriptor
                {
                    Name = "orders.created",
                    Durable = true,

                    Binder = new BinderDescriptor
                    {
                        RoutingKey = "orders.created"
                    }
                }
            }
        }
    }
};

await using var publisher =
    new PublisherBound(
        descriptor,
        formatter,
        channel);

await publisher.SendAsync(
    new Message<OrderCreated>
    {
        Created = DateTime.UtcNow,
        Route = "orders.created",

        Data = new OrderCreated
        {
            OrderId = Guid.NewGuid(),
            CreatedAtUtc = DateTime.UtcNow
        }
    });

Message Contract

public sealed class OrderCreated
{
    public Guid OrderId { get; init; }

    public DateTime CreatedAtUtc { get; init; }
}

Next Step

Continue with:

  • Create Subscriber, to consume messages from the queue.
  • Publishing, for advanced publishing scenarios.
  • Reference → PublisherBoundDescriptor, for a complete description of the publisher descriptor.