/// <summary> /// 删除用户实例。 /// </summary> /// <param name="user">当前用户实例。</param> /// <param name="cancellationToken">取消标志。</param> /// <returns>返回删除结果。</returns> public override async Task <IdentityResult> DeleteAsync(TUser user, CancellationToken cancellationToken = default) { cancellationToken.ThrowIfCancellationRequested(); if (user == null) { throw new ArgumentNullException(nameof(user)); } // ReSharper disable once SuspiciousTypeConversion.Global if (user is IUserEventHandler <TUser> handler) { if (await UserContext.BeginTransactionAsync(async db => { if (!await handler.OnDeleteAsync(db, cancellationToken)) { return(false); } if (!await db.DeleteAsync(user.UserId, cancellationToken)) { return(false); } return(true); }, cancellationToken: cancellationToken)) { return(IdentityResult.Success); } } else if (await UserContext.DeleteAsync(user.UserId, cancellationToken)) { return(IdentityResult.Success); } return(IdentityResult.Failed(ErrorDescriber.DefaultError())); }
/// <summary> /// 删除用户实例。 /// </summary> /// <param name="user">当前用户实例。</param> /// <param name="cancellationToken">取消标志。</param> /// <returns>返回删除结果。</returns> public override async Task <IdentityResult> DeleteAsync(TUser user, CancellationToken cancellationToken = default) { cancellationToken.ThrowIfCancellationRequested(); if (user == null) { throw new ArgumentNullException(nameof(user)); } var handlers = ServiceProvider.GetServices <IUserEventHandler <TUser> >().ToList(); if (handlers.Count > 0) { handlers = handlers.OrderByDescending(x => x.Priority).ToList(); if (await UserContext.BeginTransactionAsync(async db => { foreach (var handler in handlers) { try { if (!await handler.OnDeleteAsync(db, user, cancellationToken)) { return(false); } } catch (Exception exception) { Logger.LogError(exception, "删除用户发生错误:{0}", exception.Message); return(false); } } if (!await db.DeleteAsync(user.Id, cancellationToken)) { return(false); } return(true); }, cancellationToken: cancellationToken)) { return(IdentityResult.Success); } } else if (await UserContext.DeleteAsync(user.Id, cancellationToken)) { return(IdentityResult.Success); } return(IdentityResult.Failed(ErrorDescriber.DefaultError())); }
public async Task <TResponse> Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate <TResponse> next) { var response = default(TResponse); var typeName = request.GetGenericTypeName(); try { if (_dbContext.HasActiveTransaction) { return(await next()); } var strategy = _dbContext.Database.CreateExecutionStrategy(); await strategy.ExecuteAsync(async() => { Guid transactionId; using (var transaction = await _dbContext.BeginTransactionAsync()) using (LogContext.PushProperty("TransactionContext", transaction.TransactionId)) { _logger.LogInformation("----- Begin transaction {TransactionId} for {CommandName} ({@Command})", transaction.TransactionId, typeName, request); response = await next(); _logger.LogInformation("----- Commit transaction {TransactionId} for {CommandName}", transaction.TransactionId, typeName); await _dbContext.CommitTransactionAsync(transaction); transactionId = transaction.TransactionId; } await _userIntegrationEventService.PublishEventsThroughEventBusAsync(transactionId); }); return(response); } catch (Exception ex) { _logger.LogError(ex, "ERROR Handling transaction for {CommandName} ({@Command})", typeName, request); throw; } }