/// <summary> /// Execute transaction /// </summary> /// <param name="strategy"></param> /// <returns></returns> protected virtual async Task ExecuteRegistrationTransaction(DbContext database, IUploader user, string password, RequestResult <IUploader> serviceResult) { IExecutionStrategy databaseStrategy = database.Database.CreateExecutionStrategy(); await databaseStrategy.ExecuteAsync(async() => { using (var transaction = database.Database.BeginTransaction()) { try { IdentityResult createResult = await userManager.CreateAsync(user as GalleryUser, password); if (!createResult.Succeeded) //Check status of operation { serviceResult.BadRequest(createResult.Errors.Select(item => item.Description).ToList()); throw new Exception("Failed to add user to identity database"); } var roleResult = userManager.AddToRoleAsync(user as GalleryUser, Role.User.Name); //Add role to user in identity database var galleryDatabase = repository.AddAsync(user.ToDomainModel()); await Task.WhenAll(roleResult, galleryDatabase); // wait tasks to finish if (!roleResult.Result.Succeeded) // Check status of adding role to user { serviceResult.BadRequest("Problem with registration"); throw new Exception("Failed to add role to user"); } if (!string.IsNullOrEmpty(galleryDatabase.Result.errorMessage)) //Check status of adding user to gallery database { serviceResult.BadRequest("Problem with registration"); throw new Exception("Failed to add role to user"); } transaction.Commit(); // commit transaction serviceResult.GoodRequest(user); } catch (Exception e) //Just for debuging purpose { transaction?.Rollback(); serviceResult.BadRequest("Problem with registration"); } } }); }