/// <summary> /// Scans the lifetime scope and registers any state machines sagas which are found in the scope using the Autofac saga repository /// and the appropriate state machine saga repository under the hood. /// </summary> /// <param name="configurator"></param> /// <param name="context"></param> /// <param name="name"></param> /// <param name="configureScope">Configuration for scope container</param> public static void LoadStateMachineSagas(this IReceiveEndpointConfigurator configurator, IComponentContext context, string name = "message", Action <ContainerBuilder, ConsumeContext> configureScope = null) { var registration = context.ResolveOptional <IRegistration>(); if (registration != null) { registration.ConfigureSagas(configurator); return; } var scope = context.Resolve <ILifetimeScope>(); IEnumerable <Type> sagaTypes = FindStateMachineSagaTypes(context); var stateMachineFactory = new AutofacSagaStateMachineFactory(scope); var scopeProvider = new SingleLifetimeScopeProvider(scope); var repositoryFactory = new AutofacSagaRepositoryFactory(scopeProvider, name, configureScope); var activityFactory = new AutofacStateMachineActivityFactory(); foreach (var sagaType in sagaTypes) { StateMachineSagaConfiguratorCache.Configure(sagaType, configurator, stateMachineFactory, repositoryFactory, activityFactory); } }
ISagaScopeContext <T> ISagaScopeProvider <TSaga> .GetScope <T>(ConsumeContext <T> context) { if (context.TryGetPayload <ILifetimeScope>(out _)) { return(new ExistingSagaScopeContext <T>(context)); } var parentLifetimeScope = _scopeProvider.GetLifetimeScope(context); var lifetimeScope = parentLifetimeScope.BeginLifetimeScope(_name, builder => { builder.ConfigureScope(context); _configureScope?.Invoke(builder, context); }); try { IStateMachineActivityFactory factory = new AutofacStateMachineActivityFactory(); var proxy = new ConsumeContextScope <T>(context, lifetimeScope, factory); foreach (Action <ConsumeContext> scopeAction in _scopeActions) { scopeAction(proxy); } return(new CreatedSagaScopeContext <ILifetimeScope, T>(lifetimeScope, proxy)); } catch { lifetimeScope.Dispose(); throw; } }