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

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

            var serviceScope = serviceProvider.CreateScope();

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

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

                throw;
            }
        }
        public IPublishScopeContext <T> GetScope <T>(PublishContext <T> context)
            where T : class
        {
            if (context.TryGetPayload <INestedContainer>(out _))
            {
                return(new ExistingPublishScopeContext <T>(context));
            }

            var nestedContainer = _container.GetNestedContainer(context);

            try
            {
                var publishContext = new PublishContextScope <T>(context, nestedContainer);

                return(new CreatedPublishScopeContext <INestedContainer, T>(nestedContainer, publishContext));
            }
            catch
            {
                nestedContainer.Dispose();
                throw;
            }
        }
Пример #3
0
        public IPublishScopeContext <T> GetScope <T>(PublishContext <T> context)
            where T : class
        {
            if (context.TryGetPayload <IKernel>(out _))
            {
                return(new ExistingPublishScopeContext <T>(context));
            }

            var scope = _kernel.CreateNewMessageScope(context);

            try
            {
                var publishContext = new PublishContextScope <T>(context, _kernel);

                return(new CreatedPublishScopeContext <IDisposable, T>(scope, publishContext));
            }
            catch
            {
                scope?.Dispose();
                throw;
            }
        }
Пример #4
0
        public IPublishScopeContext <T> GetScope <T>(PublishContext <T> context)
            where T : class
        {
            if (context.TryGetPayload <Scope>(out _))
            {
                return(new ExistingPublishScopeContext <T>(context));
            }

            var scope = AsyncScopedLifestyle.BeginScope(_container);

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

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

                throw;
            }
        }