Browse Source

Use `ICancellationTokenProvider` to replace the `RequestAborted` token.

Resolve #20916
pull/20917/head
maliming 2 years ago
parent
commit
d82d6a8265
No known key found for this signature in database GPG Key ID: A646B9CB645ECEA4
  1. 4
      framework/src/Volo.Abp.AspNetCore.Components.Server/Microsoft/AspNetCore/Authentication/Cookies/CookieAuthenticationOptionsExtensions.cs
  2. 14
      framework/src/Volo.Abp.AspNetCore.MultiTenancy/Volo/Abp/AspNetCore/MultiTenancy/AbpAspNetCoreMultiTenancyOptions.cs
  3. 21
      framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Uow/AbpUowActionFilter.cs
  4. 21
      framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Uow/AbpUowPageFilter.cs
  5. 4
      framework/src/Volo.Abp.AspNetCore/Microsoft/Extensions/DependencyInjection/CookieAuthenticationOptionsExtensions.cs
  6. 8
      framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Uow/AbpUnitOfWorkMiddleware.cs

4
framework/src/Volo.Abp.AspNetCore.Components.Server/Microsoft/AspNetCore/Authentication/Cookies/CookieAuthenticationOptionsExtensions.cs

@ -5,6 +5,7 @@ using Microsoft.AspNetCore.Authentication.OpenIdConnect;
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 @@ public static class CookieAuthenticationOptionsExtensions
var openIdConnectOptions = principalContext.HttpContext.RequestServices.GetRequiredService<IOptionsMonitor<OpenIdConnectOptions>>().Get(oidcAuthenticationScheme);
if (openIdConnectOptions.Configuration == null && openIdConnectOptions.ConfigurationManager != null)
{
openIdConnectOptions.Configuration = await openIdConnectOptions.ConfigurationManager.GetConfigurationAsync(principalContext.HttpContext.RequestAborted);
var cancellationTokenProvider = principalContext.HttpContext.RequestServices.GetRequiredService<ICancellationTokenProvider>();
openIdConnectOptions.Configuration = await openIdConnectOptions.ConfigurationManager.GetConfigurationAsync(cancellationTokenProvider.Token);
}
return openIdConnectOptions;

14
framework/src/Volo.Abp.AspNetCore.MultiTenancy/Volo/Abp/AspNetCore/MultiTenancy/AbpAspNetCoreMultiTenancyOptions.cs

@ -19,6 +19,7 @@ using Microsoft.Net.Http.Headers;
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 class AbpAspNetCoreMultiTenancyOptions
context.Response.ContentType = resolvedContentType;
context.Response.StatusCode = (int)HttpStatusCode.NotFound;
var cancellationTokenProvider = context.RequestServices.GetRequiredService<ICancellationTokenProvider>();
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 class 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);

21
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 class AbpUowActionFilter : IAsyncActionFilter, IAbpFilter, ITransientDepe
var options = CreateOptions(context, unitOfWorkAttr);
var unitOfWorkManager = context.GetRequiredService<IUnitOfWorkManager>();
var cancellationTokenProvider = context.GetRequiredService<ICancellationTokenProvider>();
//Trying to begin a reserved UOW by AbpUnitOfWorkMiddleware
if (unitOfWorkManager.TryBeginReserved(UnitOfWork.UnitOfWorkReservationName, options))
@ -44,11 +47,11 @@ public class AbpUowActionFilter : IAsyncActionFilter, IAbpFilter, ITransientDepe
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 class AbpUowActionFilter : IAsyncActionFilter, IAbpFilter, ITransientDepe
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 @@ public class AbpUowActionFilter : IAsyncActionFilter, IAbpFilter, ITransientDepe
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;
}
}

21
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 Microsoft.Extensions.Options;
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 class AbpUowPageFilter : IAsyncPageFilter, IAbpFilter, ITransientDependen
var options = CreateOptions(context, unitOfWorkAttr);
var unitOfWorkManager = context.GetRequiredService<IUnitOfWorkManager>();
var cancellationTokenProvider = context.GetRequiredService<ICancellationTokenProvider>();
//Trying to begin a reserved UOW by AbpUnitOfWorkMiddleware
if (unitOfWorkManager.TryBeginReserved(UnitOfWork.UnitOfWorkReservationName, options))
@ -50,11 +53,11 @@ public class AbpUowPageFilter : IAsyncPageFilter, IAbpFilter, ITransientDependen
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 class AbpUowPageFilter : IAsyncPageFilter, IAbpFilter, ITransientDependen
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 @@ public class AbpUowPageFilter : IAsyncPageFilter, IAbpFilter, ITransientDependen
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;
}
}

4
framework/src/Volo.Abp.AspNetCore/Microsoft/Extensions/DependencyInjection/CookieAuthenticationOptionsExtensions.cs

@ -7,6 +7,7 @@ using Microsoft.AspNetCore.Authentication.Cookies;
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 class CookieAuthenticationOptionsExtensions
private async static Task<OpenIdConnectOptions> GetOpenIdConnectOptions(CookieValidatePrincipalContext principalContext, string oidcAuthenticationScheme)
{
var openIdConnectOptions = principalContext.HttpContext.RequestServices.GetRequiredService<IOptionsMonitor<OpenIdConnectOptions>>().Get(oidcAuthenticationScheme);
var cancellationTokenProvider = principalContext.HttpContext.RequestServices.GetRequiredService<ICancellationTokenProvider>();
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;

8
framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Uow/AbpUnitOfWorkMiddleware.cs

@ -6,6 +6,7 @@ using Microsoft.AspNetCore.Http;
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<AbpAspNetCoreUnitOfWorkOptions> options)
IOptions<AbpAspNetCoreUnitOfWorkOptions> options,
ICancellationTokenProvider cancellationTokenProvider)
{
_unitOfWorkManager = unitOfWorkManager;
_options = options.Value;
_cancellationTokenProvider = cancellationTokenProvider;
}
public async override Task InvokeAsync(HttpContext context, RequestDelegate next)
@ -34,7 +38,7 @@ public class AbpUnitOfWorkMiddleware : AbpMiddlewareBase, ITransientDependency
using (var uow = _unitOfWorkManager.Reserve(UnitOfWork.UnitOfWorkReservationName))
{
await next(context);
await uow.CompleteAsync(context.RequestAborted);
await uow.CompleteAsync(_cancellationTokenProvider.Token);
}
}

Loading…
Cancel
Save