Masstransit v3, Autofac and saga registration problem

310 views
Skip to first unread message

Paul

unread,
Nov 18, 2015, 5:12:17 PM11/18/15
to masstransit-discuss
Hi,

I'm trying to run my tests with Autofac. The problem is that tests are not working if I create bus in following way:

private IBusControl CreateBus(IComponentContext context)
        {
            return MassTransit.Bus.Factory.CreateUsingInMemory(x =>
            {
                _inMemoryTransportCache = new InMemoryTransportCache(Environment.ProcessorCount);
                x.SetTransportProvider(_inMemoryTransportCache);
                x.ReceiveEndpoint("input_queue", configurator =>
                {
                    var repo = Container.Resolve<ISagaRepository<MySaga>>();
                    var machine = Container.Resolve<SagaStateMachine<MySaga>>();
                    configurator.LoadFrom(context);
                });
            });
        }

but if I only add one more line like below then It works.

...
                x.ReceiveEndpoint("input_queue", configurator =>
                {
                    var repo = Container.Resolve<ISagaRepository<MySaga>>();
                    var machine = Container.Resolve<SagaStateMachine<MySaga>>();
                    configurator.LoadFrom(context);
                    configurator.StateMachineSaga(machine, repo);
                });
            });
        }


Autofac registrations look like this:

var builder = new ContainerBuilder();
            builder
                .Register(c => new InMemorySessionFactoryFactory().BuildFactory(typeof(MySagaMap)))
                .As<ISessionFactory>()
                .SingleInstance();
            builder
                .RegisterType(typeof(NHibernateSagaRepository<MySaga>))
                .As(typeof(ISagaRepository<MySaga>))
                .SingleInstance();
            builder
                .RegisterType<MySagaStateMachine>()
                .As<SagaStateMachine<MySaga>>();
            builder
                .RegisterType<MySaga>()
                .AsSelf();

            builder.Register(CreateBus)
                .As<IBus>()
                .As<IBusControl>()
                .SingleInstance();

            builder.Build();

Do you know what could be wrong with it?

Chris Patterson

unread,
Nov 18, 2015, 5:47:22 PM11/18/15
to masstransit-discuss

State machine sagas would not be properly registered using Autofac, as the Autofac extensions don’t know anything about the state machine (they’re separate assemblies).

 

It’s confusing, yeah, but not sure how else it would get wired up. Perhaps adding some general discovery things to MT might help, it would require changing how the container discovery works for every container though.

--
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/c8a8ba5a-f902-4396-af2d-8f1bdf6f1777%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

 

 

Paul

unread,
Nov 19, 2015, 2:08:58 PM11/19/15
to masstransit-discuss
Thank you again Chris :) 
Now I'm sure that my direction is correct.

If there will be some free time in a project I will try to change the way of discovering sagas.

P.

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

Alexey Zimarev

unread,
Mar 21, 2016, 6:34:20 AM3/21/16
to masstransit-discuss
My worry is that since SagaStateMachineInstance is ISaga, when I use LoadFrom(context), it scans for all IConsumer and ISaga in the container and for ISaga it calls configurator.Saga(repository), whilst for state machine Sagas we want it to call configurator.StateMachineSaga(machine, repository).

Doesn't it register message consumers there and if I call configurator.StateMachineSaga myself in the service initialization, it will do it again?


On Wednesday, November 18, 2015 at 11:47:22 PM UTC+1, Chris Patterson wrote:

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

Chris Patterson

unread,
Mar 21, 2016, 9:05:53 AM3/21/16
to masstrans...@googlegroups.com
Yeah load from really is a nice to have and not something that I would use with sagas. 

__
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.

Alexey Zimarev

unread,
Mar 22, 2016, 4:54:19 AM3/22/16
to masstransit-discuss
I created a small adapter that allows you to register state machine sagas and load them into the configuration.
Also, it allows loading consumers only, so sagas don't registered twice (this would happen if you use LoadFrom).

Here it is: https://github.com/alexeyzimarev/MassTransit.Autofac.StateMachineSagas
Reply all
Reply to author
Forward
0 new messages