rebus-org / Rebus.SimpleInjector

:bus: SimpleInjector container adapter for Rebus

Home Page:https://mookid.dk/category/rebus

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Rebus.SimpleInjector

install from nuget

Provides a SimpleInjector-based container adapter for Rebus.


To configure Rebus to work with your SimpleInjector container, simply call

container.RegisterRebus(
    configurer => configurer
        .(...)
);

where the (...) is the part usually omitted from the Rebus configuration examples.

A slightly more realistic example (using Serilog, RabbitMQ and SQL Server) could look like this:

var rabbitMqConnectionString = "amqp://rebususer:blablasecret@BIGRABBIT01.local";
var sqlServerConnectionString = "server=SQLMOTEL01.local; database=RebusStuff; trusted_connection=true";

container.RegisterRebus(
	configurer => configurer
		.Logging(l => l.Serilog())
		.Transport(t => t.UseRabbitMq(rabbitMqConnectionString, "simpleinjectortest"))
		.Sagas(s => s.StoreInSqlServer(sqlServerConnectionString, "Sagas", "SagaIndex"))
);

The examples shown so far will make the necessary container registrations, but the bus will not be started until either

  1. The container resolves the IBus instance, or
  2. You call the container.StartBus() extension method

so you should probably always remember to call container.StartBus() when your application starts (after it has finished making ALL of its container registrations).

If you would like to be able to resolve IBus WITHOUT starting consuming messages, you can set startAutomatically to false when configuring it like so:

container.RegisterRebus(
	configurer => configurer
		.Transport(...),

	startAutomatically: false
);

which will configure the bus to have 0 workers when it's resolved from the container. You must then call

container.StartBus();

to start it.

So why is it different from all the other container adapters?

Because SimpleInjector is very opinionated about its registration API and Rebus is pretty loose about it :)

How to register handlers?

Since Rebus' container adapters resolve ALL handlers that can handle an incoming message, handlers must be registered with the registration API for collections, e.g. like

container.Collection.Register<IHandleMessages<SomeMessage>>(typeof(SomeMessageHandler));

Due to limitations in SimpleInjector, you must be sure that all handlers for a given message type get registered with a single registration call similar to the one above.

There also exists a couple of extension methods that help you register handlers (up to three per message type) just the right way:

// one message handler
container.RegisterHandlers<SomeMessage, SomeMessageHandler>();

// two handlers of SomeMessage
container.RegisterHandlers<SomeMessage, SomeMessageHandler, AnotherMessageHandler>();

// three handlers of SomeMessage
container.RegisterHandlers<SomeMessage, SomeMessageHandler, AnotherMessageHandler, YetAnotherMessageHandler>();

The advantage of using Rebus' configuration extension is that it's easier to get right, because the extension will register it the right way in the container, and the API uses C# generics constraints to validate that the given handler types do in fact implement IHandleMessages<> closed with the right type.

About

:bus: SimpleInjector container adapter for Rebus

https://mookid.dk/category/rebus

License:Other


Languages

Language:C# 92.5%Language:Batchfile 7.5%