-
Notifications
You must be signed in to change notification settings - Fork 748
Using Alternative DI Containers
From v8 and higher: EasyNetQ uses the default Microsoft abstractions for dependency resolution, so any container implementing those abstractions should work.
var serviceCollection = new ServiceCollection();
serviceCollection.AddEasyNetQ($"host={fixture.Host});
serviceProvider = serviceCollection.BuildServiceProvider();
bus = serviceProvider.GetRequiredService<IBus>();EasyNetQ is made up of a collection of independent components. Internally it uses LightInject container, which is wrapped by DefaultServiceContainer. If you look at the code for the static RabbitHutch class that you use to create instances of the core IBus interface, you will see that it simply creates a new DefaultServiceContainer, registers all of EasyNetQ’s components, and then calls container.Resolve() creating a new instance of IBus with its tree of dependencies supplied by the container:
public static IBus CreateBus(Func<IServiceResolver, ConnectionConfiguration> connectionConfigurationFactory, Action<IServiceRegister> registerServices)
{
var container = new DefaultServiceContainer();
RegisterBus(container, connectionConfigurationFactory, registerServices);
return container.Resolve<IBus>();
}But what if you want EasyNetQ to use your container of choice? From version 3 the RabbitHutch class provides a static method, RegisterBus, that allows you to register EasyNetQ components in IServiceRegister, which you can implement for any container you want. There are several already implemented adapters for Castle Windsor, Autofac, LightInject, NInject, StructureMap, SimpleInjector.
In this example we are using Autofac and EasyNetQ.DI.Autofac package. It provides RegisterEasyNetQ extension method on top of ContainerBuilder.
var containerBuilder = new ContainerBuilder();
containerBuilder.RegisterEasyNetQ("host=localhost", c => {/* override services here */});
var container = containerBuilder.Build();After calling RegisterEasyNetQ all components will be registered in ContainerBuilder and after building container you will be able to resolve IBus by calling container.Resolve<IBus>().
- Quick Start
- Introduction
- A Note on Versioning
- Installing EasyNetQ
- EasyNetQ v8 Migration Guide
- 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