From b3216e65715032cd290ec4834bb2e0872aec3932 Mon Sep 17 00:00:00 2001 From: Richard Edwards Date: Thu, 10 Oct 2024 23:50:04 +0100 Subject: [PATCH] Fixes --- .../Abstractions/UmbrellaComponentBase.cs | 39 +++++-------------- 1 file changed, 10 insertions(+), 29 deletions(-) diff --git a/AspNetCore/src/Umbrella.AspNetCore.Shared/Components/Abstractions/UmbrellaComponentBase.cs b/AspNetCore/src/Umbrella.AspNetCore.Shared/Components/Abstractions/UmbrellaComponentBase.cs index 7e14ebd44..7fdb67df3 100644 --- a/AspNetCore/src/Umbrella.AspNetCore.Shared/Components/Abstractions/UmbrellaComponentBase.cs +++ b/AspNetCore/src/Umbrella.AspNetCore.Shared/Components/Abstractions/UmbrellaComponentBase.cs @@ -1,6 +1,5 @@ using Microsoft.AspNetCore.Components; using Microsoft.Extensions.Logging; -using System.Diagnostics.CodeAnalysis; using System.Security.Claims; using Umbrella.AspNetCore.Shared.Services.Abstractions; using Umbrella.Utilities.Mapping.Abstractions; @@ -12,11 +11,9 @@ namespace Umbrella.AspNetCore.Shared.Components.Abstractions; /// /// /// -[SuppressMessage("Usage", "CA2213:Disposable fields should be disposed", Justification = "False positive. The fields are disposed correctly.")] public abstract class UmbrellaComponentBase : ComponentBase, IAsyncDisposable { - private CancellationTokenSource? _componentCancellationTokenSource; - private CancellationTokenSource? _linkedCancellationTokenSource; + private Lazy? _cancellationTokenSource; private bool _disposedValue; [Inject] @@ -64,13 +61,12 @@ protected CancellationToken CancellationToken { get { - if (_linkedCancellationTokenSource is not null) - return _linkedCancellationTokenSource.Token; + if (_cancellationTokenSource is not null) + return _cancellationTokenSource.Value.Token; - _componentCancellationTokenSource = new CancellationTokenSource(); - _linkedCancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(_componentCancellationTokenSource.Token, HttpRequestAbortedService.RequestAborted); + _cancellationTokenSource = new Lazy(() => CancellationTokenSource.CreateLinkedTokenSource(HttpRequestAbortedService.RequestAborted)); - return _linkedCancellationTokenSource.Token; + return _cancellationTokenSource.Value.Token; } } @@ -92,34 +88,19 @@ protected virtual async ValueTask DisposeAsync(bool disposing) { if (disposing) { - if (_componentCancellationTokenSource is not null) + if (_cancellationTokenSource is { IsValueCreated: true }) { - if (_componentCancellationTokenSource.IsCancellationRequested) + if (_cancellationTokenSource.Value.IsCancellationRequested) { #if NET8_0_OR_GREATER - await _componentCancellationTokenSource.CancelAsync(); + await _cancellationTokenSource.Value.CancelAsync(); #else await Task.Yield(); - _componentCancellationTokenSource.Cancel(); + _cancellationTokenSource.Value.Cancel(); #endif } - _componentCancellationTokenSource.Dispose(); - } - - if (_linkedCancellationTokenSource is not null) - { - if (_linkedCancellationTokenSource.IsCancellationRequested) - { -#if NET8_0_OR_GREATER - await _linkedCancellationTokenSource.CancelAsync(); -#else - await Task.Yield(); - _linkedCancellationTokenSource.Cancel(); -#endif - } - - _linkedCancellationTokenSource.Dispose(); + _cancellationTokenSource.Value.Dispose(); } }