Lev
Lev

Reputation: 434

MassTransit , Autofac and 2 bus instances

I have a situation with a publisher and consumer sitting in the same application. I'm using autofac.

As I understand I need 2 Bus instances with 2 endpoints, one for publisher, one for subscriber.

I'm using autofac but I don't know how to create 2 bus instances, each with it's own subscribing classes (which should be resolved by autofac). In JEE/CDI I would use qualifiers, but as far as I see autofac has nothing like that (and named services aren't autowired).

So basically I have 2 problems:

Any hints how this can be done?

Upvotes: 2

Views: 1413

Answers (2)

Chris Patterson
Chris Patterson

Reputation: 33298

If you take a look at the RapidTransit project, it has libraries for getting off the ground with building services using MassTransit and Autofac. You can also install the packages from NuGet for either Windows services or Web applications.

https://github.com/MassTransit/RapidTransit

You can also look at Riktig, which is using RapidTransit.

https://github.com/phatboyg/riktig/

You can see how multiple bus instances are created in the same process, using nested lifetime scopes in Autofac.

https://github.com/phatboyg/Riktig/blob/master/src/Riktig.CoordinationService/ImageRetrievalStateBusInstance.cs

And how it's all wired together using the Bootstrappers:

https://github.com/phatboyg/Riktig/blob/master/src/Riktig.CoordinationService/CoordinationServiceBootstrapper.cs

Key is the use of:

        builder.RegisterType<ImageRetrievalStateBusInstance>()
               .As<IServiceBusInstance>();

This is one instance, and multiple can be registered within an instance host to give you multiple services.

Upvotes: 2

Travis
Travis

Reputation: 10547

It's using named instances...

builder.Register(c => new FooImpl())
    .As<IFoo>()
    .Named("Foo1");

Then

container.ResolveNamed<IFoo>("Foo1");

If my Autofac syntax is correct off the top of my head. This should at least get you down the right path. Just request an instance of each IServiceBus, so that they get resolved and created from your container.

Oh, and you can't use LoadFrom to register the consumers. You'll have to resolve and register each one by hand. Since calling LoadFrom will register all the consumers in your container.

You could create two subcontainers, one for each IServiceBus but now we're well outside of the realm of what I know how to do off the top of my head with Autofac. You can do it with almost ever other container, so I assume you can with Autofac though.

Upvotes: 3

Related Questions