Using Dependency Injection in Consumer

2,218 views
Skip to first unread message

alexandre joyeux

unread,
Oct 3, 2015, 12:42:23 PM10/3/15
to masstransit-discuss

 Hi,


I'm new to masstransit and I'm trying to injrct dependencies in my message consumer.


I'm not sure how to register my consumer using autofac.

Any help would be highly appreciated :-)


Thanks, 

Alex


public class InstrumentCommandConsumer : IConsumer<TakeMeasure>

    {


        private IUnitOfWorkAsync _uow;

        private IInstrumentOutputDomainService _instrumentOutputDomainService;



        public InstrumentCommandHandler(IUnitOfWorkAsync uow,

            IInstrumentOutputDomainService instrumentOutputDomainService)

        {

            _uow = uow;

            _instrumentOutputDomainService = instrumentOutputDomainService;

        }

Chris Patterson

unread,
Oct 3, 2015, 1:04:31 PM10/3/15
to masstrans...@googlegroups.com
Usage of Autofac (and several other containers) is well documented in the documentation.



--
You received this message because you are subscribed to the Google Groups "masstransit-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to masstransit-dis...@googlegroups.com.
To post to this group, send email to masstrans...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/masstransit-discuss/a688cf83-8549-424c-b32a-a8bd1e2e526f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

alexandre joyeux

unread,
Oct 3, 2015, 3:32:55 PM10/3/15
to masstrans...@googlegroups.com

Hi Chris,

As you said the doc for autofac is well documented, but I just don't understand how to register my consumer. It says that it needs a parameterless constructor to be registered.

I must have missed smth...


You received this message because you are subscribed to a topic in the Google Groups "masstransit-discuss" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/masstransit-discuss/-L0QIsJXfq4/unsubscribe.
To unsubscribe from this group and all its topics, send an email to masstransit-dis...@googlegroups.com.

To post to this group, send email to masstrans...@googlegroups.com.

mald

unread,
Oct 3, 2015, 4:52:04 PM10/3/15
to masstransit-discuss
This is the perfect opportunity for me to shamelessly self promote my post with explanation and sample code on github. I used Autofac as my DI container, however the project is structured so you can substitute your favorite DI in. I hope it helps!

On Saturday, 3 October 2015 15:32:55 UTC-4, alexandre joyeux wrote:

Hi Chris,

As you said the doc for autofac is well documented, but I just don't understand how to register my consumer. It says that it needs a parameterless constructor to be registered.

I must have missed smth...


On Sat, Oct 3, 2015, 11:04 AM Chris Patterson <ch...@phatboyg.com> wrote:
Usage of Autofac (and several other containers) is well documented in the documentation.



On Sat, Oct 3, 2015 at 9:42 AM, alexandre joyeux <alexan...@gmail.com> wrote:

 Hi,


I'm new to masstransit and I'm trying to injrct dependencies in my message consumer.


I'm not sure how to register my consumer using autofac.

Any help would be highly appreciated :-)


Thanks, 

Alex


public class InstrumentCommandConsumer : IConsumer<TakeMeasure>

    {


        private IUnitOfWorkAsync _uow;

        private IInstrumentOutputDomainService _instrumentOutputDomainService;



        public InstrumentCommandHandler(IUnitOfWorkAsync uow,

            IInstrumentOutputDomainService instrumentOutputDomainService)

        {

            _uow = uow;

            _instrumentOutputDomainService = instrumentOutputDomainService;

        }

--
You received this message because you are subscribed to the Google Groups "masstransit-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to masstransit-discuss+unsub...@googlegroups.com.

--
You received this message because you are subscribed to a topic in the Google Groups "masstransit-discuss" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/masstransit-discuss/-L0QIsJXfq4/unsubscribe.
To unsubscribe from this group and all its topics, send an email to masstransit-discuss+unsub...@googlegroups.com.

Chris Patterson

unread,
Oct 3, 2015, 5:54:19 PM10/3/15
to masstrans...@googlegroups.com
No shame. It's a great post. 

__
Chris Patterson




To unsubscribe from this group and stop receiving emails from it, send an email to masstransit-dis...@googlegroups.com.

To post to this group, send email to masstrans...@googlegroups.com.

Dmitry Gavrovsky

unread,
Oct 8, 2015, 7:09:46 AM10/8/15
to masstransit-discuss
Hi, Alexandre

Have you been able to resolve your issue? I have same issue and still investigating how to register consumer properly.

On Saturday, October 3, 2015 at 10:32:55 PM UTC+3, alexandre joyeux wrote:

Hi Chris,

As you said the doc for autofac is well documented, but I just don't understand how to register my consumer. It says that it needs a parameterless constructor to be registered.

I must have missed smth...


On Sat, Oct 3, 2015, 11:04 AM Chris Patterson <ch...@phatboyg.com> wrote:
Usage of Autofac (and several other containers) is well documented in the documentation.



On Sat, Oct 3, 2015 at 9:42 AM, alexandre joyeux <alexan...@gmail.com> wrote:

 Hi,


I'm new to masstransit and I'm trying to injrct dependencies in my message consumer.


I'm not sure how to register my consumer using autofac.

Any help would be highly appreciated :-)


Thanks, 

Alex


public class InstrumentCommandConsumer : IConsumer<TakeMeasure>

    {


        private IUnitOfWorkAsync _uow;

        private IInstrumentOutputDomainService _instrumentOutputDomainService;



        public InstrumentCommandHandler(IUnitOfWorkAsync uow,

            IInstrumentOutputDomainService instrumentOutputDomainService)

        {

            _uow = uow;

            _instrumentOutputDomainService = instrumentOutputDomainService;

        }

--
You received this message because you are subscribed to the Google Groups "masstransit-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to masstransit-discuss+unsub...@googlegroups.com.

--
You received this message because you are subscribed to a topic in the Google Groups "masstransit-discuss" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/masstransit-discuss/-L0QIsJXfq4/unsubscribe.
To unsubscribe from this group and all its topics, send an email to masstransit-discuss+unsub...@googlegroups.com.

Sudhanshu Mishra

unread,
May 16, 2016, 4:50:23 AM5/16/16
to masstransit-discuss
This may be late for you, but for the sake of completeness, here's what worked as expected using the Unity DI container (Autofac should be similar)

1) Install-Package MassTransit.Unity 
2) Build container
3) When adding consumer, pass a reference to container:

private static IBusControl InitialiseBus(MessagingConfiguration messagingConfiguration, IUnityContainer container)
        {
            
            IBusControl busControl = Bus.Factory.CreateUsingRabbitMq(busFactoryConfigurator =>
            {
                var host = busFactoryConfigurator.Host(new Uri(messagingConfiguration.MqServerUri),
                    rabbitMqHostConfigurator =>
                    {
                        rabbitMqHostConfigurator.Username(messagingConfiguration.Username);
                        rabbitMqHostConfigurator.Password(messagingConfiguration.Password);
                    });

                busFactoryConfigurator.ReceiveEndpoint(host, messagingConfiguration.ReceiveQueue,
                    receiveEndpointConfigurator =>
                    {
                        // Uncomment When you have a ISaga
                        // receiveEndpointConfigurator.LoadFrom(UnityBootstrapper.Container);
                        receiveEndpointConfigurator.Consumer<PaymentRequestConsumer>(container);
                    });
            });

            return busControl;
        }
Reply all
Reply to author
Forward
0 new messages