diff --git a/framework/src/Volo.Abp.AspNetCore.Components.Server/Microsoft/AspNetCore/Authentication/Cookies/CookieAuthenticationOptionsExtensions.cs b/framework/src/Volo.Abp.AspNetCore.Components.Server/Microsoft/AspNetCore/Authentication/Cookies/CookieAuthenticationOptionsExtensions.cs index 7852848e30c..9d303579da0 100644 --- a/framework/src/Volo.Abp.AspNetCore.Components.Server/Microsoft/AspNetCore/Authentication/Cookies/CookieAuthenticationOptionsExtensions.cs +++ b/framework/src/Volo.Abp.AspNetCore.Components.Server/Microsoft/AspNetCore/Authentication/Cookies/CookieAuthenticationOptionsExtensions.cs @@ -5,6 +5,7 @@ using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; +using Volo.Abp.Threading; namespace Microsoft.AspNetCore.Authentication.Cookies; @@ -70,7 +71,8 @@ private async static Task GetOpenIdConnectOptions(CookieVa var openIdConnectOptions = principalContext.HttpContext.RequestServices.GetRequiredService>().Get(oidcAuthenticationScheme); if (openIdConnectOptions.Configuration == null && openIdConnectOptions.ConfigurationManager != null) { - openIdConnectOptions.Configuration = await openIdConnectOptions.ConfigurationManager.GetConfigurationAsync(principalContext.HttpContext.RequestAborted); + var cancellationTokenProvider = principalContext.HttpContext.RequestServices.GetRequiredService(); + openIdConnectOptions.Configuration = await openIdConnectOptions.ConfigurationManager.GetConfigurationAsync(cancellationTokenProvider.Token); } return openIdConnectOptions; diff --git a/framework/src/Volo.Abp.AspNetCore.MultiTenancy/Volo/Abp/AspNetCore/MultiTenancy/AbpAspNetCoreMultiTenancyOptions.cs b/framework/src/Volo.Abp.AspNetCore.MultiTenancy/Volo/Abp/AspNetCore/MultiTenancy/AbpAspNetCoreMultiTenancyOptions.cs index 95efaf2d0c3..7ac6f411768 100644 --- a/framework/src/Volo.Abp.AspNetCore.MultiTenancy/Volo/Abp/AspNetCore/MultiTenancy/AbpAspNetCoreMultiTenancyOptions.cs +++ b/framework/src/Volo.Abp.AspNetCore.MultiTenancy/Volo/Abp/AspNetCore/MultiTenancy/AbpAspNetCoreMultiTenancyOptions.cs @@ -19,6 +19,7 @@ using Volo.Abp.Http; using Volo.Abp.Json; using Volo.Abp.MultiTenancy; +using Volo.Abp.Threading; namespace Volo.Abp.AspNetCore.MultiTenancy; @@ -92,15 +93,16 @@ public AbpAspNetCoreMultiTenancyOptions() context.Response.ContentType = resolvedContentType; context.Response.StatusCode = (int)HttpStatusCode.NotFound; + var cancellationTokenProvider = context.RequestServices.GetRequiredService(); var responseStream = context.Response.Body; if (resolvedContentTypeEncoding.CodePage == Encoding.UTF8.CodePage) { try { - await JsonSerializer.SerializeAsync(responseStream, error, error.GetType(), jsonSerializerOptions, context.RequestAborted); - await responseStream.FlushAsync(context.RequestAborted); + await JsonSerializer.SerializeAsync(responseStream, error, error.GetType(), jsonSerializerOptions, cancellationTokenProvider.Token); + await responseStream.FlushAsync(cancellationTokenProvider.Token); } - catch (OperationCanceledException) when (context.RequestAborted.IsCancellationRequested) { } + catch (OperationCanceledException) when (cancellationTokenProvider.Token.IsCancellationRequested) { } } else { @@ -108,10 +110,10 @@ public AbpAspNetCoreMultiTenancyOptions() ExceptionDispatchInfo? exceptionDispatchInfo = null; try { - await JsonSerializer.SerializeAsync(transcodingStream, error, error.GetType(), jsonSerializerOptions, context.RequestAborted); - await transcodingStream.FlushAsync(context.RequestAborted); + await JsonSerializer.SerializeAsync(transcodingStream, error, error.GetType(), jsonSerializerOptions, cancellationTokenProvider.Token); + await transcodingStream.FlushAsync(cancellationTokenProvider.Token); } - catch (OperationCanceledException) when (context.RequestAborted.IsCancellationRequested) { } + catch (OperationCanceledException) when (cancellationTokenProvider.Token.IsCancellationRequested) { } catch (Exception ex) { exceptionDispatchInfo = ExceptionDispatchInfo.Capture(ex); diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Uow/AbpUowActionFilter.cs b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Uow/AbpUowActionFilter.cs index 4a6a04a554e..9512a826986 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Uow/AbpUowActionFilter.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Uow/AbpUowActionFilter.cs @@ -1,11 +1,13 @@ using System; using System.Net.Http; +using System.Threading; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc.Abstractions; using Microsoft.AspNetCore.Mvc.Filters; using Microsoft.Extensions.Options; using Volo.Abp.AspNetCore.Filters; using Volo.Abp.DependencyInjection; +using Volo.Abp.Threading; using Volo.Abp.Uow; namespace Volo.Abp.AspNetCore.Mvc.Uow; @@ -37,6 +39,7 @@ public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionE var options = CreateOptions(context, unitOfWorkAttr); var unitOfWorkManager = context.GetRequiredService(); + var cancellationTokenProvider = context.GetRequiredService(); //Trying to begin a reserved UOW by AbpUnitOfWorkMiddleware if (unitOfWorkManager.TryBeginReserved(UnitOfWork.UnitOfWorkReservationName, options)) @@ -44,11 +47,11 @@ public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionE var result = await next(); if (Succeed(result)) { - await SaveChangesAsync(context, unitOfWorkManager); + await SaveChangesAsync(context, unitOfWorkManager, cancellationTokenProvider.Token); } else { - await RollbackAsync(context, unitOfWorkManager); + await RollbackAsync(context, unitOfWorkManager, cancellationTokenProvider.Token); } return; @@ -59,11 +62,11 @@ public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionE var result = await next(); if (Succeed(result)) { - await uow.CompleteAsync(context.HttpContext.RequestAborted); + await uow.CompleteAsync(cancellationTokenProvider.Token); } else { - await uow.RollbackAsync(context.HttpContext.RequestAborted); + await uow.RollbackAsync(cancellationTokenProvider.Token); } } } @@ -85,27 +88,27 @@ private AbpUnitOfWorkOptions CreateOptions(ActionExecutingContext context, UnitO return options; } - private async Task RollbackAsync(ActionExecutingContext context, IUnitOfWorkManager unitOfWorkManager) + private async Task RollbackAsync(ActionExecutingContext context, IUnitOfWorkManager unitOfWorkManager, CancellationToken cancellationToken) { var currentUow = unitOfWorkManager.Current; if (currentUow != null) { - await currentUow.RollbackAsync(context.HttpContext.RequestAborted); + await currentUow.RollbackAsync(cancellationToken); } } - private async Task SaveChangesAsync(ActionExecutingContext context, IUnitOfWorkManager unitOfWorkManager) + private async Task SaveChangesAsync(ActionExecutingContext context, IUnitOfWorkManager unitOfWorkManager, CancellationToken cancellationToken) { var currentUow = unitOfWorkManager.Current; if (currentUow != null) { try { - await currentUow.SaveChangesAsync(context.HttpContext.RequestAborted); + await currentUow.SaveChangesAsync(cancellationToken); } catch (Exception e) { - await currentUow.RollbackAsync(context.HttpContext.RequestAborted); + await currentUow.RollbackAsync(cancellationToken); throw; } } diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Uow/AbpUowPageFilter.cs b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Uow/AbpUowPageFilter.cs index f4a7be33339..5c69c07797b 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Uow/AbpUowPageFilter.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Uow/AbpUowPageFilter.cs @@ -1,5 +1,6 @@ using System; using System.Net.Http; +using System.Threading; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc.Abstractions; using Microsoft.AspNetCore.Mvc.Filters; @@ -7,6 +8,7 @@ using Volo.Abp.AspNetCore.Filters; using Volo.Abp.AspNetCore.Uow; using Volo.Abp.DependencyInjection; +using Volo.Abp.Threading; using Volo.Abp.Uow; namespace Volo.Abp.AspNetCore.Mvc.Uow; @@ -43,6 +45,7 @@ public async Task OnPageHandlerExecutionAsync(PageHandlerExecutingContext contex var options = CreateOptions(context, unitOfWorkAttr); var unitOfWorkManager = context.GetRequiredService(); + var cancellationTokenProvider = context.GetRequiredService(); //Trying to begin a reserved UOW by AbpUnitOfWorkMiddleware if (unitOfWorkManager.TryBeginReserved(UnitOfWork.UnitOfWorkReservationName, options)) @@ -50,11 +53,11 @@ public async Task OnPageHandlerExecutionAsync(PageHandlerExecutingContext contex var result = await next(); if (Succeed(result)) { - await SaveChangesAsync(context, unitOfWorkManager); + await SaveChangesAsync(context, unitOfWorkManager, cancellationTokenProvider.Token); } else { - await RollbackAsync(context, unitOfWorkManager); + await RollbackAsync(context, unitOfWorkManager, cancellationTokenProvider.Token); } return; @@ -65,11 +68,11 @@ public async Task OnPageHandlerExecutionAsync(PageHandlerExecutingContext contex var result = await next(); if (Succeed(result)) { - await uow.CompleteAsync(context.HttpContext.RequestAborted); + await uow.CompleteAsync(cancellationTokenProvider.Token); } else { - await uow.RollbackAsync(context.HttpContext.RequestAborted); + await uow.RollbackAsync(cancellationTokenProvider.Token); } } } @@ -91,27 +94,27 @@ private AbpUnitOfWorkOptions CreateOptions(PageHandlerExecutingContext context, return options; } - private async Task RollbackAsync(PageHandlerExecutingContext context, IUnitOfWorkManager unitOfWorkManager) + private async Task RollbackAsync(PageHandlerExecutingContext context, IUnitOfWorkManager unitOfWorkManager, CancellationToken cancellationToken) { var currentUow = unitOfWorkManager.Current; if (currentUow != null) { - await currentUow.RollbackAsync(context.HttpContext.RequestAborted); + await currentUow.RollbackAsync(cancellationToken); } } - private async Task SaveChangesAsync(PageHandlerExecutingContext context, IUnitOfWorkManager unitOfWorkManager) + private async Task SaveChangesAsync(PageHandlerExecutingContext context, IUnitOfWorkManager unitOfWorkManager, CancellationToken cancellationToken) { var currentUow = unitOfWorkManager.Current; if (currentUow != null) { try { - await currentUow.SaveChangesAsync(context.HttpContext.RequestAborted); + await currentUow.SaveChangesAsync(cancellationToken); } catch (Exception e) { - await currentUow.RollbackAsync(context.HttpContext.RequestAborted); + await currentUow.RollbackAsync(cancellationToken); throw; } } diff --git a/framework/src/Volo.Abp.AspNetCore/Microsoft/Extensions/DependencyInjection/CookieAuthenticationOptionsExtensions.cs b/framework/src/Volo.Abp.AspNetCore/Microsoft/Extensions/DependencyInjection/CookieAuthenticationOptionsExtensions.cs index 15e353431ed..74def27c8ec 100644 --- a/framework/src/Volo.Abp.AspNetCore/Microsoft/Extensions/DependencyInjection/CookieAuthenticationOptionsExtensions.cs +++ b/framework/src/Volo.Abp.AspNetCore/Microsoft/Extensions/DependencyInjection/CookieAuthenticationOptionsExtensions.cs @@ -7,6 +7,7 @@ using Microsoft.AspNetCore.Authentication.OpenIdConnect; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; +using Volo.Abp.Threading; namespace Microsoft.Extensions.DependencyInjection; @@ -84,9 +85,10 @@ public static CookieAuthenticationOptions CheckTokenExpiration(this CookieAuthen private async static Task GetOpenIdConnectOptions(CookieValidatePrincipalContext principalContext, string oidcAuthenticationScheme) { var openIdConnectOptions = principalContext.HttpContext.RequestServices.GetRequiredService>().Get(oidcAuthenticationScheme); + var cancellationTokenProvider = principalContext.HttpContext.RequestServices.GetRequiredService(); if (openIdConnectOptions.Configuration == null && openIdConnectOptions.ConfigurationManager != null) { - openIdConnectOptions.Configuration = await openIdConnectOptions.ConfigurationManager.GetConfigurationAsync(principalContext.HttpContext.RequestAborted); + openIdConnectOptions.Configuration = await openIdConnectOptions.ConfigurationManager.GetConfigurationAsync(cancellationTokenProvider.Token); } return openIdConnectOptions; diff --git a/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Uow/AbpUnitOfWorkMiddleware.cs b/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Uow/AbpUnitOfWorkMiddleware.cs index a3c98978f04..74833bf7480 100644 --- a/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Uow/AbpUnitOfWorkMiddleware.cs +++ b/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Uow/AbpUnitOfWorkMiddleware.cs @@ -6,6 +6,7 @@ using Microsoft.Extensions.Options; using Volo.Abp.AspNetCore.Middleware; using Volo.Abp.DependencyInjection; +using Volo.Abp.Threading; using Volo.Abp.Uow; namespace Volo.Abp.AspNetCore.Uow; @@ -14,13 +15,16 @@ public class AbpUnitOfWorkMiddleware : AbpMiddlewareBase, ITransientDependency { private readonly IUnitOfWorkManager _unitOfWorkManager; private readonly AbpAspNetCoreUnitOfWorkOptions _options; + private readonly ICancellationTokenProvider _cancellationTokenProvider; public AbpUnitOfWorkMiddleware( IUnitOfWorkManager unitOfWorkManager, - IOptions options) + IOptions options, + ICancellationTokenProvider cancellationTokenProvider) { _unitOfWorkManager = unitOfWorkManager; _options = options.Value; + _cancellationTokenProvider = cancellationTokenProvider; } public async override Task InvokeAsync(HttpContext context, RequestDelegate next) @@ -34,7 +38,7 @@ public async override Task InvokeAsync(HttpContext context, RequestDelegate next using (var uow = _unitOfWorkManager.Reserve(UnitOfWork.UnitOfWorkReservationName)) { await next(context); - await uow.CompleteAsync(context.RequestAborted); + await uow.CompleteAsync(_cancellationTokenProvider.Token); } }