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; } }
/// <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() )); }
/// <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)); } }
/// <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); } }
/// <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); } }
/// <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); } }
/// <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}"); })); }
public IocEventHandlerFactory(IHybridServiceScopeFactory scopeFactory, Type handlerType) { HandlerType = handlerType; ServiceScope = scopeFactory.CreateScope(); }
/// <summary> /// 获取事件处理器实例 /// </summary> /// <returns></returns> public EventHandlerDisposeWrapper GetHandler() { IServiceScope scope = _serviceScopeFactory.CreateScope(); return(new EventHandlerDisposeWrapper((IEventHandler)scope.ServiceProvider.GetService(_handlerType), () => scope.Dispose())); }