Example #1
0
        async ValueTask <IConsumerScopeContext <TConsumer, T> > IConsumerScopeProvider.GetScope <TConsumer, T>(ConsumeContext <T> context)
        {
            if (context.TryGetPayload <IServiceScope>(out var existingServiceScope))
            {
                existingServiceScope.SetCurrentConsumeContext(context);

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

                var consumerContext = new ConsumerConsumeContextScope <TConsumer, T>(context, consumer);

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

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

            var serviceScope = serviceProvider.CreateScope();

            try
            {
                var scopeServiceProvider = new DependencyInjectionScopeServiceProvider(serviceScope.ServiceProvider);

                var scopeContext = new ConsumeContextScope <T>(context, serviceScope, serviceScope.ServiceProvider, scopeServiceProvider);

                serviceScope.SetCurrentConsumeContext(scopeContext);

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

                var consumerContext = new ConsumerConsumeContextScope <TConsumer, T>(scopeContext, consumer);

                return(new CreatedConsumerScopeContext <IServiceScope, TConsumer, T>(serviceScope, consumerContext));
            }
            catch
            {
                if (serviceScope is IAsyncDisposable asyncDisposable)
                {
                    await asyncDisposable.DisposeAsync().ConfigureAwait(false);
                }
                else
                {
                    serviceScope.Dispose();
                }

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

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

                var consumerContext = new ConsumerConsumeContextScope <TConsumer, T>(context, consumer);

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

            var scope = _kernel.CreateNewOrUseExistingMessageScope();

            try
            {
                var scopeContext = new ConsumeContextScope <T>(context, _kernel);

                _kernel.UpdateScope(scopeContext);

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

                var consumerContext = new ConsumerConsumeContextScope <TConsumer, T>(scopeContext, consumer);

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

                return(new CreatedConsumerScopeContext <IDisposable, TConsumer, T>(scope, consumerContext, ReleaseComponent));
            }
            catch
            {
                scope?.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}'.");
                }

                var consumerContext = new ConsumerConsumeContextScope <TConsumer, T>(context, consumer);

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

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

            var serviceScope = serviceProvider.CreateScope();

            try
            {
                var scopeContext = new ConsumeContextScope <T>(context, serviceScope, serviceScope.ServiceProvider);

                serviceScope.UpdateScope(scopeContext);

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

                var consumerContext = new ConsumerConsumeContextScope <TConsumer, T>(scopeContext, consumer);

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

                throw;
            }
        }
Example #4
0
        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}'.");
                }

                var consumerContext = new ConsumerConsumeContextScope <TConsumer, T>(context, consumer);

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

            var scope = AsyncScopedLifestyle.BeginScope(_container);

            try
            {
                var scopeContext = new ConsumeContextScope <T>(context, scope, scope.Container);

                scope.UpdateScope(scopeContext);

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

                var consumerContext = new ConsumerConsumeContextScope <TConsumer, T>(scopeContext, consumer);

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

                throw;
            }
        }
Example #5
0
        public IConsumerScopeContext <TConsumer, T> GetScope <TConsumer, T>(ConsumeContext <T> context)
            where TConsumer : class
            where T : class
        {
            if (context.TryGetPayload <INestedContainer>(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}'.");
                }

                var consumerContext = new ConsumerConsumeContextScope <TConsumer, T>(context, consumer);

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

            var nestedContainer = _container.GetNestedContainer(context);

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

                var consumerContext = new ConsumerConsumeContextScope <TConsumer, T>(context, consumer, nestedContainer);

                return(new CreatedConsumerScopeContext <INestedContainer, TConsumer, T>(nestedContainer, consumerContext));
            }
            catch
            {
                nestedContainer.Dispose();
                throw;
            }
        }
Example #6
0
        public IConsumerScopeContext <TConsumer, T> GetScope <TConsumer, T>(ConsumeContext <T> context)
            where TConsumer : class
            where T : class
        {
            if (context.TryGetPayload <IUnityContainer>(out var existingScope))
            {
                var consumer = existingScope.Resolve <TConsumer>();
                if (consumer == null)
                {
                    throw new ConsumerException($"Unable to resolve consumer type '{TypeMetadataCache<TConsumer>.ShortName}'.");
                }

                var consumerContext = new ConsumerConsumeContextScope <TConsumer, T>(context, consumer);

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

            var scope = _container.CreateChildContainer();

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

                var consumerContext = new ConsumerConsumeContextScope <TConsumer, T>(context, consumer, scope);

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

                throw;
            }
        }