Skip to content

Commit

Permalink
Merge pull request #20917 from abpframework/ICancellationTokenProvider
Browse files Browse the repository at this point in the history
Use `ICancellationTokenProvider` to replace the `RequestAborted` token.
  • Loading branch information
EngincanV authored Oct 2, 2024
2 parents 96975dc + 878dce1 commit 762585a
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -70,7 +71,8 @@ private async static Task<OpenIdConnectOptions> GetOpenIdConnectOptions(CookieVa
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -92,26 +93,27 @@ public 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
{
var transcodingStream = Encoding.CreateTranscodingStream(context.Response.Body, resolvedContentTypeEncoding, Encoding.UTF8, leaveOpen: true);
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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -37,18 +39,19 @@ public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionE
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))
{
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;
Expand All @@ -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);
}
}
}
Expand All @@ -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;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
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.AspNetCore.Uow;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Threading;
using Volo.Abp.Uow;

namespace Volo.Abp.AspNetCore.Mvc.Uow;
Expand Down Expand Up @@ -43,18 +45,19 @@ public async Task OnPageHandlerExecutionAsync(PageHandlerExecutingContext contex
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))
{
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;
Expand All @@ -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);
}
}
}
Expand All @@ -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;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -84,9 +85,10 @@ public static CookieAuthenticationOptions CheckTokenExpiration(this CookieAuthen
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)
Expand All @@ -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);
}
}

Expand Down

0 comments on commit 762585a

Please sign in to comment.