-
Notifications
You must be signed in to change notification settings - Fork 749
Replacing EasyNetQ Components
EasyNetQ is a library composed of small components. 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
- Quick Start
- Introduction
- A Note on Versioning
- Installing EasyNetQ
- Connecting to RabbitMQ
- Connecting with SSL
- Logging
- Logging v8.x
- Publish
- Subscribe
- Request Response
- Send Receive
- Topic Based Routing
- Controlling Queue names
- Polymorphic Publish and Subscribe
- Versioning Messages
- Publisher Confirms
- Scheduling Events with Future Publish
- Support for Delayed Messages Plugin
- Auto Subscriber
- Auto Subscriber v8.x
- Error Conditions
- Re Submitting Error Messages With EasyNetQ.Hosepipe
- The Advanced API
- Cluster Support
- Wiring up EasyNetQ with TopShelf and Windsor
- Replacing EasyNetQ Components
- Using Alternative DI Containers
- Enable Legacy Conventions