diff --git a/Hcs.WebApp/BackgroundServices/CampaignManagers/ExportCommonRegistryElementsManager_15_7_0_1.cs b/Hcs.WebApp/BackgroundServices/CampaignManagers/ExportCommonRegistryElementsManager_15_7_0_1.cs index aee05dd..bd25535 100644 --- a/Hcs.WebApp/BackgroundServices/CampaignManagers/ExportCommonRegistryElementsManager_15_7_0_1.cs +++ b/Hcs.WebApp/BackgroundServices/CampaignManagers/ExportCommonRegistryElementsManager_15_7_0_1.cs @@ -1,5 +1,6 @@ using Hcs.WebApp.Data.Hcs; using Hcs.WebApp.Services; +using Microsoft.EntityFrameworkCore; namespace Hcs.WebApp.BackgroundServices.CampaignManagers { @@ -41,29 +42,33 @@ namespace Hcs.WebApp.BackgroundServices.CampaignManagers private async Task DoStartStepAsync() { - IEnumerable operations; + IEnumerable? operations = null; using var context = HeadquartersService.GetNewContext(); - using var transaction = await context.Database.BeginTransactionAsync(); - try + var executionStrategy = context.Database.CreateExecutionStrategy(); + await executionStrategy.ExecuteAsync(async () => { - await HeadquartersService.SetCampaignStartedAsync(context, campaign.Id); + using var transaction = await context.Database.BeginTransactionAsync(); + try + { + await HeadquartersService.SetCampaignStartedAsync(context, campaign.Id); - campaign.Step = (int)Step.ExecutionWait; - await HeadquartersService.UpdateCampaignStepAsync(context, campaign); + campaign.Step = (int)Step.ExecutionWait; + await HeadquartersService.UpdateCampaignStepAsync(context, campaign); - var registryCount = await RegistryService.GetRegistryCountAsync(context, true); - operations = await HeadquartersService.InitiateOperationsAsync(context, registryCount, campaign.Id, Operation.OperationType.NsiCommon_ExportNsiItem_15_7_0_1); - await RegistryService.SetOperationsToRegistriesAsync(context, true, operations); + var registryCount = await RegistryService.GetRegistryCountAsync(context, true); + operations = await HeadquartersService.InitiateOperationsAsync(context, registryCount, campaign.Id, Operation.OperationType.NsiCommon_ExportNsiItem_15_7_0_1); + await RegistryService.SetOperationsToRegistriesAsync(context, true, operations); - await transaction.CommitAsync(); - } - catch - { - transaction?.Rollback(); + await transaction.CommitAsync(); + } + catch + { + transaction?.Rollback(); - throw; - } + throw; + } + }); if (operations != null) { @@ -98,22 +103,26 @@ namespace Hcs.WebApp.BackgroundServices.CampaignManagers if (resultWaitState.HasCampaignOperation(campaign.Id)) return; using var context = HeadquartersService.GetNewContext(); - using var transaction = await context.Database.BeginTransactionAsync(); - try + var executionStrategy = context.Database.CreateExecutionStrategy(); + await executionStrategy.ExecuteAsync(async () => { - campaign.Step = (int)Step.End; - await HeadquartersService.UpdateCampaignStepAsync(context, campaign); + using var transaction = await context.Database.BeginTransactionAsync(); + try + { + campaign.Step = (int)Step.End; + await HeadquartersService.UpdateCampaignStepAsync(context, campaign); - await HeadquartersService.SetCampaignEndedAsync(context, campaign.Id); + await HeadquartersService.SetCampaignEndedAsync(context, campaign.Id); - await transaction.CommitAsync(); - } - catch - { - transaction?.Rollback(); + await transaction.CommitAsync(); + } + catch + { + transaction?.Rollback(); - throw; - } + throw; + } + }); State = IManager.ManagerState.Ended; } diff --git a/Hcs.WebApp/BackgroundServices/ResultGetters/NsiCommon/ExportNsiItemGetter_15_7_0_1.cs b/Hcs.WebApp/BackgroundServices/ResultGetters/NsiCommon/ExportNsiItemGetter_15_7_0_1.cs index a37a8ee..0c665f4 100644 --- a/Hcs.WebApp/BackgroundServices/ResultGetters/NsiCommon/ExportNsiItemGetter_15_7_0_1.cs +++ b/Hcs.WebApp/BackgroundServices/ResultGetters/NsiCommon/ExportNsiItemGetter_15_7_0_1.cs @@ -1,6 +1,7 @@ using Hcs.Broker; using Hcs.WebApp.Data.Hcs; using Hcs.WebApp.Services; +using Microsoft.EntityFrameworkCore; using Newtonsoft.Json; namespace Hcs.WebApp.BackgroundServices.ResultGetters.NsiCommon @@ -21,36 +22,40 @@ namespace Hcs.WebApp.BackgroundServices.ResultGetters.NsiCommon var registryService = scope.ServiceProvider.GetRequiredService(); using var context = headquartersService.GetNewContext(); - using var transaction = await context.Database.BeginTransactionAsync(); - try + var executionStrategy = context.Database.CreateExecutionStrategy(); + await executionStrategy.ExecuteAsync(async () => { - var registry = await registryService.GetRegistryByOperationIdAsync(context, operation.Id, true); - registry.SyncedAt = DateTime.UtcNow; - registry.Elements.Clear(); - await context.SaveChangesAsync(); - - foreach (var element in result.Result!.NsiElement) + using var transaction = await context.Database.BeginTransactionAsync(); + try { - var registryElement = new RegistryElement() + var registry = await registryService.GetRegistryByOperationIdAsync(context, operation.Id, true); + registry.SyncedAt = DateTime.UtcNow; + registry.Elements.Clear(); + await context.SaveChangesAsync(); + + foreach (var element in result.Result!.NsiElement) { - Code = element.Code, - GUID = element.GUID, - Json = JsonConvert.SerializeObject(element) - }; - registry.Elements.Add(registryElement); + var registryElement = new RegistryElement() + { + Code = element.Code, + GUID = element.GUID, + Json = JsonConvert.SerializeObject(element) + }; + registry.Elements.Add(registryElement); + } + await context.SaveChangesAsync(); + + await headquartersService.SetOperationEndedAsync(context, operation.Id); + + await transaction.CommitAsync(); } - await context.SaveChangesAsync(); + catch + { + await transaction.RollbackAsync(); - await headquartersService.SetOperationEndedAsync(context, operation.Id); - - await transaction.CommitAsync(); - } - catch - { - transaction?.Rollback(); - - throw; - } + throw; + } + }); return true; } diff --git a/Hcs.WebApp/Services/UsersService.cs b/Hcs.WebApp/Services/UsersService.cs index 53a4368..4cc42eb 100644 --- a/Hcs.WebApp/Services/UsersService.cs +++ b/Hcs.WebApp/Services/UsersService.cs @@ -44,49 +44,54 @@ namespace Hcs.WebApp.Services public async Task UpdateUserAsync(string userId, string roleId, string password) { using var context = factory.CreateDbContext(); - using var transaction = await context.Database.BeginTransactionAsync(); - try + var executionStrategy = context.Database.CreateExecutionStrategy(); + await executionStrategy.ExecuteAsync(async () => { - var curUserRole = await (from userRole in context.UserRoles - where userRole.UserId == userId - select userRole).SingleAsync(); - var curRole = await (from role in context.Roles - where role.Id == curUserRole.RoleId - select role).SingleAsync(); - if (curRole.Id != roleId) + using var transaction = await context.Database.BeginTransactionAsync(); + try { - context.UserRoles.Remove(curUserRole); - - var newRole = await (from role in context.Roles - where role.Id == roleId + var curUserRole = await (from userRole in context.UserRoles + where userRole.UserId == userId + select userRole).SingleAsync(); + var curRole = await (from role in context.Roles + where role.Id == curUserRole.RoleId select role).SingleAsync(); - await context.UserRoles.AddAsync(new IdentityUserRole() + if (curRole.Id != roleId) { - UserId = userId, - RoleId = newRole.Id - }); - } + context.UserRoles.Remove(curUserRole); - var curUser = await (from user in context.Users - where user.Id == userId - select user).SingleAsync(); - var newPasswordHash = passwordHasher.HashPassword(curUser, password); - if (curUser.PasswordHash != newPasswordHash) + var newRole = await (from role in context.Roles + where role.Id == roleId + select role).SingleAsync(); + await context.UserRoles.AddAsync(new IdentityUserRole() + { + UserId = userId, + RoleId = newRole.Id + }); + } + + var curUser = await (from user in context.Users + where user.Id == userId + select user).SingleAsync(); + var newPasswordHash = passwordHasher.HashPassword(curUser, password); + if (curUser.PasswordHash != newPasswordHash) + { + curUser.PasswordHash = newPasswordHash; + + context.Users.Update(curUser); + } + + await context.SaveChangesAsync(); + + await transaction.CommitAsync(); + } + catch { - curUser.PasswordHash = newPasswordHash; + await transaction.RollbackAsync(); - context.Users.Update(curUser); + throw; } - - await context.SaveChangesAsync(); - } - catch (Exception) - { - await transaction.RollbackAsync(); - - throw; - } - await transaction.CommitAsync(); + }); } public async Task DeleteUserAsync(string userId)