コード例 #1
0
        private IUnitOfWork CreateNewUnitOfWork()
        {
            var scope = _serviceScopeFactory.CreateScope();

            try
            {
                var outerUow = _ambientUnitOfWork.UnitOfWork;

                var unitOfWork = scope.ServiceProvider.GetRequiredService <IUnitOfWork>();

                unitOfWork.SetOuter(outerUow);

                _ambientUnitOfWork.SetUnitOfWork(unitOfWork);

                unitOfWork.Disposed += (sender, args) =>
                {
                    _ambientUnitOfWork.SetUnitOfWork(outerUow);
                    scope.Dispose();
                };

                return(unitOfWork);
            }
            catch
            {
                scope.Dispose();
                throw;
            }
        }
コード例 #2
0
        /// <summary>
        /// Resolves handler object from Ioc container.
        /// </summary>
        /// <returns>Resolved handler object</returns>
        public IEventHandlerDisposeWrapper GetHandler()
        {
            var scope = _serviceScopeFactory.CreateScope();

            return(new EventHandlerDisposeWrapper(
                       (IEventHandler)scope.ServiceProvider.GetRequiredService(HandlerType), () => scope.Dispose()
                       ));
        }
コード例 #3
0
        /// <summary>
        /// 执行<see cref="ServiceLifetime.Scoped"/>生命周期的业务逻辑,并获取返回值
        /// 1.当前处理<see cref="ServiceLifetime.Scoped"/>生命周期外,使用CreateScope创建<see cref="ServiceLifetime.Scoped"/>
        /// 生命周期的ServiceProvider来执行,并释放资源
        /// 2.当前处于<see cref="ServiceLifetime.Scoped"/>生命周期内,直接使用<see cref="ServiceLifetime.Scoped"/>的ServiceProvider来执行
        /// </summary>
        public static async Task <TResult> ExecuteScopedWorkAsync <TResult>(this IServiceProvider provider, Func <IServiceProvider, Task <TResult> > func)
        {
            IHybridServiceScopeFactory factory = provider.GetService <IHybridServiceScopeFactory>();

            using (IServiceScope scope = factory.CreateScope())
            {
                return(await func(scope.ServiceProvider));
            }
        }
コード例 #4
0
        /// <summary>
        /// 异步执行<see cref="ServiceLifetime.Scoped"/>生命周期的业务逻辑
        /// 1.当前处理<see cref="ServiceLifetime.Scoped"/>生命周期外,使用CreateScope创建<see cref="ServiceLifetime.Scoped"/>
        /// 生命周期的ServiceProvider来执行,并释放资源
        /// 2.当前处于<see cref="ServiceLifetime.Scoped"/>生命周期内,直接使用<see cref="ServiceLifetime.Scoped"/>的ServiceProvider来执行
        /// </summary>
        public static async Task ExecuteScopedWorkAsync(this IServiceProvider provider, Func <IServiceProvider, Task> action)
        {
            IHybridServiceScopeFactory factory = provider.GetService <IHybridServiceScopeFactory>();

            using (IServiceScope scope = factory.CreateScope())
            {
                await action(scope.ServiceProvider);
            }
        }
コード例 #5
0
        /// <summary>
        /// 执行<see cref="ServiceLifetime.Scoped"/>生命周期的业务逻辑
        /// 1.当前处理<see cref="ServiceLifetime.Scoped"/>生命周期外,使用CreateScope创建<see cref="ServiceLifetime.Scoped"/>
        /// 生命周期的ServiceProvider来执行,并释放资源
        /// 2.当前处于<see cref="ServiceLifetime.Scoped"/>生命周期内,直接使用<see cref="ServiceLifetime.Scoped"/>的ServiceProvider来执行
        /// </summary>
        public static void ExecuteScopedWork(this IServiceProvider provider, Action <IServiceProvider> action)
        {
            IHybridServiceScopeFactory factory = provider.GetService <IHybridServiceScopeFactory>();

            using (IServiceScope scope = factory.CreateScope())
            {
                action(scope.ServiceProvider);
            }
        }
コード例 #6
0
        /// <summary>
        /// 认证
        /// </summary>
        /// <param name="userDomainName">用户名 </param>
        /// <param name="password"></param>
        /// <returns></returns>
        public bool Authenticate(string userDomainName, string password)
        {
            try {
                using (GetConnection(userDomainName, password)) {
                    return(true);
                }
            } catch (Exception ex) {
                using (var scope = _hybridServiceScopeFactory.CreateScope()) {
                    scope.ServiceProvider
                    .GetRequiredService <IExceptionNotifier> ()
                    .NotifyAsync(ex);
                }

                return(false);
            }
        }
コード例 #7
0
ファイル: IocEventHandlerFactory.cs プロジェクト: zy97/osharp
        /// <summary>
        /// 获取事件处理器实例
        /// </summary>
        /// <returns></returns>
        public EventHandlerDisposeWrapper GetHandler()
        {
            IServiceScope scope = _serviceScopeFactory.CreateScope();
            string        token = scope.GetHashCode().ToString();

            _logger.LogDebug($"创建处理器“{_handlerType}”的执行作用域,作用域标识:{token}");
            IServiceProvider scopeProvider = scope.ServiceProvider;
            IEventHandler    eventHandler  = (IEventHandler)scopeProvider.GetService(_handlerType);

            _logger.LogDebug($"创建处理器“{_handlerType}”的实例,作用域标识:{token}");
            return(new EventHandlerDisposeWrapper(eventHandler, () =>
            {
                scope.Dispose();
                _logger.LogDebug($"释放处理器“{_handlerType}”的执行作用域,作用域标识: {token}");
            }));
        }
コード例 #8
0
 public IocEventHandlerFactory(IHybridServiceScopeFactory scopeFactory, Type handlerType)
 {
     HandlerType  = handlerType;
     ServiceScope = scopeFactory.CreateScope();
 }
コード例 #9
0
        /// <summary>
        /// 获取事件处理器实例
        /// </summary>
        /// <returns></returns>
        public EventHandlerDisposeWrapper GetHandler()
        {
            IServiceScope scope = _serviceScopeFactory.CreateScope();

            return(new EventHandlerDisposeWrapper((IEventHandler)scope.ServiceProvider.GetService(_handlerType), () => scope.Dispose()));
        }