IConsumerScopeContext IConsumerScopeProvider.GetScope(ConsumeContext context)
        {
            if (context.TryGetPayload <ILifetimeScope>(out _))
            {
                return(new ExistingConsumerScopeContext(context));
            }

            var parentLifetimeScope = _scopeProvider.GetLifetimeScope(context);

            var lifetimeScope = parentLifetimeScope.BeginLifetimeScope(_name, builder =>
            {
                builder.ConfigureScope(context);
                _configureScope?.Invoke(builder, context);
            });

            try
            {
                var scopeContext = new ConsumeContextScope(context, lifetimeScope);

                return(new CreatedConsumerScopeContext <ILifetimeScope>(lifetimeScope, scopeContext));
            }
            catch
            {
                lifetimeScope.Dispose();

                throw;
            }
        }
        IConsumerScopeContext IConsumerScopeProvider.GetScope(ConsumeContext context)
        {
            if (context.TryGetPayload <ILifetimeScope>(out var existingLifetimeScope))
            {
                return(new ExistingConsumerScopeContext(context));
            }

            var parentLifetimeScope = _scopeProvider.GetLifetimeScope(context);

            var lifetimeScope = parentLifetimeScope.BeginLifetimeScope(_name, builder => builder.ConfigureScope(context));

            try
            {
                var proxy = new ConsumeContextProxyScope(context);

                var scope = lifetimeScope;
                proxy.GetOrAddPayload(() => scope);

                return(new CreatedConsumerScopeContext <ILifetimeScope>(scope, proxy));
            }
            catch
            {
                lifetimeScope.Dispose();

                throw;
            }
        }
Пример #3
0
        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
            {
                var proxy = new ConsumeContextProxy <T>(context, new PayloadCacheScope(context));
                proxy.UpdatePayload(lifetimeScope);

                foreach (Action <ConsumeContext> scopeAction in _scopeActions)
                {
                    scopeAction(proxy);
                }

                return(new CreatedSagaScopeContext <ILifetimeScope, T>(lifetimeScope, proxy));
            }
            catch
            {
                lifetimeScope.Dispose();

                throw;
            }
        }
        Task Send <T>(ConsumeContext <T> context, Func <ConsumeContext <T>, ISagaRepositoryContextFactory <TSaga>, Task> send)
            where T : class
        {
            if (context.TryGetPayload <ILifetimeScope>(out var existingScope))
            {
                context.GetOrAddPayload(() => existingScope.ResolveOptional <IStateMachineActivityFactory>() ?? AutofacStateMachineActivityFactory.Instance);

                var factory = existingScope.Resolve <ISagaRepositoryContextFactory <TSaga> >();

                return(send(context, factory));
            }

            var parentLifetimeScope = _scopeProvider.GetLifetimeScope(context);

            async Task CreateScope()
            {
                await using var scope = parentLifetimeScope.BeginLifetimeScope(_name, builder =>
                {
                    builder.ConfigureScope(context);
                    _configureScope?.Invoke(builder, context);
                });

                var activityFactory = scope.ResolveOptional <IStateMachineActivityFactory>() ?? AutofacStateMachineActivityFactory.Instance;

                var consumeContextScope = new ConsumeContextScope <T>(context, scope, activityFactory);

                var factory = scope.Resolve <ISagaRepositoryContextFactory <TSaga> >();

                await send(consumeContextScope, factory).ConfigureAwait(false);
            }

            return(CreateScope());
        }
        public IExecuteActivityScopeContext <TActivity, TArguments> GetScope(ExecuteContext <TArguments> context)
        {
            if (context.TryGetPayload <ILifetimeScope>(out var existingLifetimeScope))
            {
                var activity = existingLifetimeScope.Resolve <TActivity>(TypedParameter.From(context.Arguments));

                ExecuteActivityContext <TActivity, TArguments> activityContext = new HostExecuteActivityContext <TActivity, TArguments>(activity, context);

                return(new ExistingExecuteActivityScopeContext <TActivity, TArguments>(activityContext));
            }

            var parentLifetimeScope = _scopeProvider.GetLifetimeScope(context.ConsumeContext);

            var lifetimeScope = parentLifetimeScope.BeginLifetimeScope(_name, builder => builder.ConfigureScope(context.ConsumeContext));

            try
            {
                var activity = lifetimeScope.Resolve <TActivity>(TypedParameter.From(context.Arguments));

                ExecuteActivityContext <TActivity, TArguments> activityContext = new HostExecuteActivityContext <TActivity, TArguments>(activity, context);

                var scope = lifetimeScope;
                activityContext.GetOrAddPayload(() => scope);

                return(new CreatedExecuteActivityScopeContext <ILifetimeScope, TActivity, TArguments>(scope, activityContext));
            }
            catch
            {
                lifetimeScope.Dispose();

                throw;
            }
        }
Пример #6
0
        IPublishScopeContext <T> IPublishScopeProvider.GetScope <T>(PublishContext <T> context)
            where T : class
        {
            if (context.TryGetPayload <ILifetimeScope>(out _))
            {
                return(new ExistingPublishScopeContext <T>(context));
            }

            var parentLifetimeScope = _scopeProvider.GetLifetimeScope(context);

            var lifetimeScope = parentLifetimeScope.BeginLifetimeScope(_name, builder =>
            {
                builder.ConfigureScope(context);
                _configureScope?.Invoke(builder, context);
            });

            try
            {
                PublishContext <T> publishContext = lifetimeScope.GetPublishScope(context);

                return(new CreatedPublishScopeContext <ILifetimeScope, T>(lifetimeScope, publishContext));
            }
            catch
            {
                lifetimeScope.Dispose();

                throw;
            }
        }
        public async ValueTask <IMessageScopeContext <T> > GetScope <T>(ConsumeContext <T> context)
            where T : class
        {
            if (context.TryGetPayload <ILifetimeScope>(out _))
            {
                return(new ExistingMessageScopeContext <T>(context));
            }

            var parentLifetimeScope = _scopeProvider.GetLifetimeScope(context);

            var lifetimeScope = parentLifetimeScope.BeginLifetimeScope(_name, builder =>
            {
                builder.ConfigureScope(context);
                _configureScope?.Invoke(builder, context);
            });

            try
            {
                var proxy = new ConsumeContextScope <T>(context, lifetimeScope);

                return(new CreatedMessageScopeContext <ILifetimeScope, T>(lifetimeScope, proxy));
            }
            catch
            {
                await lifetimeScope.DisposeAsync().ConfigureAwait(false);

                throw;
            }
        }
        public IExecuteActivityScopeContext <TActivity, TArguments> GetScope(ExecuteContext <TArguments> context)
        {
            if (context.TryGetPayload <ILifetimeScope>(out var existingLifetimeScope))
            {
                var activity = existingLifetimeScope.Resolve <TActivity>(TypedParameter.From(context.Arguments));

                ExecuteActivityContext <TActivity, TArguments> activityContext = context.CreateActivityContext(activity);

                return(new ExistingExecuteActivityScopeContext <TActivity, TArguments>(activityContext));
            }

            var parentLifetimeScope = _scopeProvider.GetLifetimeScope(context);

            var lifetimeScope = parentLifetimeScope.BeginLifetimeScope(_name, builder =>
            {
                builder.ConfigureScope(context);
                _configureScope?.Invoke(builder, context);
            });

            try
            {
                var activity = lifetimeScope.Resolve <TActivity>(TypedParameter.From(context.Arguments));

                var executeContext = lifetimeScope.Resolve <ExecuteContext <TArguments> >();

                ExecuteActivityContext <TActivity, TArguments> activityContext = executeContext.CreateActivityContext(activity);

                return(new CreatedExecuteActivityScopeContext <ILifetimeScope, TActivity, TArguments>(lifetimeScope, activityContext));
            }
            catch
            {
                lifetimeScope.Dispose();
                throw;
            }
        }
Пример #9
0
        public ICompensateActivityScopeContext <TActivity, TLog> GetScope(CompensateContext <TLog> context)
        {
            if (context.TryGetPayload <ILifetimeScope>(out var existingLifetimeScope))
            {
                var activity = existingLifetimeScope.Resolve <TActivity>(TypedParameter.From(context.Log));

                CompensateActivityContext <TActivity, TLog> activityContext = new HostCompensateActivityContext <TActivity, TLog>(activity, context);

                return(new ExistingCompensateActivityScopeContext <TActivity, TLog>(activityContext));
            }

            var parentLifetimeScope = _scopeProvider.GetLifetimeScope(context.ConsumeContext);

            var lifetimeScope = parentLifetimeScope.BeginLifetimeScope(_name, builder =>
            {
                builder.ConfigureScope(context.ConsumeContext);
                _configureScope?.Invoke(builder, context);
            });

            try
            {
                var activity = lifetimeScope.Resolve <TActivity>(TypedParameter.From(context.Log));

                CompensateActivityContext <TActivity, TLog> activityContext = new HostCompensateActivityContext <TActivity, TLog>(activity, context);

                var scope = lifetimeScope;
                activityContext.GetOrAddPayload(() => scope);

                return(new CreatedCompensateActivityScopeContext <ILifetimeScope, TActivity, TLog>(scope, activityContext));
            }
            catch
            {
                lifetimeScope.Dispose();

                throw;
            }
        }
        public async ValueTask <ICompensateActivityScopeContext <TActivity, TLog> > GetScope(CompensateContext <TLog> context)
        {
            if (context.TryGetPayload <ILifetimeScope>(out var existingLifetimeScope))
            {
                var activity = existingLifetimeScope.Resolve <TActivity>(TypedParameter.From(context.Log));

                CompensateActivityContext <TActivity, TLog> activityContext = context.CreateActivityContext(activity);

                return(new ExistingCompensateActivityScopeContext <TActivity, TLog>(activityContext));
            }

            var parentLifetimeScope = _scopeProvider.GetLifetimeScope(context);

            var lifetimeScope = parentLifetimeScope.BeginLifetimeScope(_name, builder =>
            {
                builder.ConfigureScope(context);
                _configureScope?.Invoke(builder, context);
            });

            try
            {
                var activity = lifetimeScope.Resolve <TActivity>(TypedParameter.From(context.Log));

                var compensateContext = lifetimeScope.Resolve <CompensateContext <TLog> >();

                CompensateActivityContext <TActivity, TLog> activityContext = compensateContext.CreateActivityContext(activity);

                return(new CreatedCompensateActivityScopeContext <ILifetimeScope, TActivity, TLog>(lifetimeScope, activityContext));
            }
            catch
            {
                await lifetimeScope.DisposeAsync().ConfigureAwait(false);

                throw;
            }
        }