diff --git a/src/OrchardCore/OrchardCore.Abstractions/Shell/Models/ShellReleaseRequestContext.cs b/src/OrchardCore/OrchardCore.Abstractions/Shell/Models/ShellReleaseRequestContext.cs new file mode 100644 index 00000000000..e59ba68ee41 --- /dev/null +++ b/src/OrchardCore/OrchardCore.Abstractions/Shell/Models/ShellReleaseRequestContext.cs @@ -0,0 +1,8 @@ +namespace OrchardCore.Environment.Shell.Models; + +public class ShellReleaseRequestContext +{ + public const string ShellScopeKey = nameof(ShellReleaseRequestContext); + + public bool Release { get; set; } +} diff --git a/src/OrchardCore/OrchardCore.Abstractions/Shell/Scope/ShellScope.cs b/src/OrchardCore/OrchardCore.Abstractions/Shell/Scope/ShellScope.cs index cf2889fd8a7..5802a7b6fbe 100644 --- a/src/OrchardCore/OrchardCore.Abstractions/Shell/Scope/ShellScope.cs +++ b/src/OrchardCore/OrchardCore.Abstractions/Shell/Scope/ShellScope.cs @@ -7,6 +7,7 @@ using Microsoft.Extensions.Logging; using OrchardCore.Environment.Cache; using OrchardCore.Environment.Shell.Builders; +using OrchardCore.Environment.Shell.Models; using OrchardCore.Modules; namespace OrchardCore.Environment.Shell.Scope @@ -128,6 +129,42 @@ public static T Get(object key) return current._items.TryGetValue(key, out var value) ? value is T item ? item : default : default; } + /// + /// Gets a shared item of a given type from the current shell scope. + /// + public static bool TryGetValue(object key, out object value) + { + var current = Current; + + if (current == null) + { + value = default; + + return false; + } + + current._items ??= []; + + return current._items.TryGetValue(key, out value); + } + + /// + /// Gets a shared item of a given type from the current shell scope. + /// + public static bool TryGetValue(object key, out T value) + { + if (TryGetValue(key, out var existingValue) && existingValue is T item) + { + value = item; + + return true; + } + + value = default; + + return false; + } + /// /// Gets (or creates) a shared item of a given type from the current shell scope. /// @@ -498,6 +535,11 @@ await scope.UsingAsync(async scope => activateShell: false); } } + + if (TryGetValue(ShellReleaseRequestContext.ShellScopeKey, out var context) && context.Release) + { + await ShellContext.ReleaseAsync(); + } } /// diff --git a/src/OrchardCore/OrchardCore/Shell/DefaultShellReleaseManager.cs b/src/OrchardCore/OrchardCore/Shell/DefaultShellReleaseManager.cs index cbb12b69fc0..b76c3e53de4 100644 --- a/src/OrchardCore/OrchardCore/Shell/DefaultShellReleaseManager.cs +++ b/src/OrchardCore/OrchardCore/Shell/DefaultShellReleaseManager.cs @@ -1,42 +1,26 @@ -using Microsoft.Extensions.DependencyInjection; +using OrchardCore.Environment.Shell.Models; using OrchardCore.Environment.Shell.Scope; namespace OrchardCore.Environment.Shell; public class DefaultShellReleaseManager : IShellReleaseManager { - private bool _release; - private bool _deferredTaskAdded; - public void SuspendReleaseRequest() { - _release = false; + if (ShellScope.TryGetValue(ShellReleaseRequestContext.ShellScopeKey, out var context)) + { + context.Release = false; + + ShellScope.Set(ShellReleaseRequestContext.ShellScopeKey, context); + } } public void RequestRelease() { - _release = true; - - if (_deferredTaskAdded) - { - return; - } - - _deferredTaskAdded = true; - - ShellScope.AddDeferredTask(async scope => - { - if (!_release) + ShellScope.Set(ShellReleaseRequestContext.ShellScopeKey, + new ShellReleaseRequestContext() { - return; - } - - _release = false; - - var shellHost = scope.ServiceProvider.GetRequiredService(); - var shellSettings = scope.ServiceProvider.GetRequiredService(); - - await shellHost.ReleaseShellContextAsync(shellSettings); - }); + Release = true + }); } }