Skip to content

Replacing EasyNetQ Components

Wiebe Tijsma edited this page Jan 28, 2026 · 17 revisions

EasyNetQ is a library composed of small components. When you write:

EasyNetQ v8.x

In v8.x, EasyNetQ uses the standard Microsoft.Extensions.DependencyInjection abstractions. You can replace any component by registering your implementation with the service collection after calling AddEasyNetQ:

var serviceCollection = new ServiceCollection();

// First, register EasyNetQ services
serviceCollection.AddEasyNetQ("host=localhost");

// Then register your custom implementations
serviceCollection.AddSingleton<ISerializer, MyCustomSerializer>();
serviceCollection.AddSingleton<IConventions, MyCustomConventions>();

using var provider = serviceCollection.BuildServiceProvider();
var bus = provider.GetRequiredService<IBus>();

To see the complete list of components that make up the IBus instance, and how they are assembled, take a look at the ServiceCollectionExtensions class.

You can access services via the IServiceProvider:

var serializer = provider.GetRequiredService<ISerializer>();

Replacing components with custom implementations

Here's an example of replacing the default serializer with a custom implementation:

public class MyCustomSerializer : ISerializer
{
    public byte[] MessageToBytes<T>(T message) where T : class
    {
        // Custom serialization logic
    }

    public T BytesToMessage<T>(byte[] bytes) where T : class
    {
        // Custom deserialization logic
    }
}

// Register it
var serviceCollection = new ServiceCollection();
serviceCollection.AddEasyNetQ("host=localhost");
serviceCollection.AddSingleton<ISerializer, MyCustomSerializer>();

using var provider = serviceCollection.BuildServiceProvider();
var bus = provider.GetRequiredService<IBus>();

To use alternative DI containers that support Microsoft.Extensions.DependencyInjection abstractions, see Using Alternative DI Containers.


EasyNetQ v7.x

When you write:

var bus = RabbitHutch.CreateBus("host=localhost");

... the static method CreateBus assembles these components using a tiny internal IoC container. An overload of the CreateBus method allows you to access the component registration so that you can provide your own versions of any of the EasyNetQ dependencies. The signature looks like this:

public static IBus CreateBus(string connectionString, Action<IServiceRegister> registerServices)

The IServiceRegister interface provides a single method:

public interface IServiceRegister
{
    IServiceRegister Register<TService>(Func<IServiceProvider, TService> serviceCreator) where TService : class;
}

So to register your own logger, based on IEasyNetQLogger, you'd write this code:

IEasyNetQLogger logger = new MyLogger(); // note the use of IEasyNetQLogger not var.

var bus = RabbitHutch.CreateBus(connectionString, 
    serviceRegister => serviceRegister.Register(serviceProvider => logger));

The Register method's argument, Func<IServiceProvider, TService>, is a function that's run when CreateBus pulls together the components to make an IBus instance. IServiceProvider looks like this:

public interface IServiceProvider
{
    TService Resolve<TService>() where TService : class;
}

This allows you to access other services that EasyNetQ provides. If for example you wanted to replace the default serializer with your own implementation of ISerializer, and you wanted to construct it with a reference to the logger, you could do this:

var bus = RabbitHutch.CreateBus(connectionString, serviceRegister => 
    serviceRegister.Register<ISerializer>(
    serviceProvider => new MySerializer(serviceProvider.Resolve<IEasyNetQLogger>())));

Note that we have to use an explicit type parameter on the Register method so that the internal IoC container knows which service we are replacing.

To see the complete list of components that make up the IBus instance, and how they are assembled, take a look at the ComponentRegistration class.

You can access the container via the IAdvancedBus's Container property. This allows you to access internal components:

var serializer = bus.Advanced.Container.Resolve<ISerializer>();

To replace the internal container with your own choice of IoC container, see Using Alternative DI Containers

Clone this wiki locally