diff --git a/src/OrchardCore/OrchardCore.Abstractions/Shell/Scope/ShellScope.cs b/src/OrchardCore/OrchardCore.Abstractions/Shell/Scope/ShellScope.cs
index cf2889fd8a7..1455015082a 100644
--- a/src/OrchardCore/OrchardCore.Abstractions/Shell/Scope/ShellScope.cs
+++ b/src/OrchardCore/OrchardCore.Abstractions/Shell/Scope/ShellScope.cs
@@ -376,7 +376,22 @@ internal async Task ActivateShellInternalAsync()
///
/// Registers a delegate to be invoked when 'BeforeDisposeAsync()' is called on this scope.
///
- internal void BeforeDispose(Func callback) => (_beforeDispose ??= []).Insert(0, callback);
+ /// The delegate to be invoked before disposal. This delegate takes a parameter and returns a .
+ /// A boolean value indicating whether the delegate should be invoked last.
+ /// If true, the delegate is added to the end of the invocation list; otherwise, it is added to the beginning.
+ internal void BeforeDispose(Func callback, bool last)
+ {
+ var list = _beforeDispose ??= [];
+
+ if (last)
+ {
+ list.Add(callback);
+ }
+ else
+ {
+ list.Insert(0, callback);
+ }
+ }
///
/// Adds a Signal (if not already present) to be sent just after 'BeforeDisposeAsync()'.
@@ -396,7 +411,7 @@ internal async Task ActivateShellInternalAsync()
///
/// Registers a delegate to be invoked before the current shell scope will be disposed.
///
- public static void RegisterBeforeDispose(Func callback) => Current?.BeforeDispose(callback);
+ public static void RegisterBeforeDispose(Func callback, bool last = false) => Current?.BeforeDispose(callback, last);
///
/// Adds a Signal (if not already present) to be sent just before the current shell scope will be disposed.
diff --git a/src/OrchardCore/OrchardCore.Abstractions/Shell/Scope/ShellScopeExtensions.cs b/src/OrchardCore/OrchardCore.Abstractions/Shell/Scope/ShellScopeExtensions.cs
index 4c91df15f42..a2625ebb192 100644
--- a/src/OrchardCore/OrchardCore.Abstractions/Shell/Scope/ShellScopeExtensions.cs
+++ b/src/OrchardCore/OrchardCore.Abstractions/Shell/Scope/ShellScopeExtensions.cs
@@ -8,22 +8,33 @@ public static class ShellScopeExtensions
///
/// Registers a delegate task to be invoked before this shell scope will be disposed.
///
- public static ShellScope RegisterBeforeDispose(this ShellScope scope, Func callback)
+ /// The instance on which to register the delegate.
+ /// The delegate task to be invoked before disposal. This delegate takes a parameter and returns a .
+ /// A boolean value indicating whether the delegate should be invoked last.
+ /// If true, the delegate is added to the end of the invocation list; otherwise, it is added to the beginning. The default value is false.
+ /// The instance for chaining further calls.
+ public static ShellScope RegisterBeforeDispose(this ShellScope scope, Func callback, bool last = false)
{
- scope?.BeforeDispose(callback);
+ scope?.BeforeDispose(callback, last);
+
return scope;
}
///
/// Registers a delegate action to be invoked before this shell scope will be disposed.
///
- public static ShellScope RegisterBeforeDispose(this ShellScope scope, Action callback)
+ /// The instance on which to register the delegate.
+ /// The delegate action to be invoked before disposal. This delegate takes a parameter.
+ /// A boolean value indicating whether the delegate should be invoked last.
+ /// If true, the delegate is added to the end of the invocation list; otherwise, it is added to the beginning. The default value is false.
+ /// The instance for chaining further calls.
+ public static ShellScope RegisterBeforeDispose(this ShellScope scope, Action callback, bool last = false)
{
scope?.BeforeDispose(scope =>
{
callback(scope);
return Task.CompletedTask;
- });
+ }, last);
return scope;
}