Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Autofac Dependency Injection in MassTransit IConsume

I'm trying to use DI with my consumer class without success.

My consumer class:

public class TakeMeasureConsumer : IConsumer<TakeMeasure>
{

    private IUnitOfWorkAsync _uow;
    private IInstrumentOutputDomainService _instrumentOutputDomainService;


    public TakeMeasureConsumer(IUnitOfWorkAsync uow,
        IInstrumentOutputDomainService instrumentOutputDomainService)
    {
        _uow = uow;
        _instrumentOutputDomainService = instrumentOutputDomainService;
    }


    public async Task Consume(ConsumeContext<TakeMeasure> context)
    {

        var instrumentOutput = Mapper.Map<InstrumentOutput>(context.Message);

        _instrumentOutputDomainService.Insert(instrumentOutput);
        await _uow.SaveChangesAsync();

    }
}

When I want to register the bus factory the consumer must have a parameterless constructor.

protected override void Load(ContainerBuilder builder)
{

    builder.Register(context =>
        Bus.Factory.CreateUsingRabbitMq(cfg =>
        {
            var host = cfg.Host(new Uri("rabbitmq://localhost/"), h =>
            {
                h.Username("guest");
                h.Password("guest");
            });

            cfg.ReceiveEndpoint(host, "intrument_take_measure", e =>
            {
                // Must be a non abastract type with a parameterless constructor....
                e.Consumer<TakeMeasureConsumer>();

            });  

        }))
    .SingleInstance()
    .As<IBusControl>()
    .As<IBus>();
}

Any help would be appreciated, I really don't know how to register my consumer...

Thanks

like image 459
JoyC Avatar asked Oct 04 '15 04:10

JoyC


People also ask

What is Autofac dependency injection?

Autofac is an open-source dependency injection (DI) or inversion of control (IoC) container developed on Google Code. Autofac differs from many related technologies in that it sticks as close to bare-metal C# programming as possible.

What is the use of Autofac in MVC?

AutoFac provides better integration for the ASP.NET MVC framework and is developed using Google code. AutoFac manages the dependencies of classes so that the application may be easy to change when it is scaled up in size and complexity.

What is Autofac MassTransit?

Autofac is a powerful and fast container, and is well supported by MassTransit. Nested lifetime scopes are used extensively to encapsulate dependencies and ensure clean object lifetime management. The following examples show the various ways that MassTransit can be configured, including the appropriate interfaces necessary.

What is Autofac dependency injection?

Using the Autofac dependency injection container to simplify the configuration of object-oriented applications. Where to Next? Autofac is an open-source dependency injection (DI) or inversion of control (IoC) container developed on Google Code.

Does MassTransit support Microsoft dependency injection?

MassTransit fully supports the Microsoft Dependency Injection framework used by ASP.NET Core. In fact, most of the container-based examples in the documentation use the Microsoft container as it is the most popular. The container section includes examples and usage details, refer to that section for more details.

How to implement the IoC container using Autofac?

First of all create one Console application to implement the sample example and provide a reference of Autofac from the NuGet Package Manager. Here is the package. Once you provide a reference, you will find the following reference in the references section of the application. We are now ready to implement the IoC container using autofac.


1 Answers

Integrating with Autofac is easy, there are extension methods in the MassTransit.Autofac package that help out.

First, there is an AutofacConsumerFactory that will resolve your consumers from the container. You can add this to the container, or you can register it yourself using:

builder.RegisterGeneric(typeof(AutofacConsumerFactory<>))
    .WithParameter(new NamedParameter("name", "message"))
    .As(typeof(IConsumerFactory<>));

Then, in the builder statement for the bus and receive endpoint:

e.Consumer(() => context.Resolve<IConsumerFactory<TakeMeasureConsumer>());

This will then resolve your consumers from the container.

Update:

For newer versions of MassTransit add receive endpoints like this:

e.Consumer<TakeMeasureConsumer>(context);
like image 54
Chris Patterson Avatar answered Oct 13 '22 05:10

Chris Patterson