示例#1
0
        IConsumerScopeContext <TConsumer, T> IConsumerScopeProvider.GetScope <TConsumer, T>(ConsumeContext <T> context)
        {
            if (context.TryGetPayload <ILifetimeScope>(out var existingLifetimeScope))
            {
                ConsumerConsumeContext <TConsumer, T> consumerContext = existingLifetimeScope.GetConsumer <TConsumer, T>(context);

                return(new ExistingConsumerScopeContext <TConsumer, T>(consumerContext));
            }

            var parentLifetimeScope = _scopeProvider.GetLifetimeScope(context);

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

            try
            {
                ConsumerConsumeContext <TConsumer, T> consumerContext = lifetimeScope.GetConsumerScope <TConsumer, T>(context);
                consumerContext.UpdatePayload(lifetimeScope);

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

                throw;
            }
        }
        IConsumerScopeContext <TConsumer, T> IConsumerScopeProvider.GetScope <TConsumer, T>(ConsumeContext <T> context)
        {
            if (context.TryGetPayload <IServiceScope>(out var existingServiceScope))
            {
                existingServiceScope.UpdateScope(context);

                var consumer = existingServiceScope.ServiceProvider.GetService <TConsumer>();
                if (consumer == null)
                {
                    throw new ConsumerException($"Unable to resolve consumer type '{TypeMetadataCache<TConsumer>.ShortName}'.");
                }

                ConsumerConsumeContext <TConsumer, T> consumerContext = context.PushConsumer(consumer);

                return(new ExistingConsumerScopeContext <TConsumer, T>(consumerContext));
            }

            if (!context.TryGetPayload(out IServiceProvider serviceProvider))
            {
                serviceProvider = _serviceProvider;
            }

            var serviceScope = serviceProvider.GetRequiredService <IServiceScopeFactory>().CreateScope();

            try
            {
                serviceScope.UpdateScope(context);

                var consumer = serviceScope.ServiceProvider.GetService <TConsumer>();
                if (consumer == null)
                {
                    throw new ConsumerException($"Unable to resolve consumer type '{TypeMetadataCache<TConsumer>.ShortName}'.");
                }

                ConsumerConsumeContext <TConsumer, T> consumerContext = context.PushConsumerScope(consumer, serviceScope);

                consumerContext.UpdatePayload(serviceScope);

                return(new CreatedConsumerScopeContext <IServiceScope, TConsumer, T>(serviceScope, consumerContext));
            }
            catch
            {
                serviceScope.Dispose();

                throw;
            }
        }
        public IConsumerScopeContext <TConsumer, T> GetScope <TConsumer, T>(ConsumeContext <T> context) where TConsumer : class where T : class
        {
            if (context.TryGetPayload <Scope>(out var existingScope))
            {
                existingScope.UpdateScope(context);

                var consumer = existingScope.Container.GetInstance <TConsumer>();
                if (consumer == null)
                {
                    throw new ConsumerException($"Unable to resolve consumer type '{TypeMetadataCache<TConsumer>.ShortName}'.");
                }

                ConsumerConsumeContext <TConsumer, T> consumerContext = context.PushConsumer(consumer);

                return(new ExistingConsumerScopeContext <TConsumer, T>(consumerContext));
            }

            var scope = AsyncScopedLifestyle.BeginScope(_container);

            try
            {
                scope.UpdateScope(context);

                var consumer = scope.Container.GetInstance <TConsumer>();
                if (consumer == null)
                {
                    throw new ConsumerException($"Unable to resolve consumer type '{TypeMetadataCache<TConsumer>.ShortName}'.");
                }

                ConsumerConsumeContext <TConsumer, T> consumerContext = context.PushConsumerScope(consumer, scope);

                consumerContext.UpdatePayload(scope);

                return(new CreatedConsumerScopeContext <Scope, TConsumer, T>(scope, consumerContext));
            }
            catch
            {
                scope.Dispose();

                throw;
            }
        }
示例#4
0
        public IConsumerScopeContext <TConsumer, T> GetScope <TConsumer, T>(ConsumeContext <T> context)
            where TConsumer : class
            where T : class
        {
            if (context.TryGetPayload <IContainer>(out var existingContainer))
            {
                existingContainer.Inject(context);

                var consumer = existingContainer.GetInstance <TConsumer>();
                if (consumer == null)
                {
                    throw new ConsumerException($"Unable to resolve consumer type '{TypeMetadataCache<TConsumer>.ShortName}'.");
                }

                ConsumerConsumeContext <TConsumer, T> consumerContext = context.PushConsumer(consumer);

                return(new ExistingConsumerScopeContext <TConsumer, T>(consumerContext));
            }

            var nestedContainer = _container?.CreateNestedContainer(context) ?? _context?.CreateNestedContainer(context);

            try
            {
                var consumer = nestedContainer.GetInstance <TConsumer>();
                if (consumer == null)
                {
                    throw new ConsumerException($"Unable to resolve consumer type '{TypeMetadataCache<TConsumer>.ShortName}'.");
                }

                ConsumerConsumeContext <TConsumer, T> consumerContext = context.PushConsumerScope(consumer, nestedContainer);
                consumerContext.UpdatePayload(nestedContainer);

                return(new CreatedConsumerScopeContext <IContainer, TConsumer, T>(nestedContainer, consumerContext));
            }
            catch
            {
                nestedContainer.Dispose();
                throw;
            }
        }