From dcb3573df6c41fdcc41b1b833f04752fdb2a1e32 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A1ra=20El-Saig?= Date: Sat, 2 Nov 2024 14:28:36 +0100 Subject: [PATCH 1/3] Add ShellHostExtensions. --- .../ServiceProviderExtensions.cs | 22 ++++-------- .../ShellHostExtensions.cs | 35 +++++++++++++++++++ 2 files changed, 41 insertions(+), 16 deletions(-) create mode 100644 Lombiq.HelpfulLibraries.OrchardCore/DependencyInjection/ShellHostExtensions.cs diff --git a/Lombiq.HelpfulLibraries.OrchardCore/DependencyInjection/ServiceProviderExtensions.cs b/Lombiq.HelpfulLibraries.OrchardCore/DependencyInjection/ServiceProviderExtensions.cs index fb9686d9..9f2acaa5 100644 --- a/Lombiq.HelpfulLibraries.OrchardCore/DependencyInjection/ServiceProviderExtensions.cs +++ b/Lombiq.HelpfulLibraries.OrchardCore/DependencyInjection/ServiceProviderExtensions.cs @@ -10,28 +10,18 @@ public static class ServiceProviderExtensions /// /// Executes in the specified shell's scope. /// - public static async Task WithShellScopeAsync( + public static Task WithShellScopeAsync( this IServiceProvider serviceProvider, Func asyncAction, - string scopeName = "Default") - { - var shellHost = serviceProvider.GetRequiredService(); - var shellScope = await shellHost.GetScopeAsync(scopeName); - await shellScope.UsingAsync(asyncAction); - } + string scopeName = "Default") => + serviceProvider.GetRequiredService().WithShellScopeAsync(asyncAction, scopeName); /// /// Executes in the specified shell's scope and returns the resulting object. /// - public static async Task GetWithShellScopeAsync( + public static Task GetWithShellScopeAsync( this IServiceProvider serviceProvider, Func> asyncFunc, - string scopeName = "Default") - { - T result = default; - - await serviceProvider.WithShellScopeAsync(async scope => result = await asyncFunc(scope), scopeName); - - return result; - } + string scopeName = "Default") => + serviceProvider.GetRequiredService().GetWithShellScopeAsync(asyncFunc, scopeName); } diff --git a/Lombiq.HelpfulLibraries.OrchardCore/DependencyInjection/ShellHostExtensions.cs b/Lombiq.HelpfulLibraries.OrchardCore/DependencyInjection/ShellHostExtensions.cs new file mode 100644 index 00000000..1b6c86b8 --- /dev/null +++ b/Lombiq.HelpfulLibraries.OrchardCore/DependencyInjection/ShellHostExtensions.cs @@ -0,0 +1,35 @@ +using OrchardCore.Environment.Shell.Scope; +using System; +using System.Threading.Tasks; + +namespace OrchardCore.Environment.Shell; + +public static class ShellHostExtensions +{ + /// + /// Executes in the specified shell's scope. + /// + public static async Task WithShellScopeAsync( + this IShellHost shellHost, + Func asyncAction, + string scopeName = "Default") + { + var shellScope = await shellHost.GetScopeAsync(scopeName); + await shellScope.UsingAsync(asyncAction); + } + + /// + /// Executes in the specified shell's scope and returns the resulting object. + /// + public static async Task GetWithShellScopeAsync( + this IShellHost shellHost, + Func> asyncFunc, + string scopeName = "Default") + { + T result = default; + + await shellHost.WithShellScopeAsync(async scope => result = await asyncFunc(scope), scopeName); + + return result; + } +} From 0f8bbfb0b733fc6f4350728731f7d614edd5a38a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A1ra=20El-Saig?= Date: Sat, 2 Nov 2024 14:29:07 +0100 Subject: [PATCH 2/3] Fix missing using (potential memory leak) in original snippet. --- .../DependencyInjection/ShellHostExtensions.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lombiq.HelpfulLibraries.OrchardCore/DependencyInjection/ShellHostExtensions.cs b/Lombiq.HelpfulLibraries.OrchardCore/DependencyInjection/ShellHostExtensions.cs index 1b6c86b8..d9a36ea4 100644 --- a/Lombiq.HelpfulLibraries.OrchardCore/DependencyInjection/ShellHostExtensions.cs +++ b/Lombiq.HelpfulLibraries.OrchardCore/DependencyInjection/ShellHostExtensions.cs @@ -14,7 +14,7 @@ public static async Task WithShellScopeAsync( Func asyncAction, string scopeName = "Default") { - var shellScope = await shellHost.GetScopeAsync(scopeName); + await using var shellScope = await shellHost.GetScopeAsync(scopeName); await shellScope.UsingAsync(asyncAction); } From 72422e636fd8ec122cb7e1878dd8a5385c195d5f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A1ra=20El-Saig?= Date: Sat, 2 Nov 2024 14:31:48 +0100 Subject: [PATCH 3/3] Use constant for default tenant name. --- .../DependencyInjection/ServiceProviderExtensions.cs | 4 ++-- .../DependencyInjection/ShellHostExtensions.cs | 4 ++-- Lombiq.HelpfulLibraries.OrchardCore/Mvc/TypedRoute.cs | 3 ++- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/Lombiq.HelpfulLibraries.OrchardCore/DependencyInjection/ServiceProviderExtensions.cs b/Lombiq.HelpfulLibraries.OrchardCore/DependencyInjection/ServiceProviderExtensions.cs index 9f2acaa5..4371d5b0 100644 --- a/Lombiq.HelpfulLibraries.OrchardCore/DependencyInjection/ServiceProviderExtensions.cs +++ b/Lombiq.HelpfulLibraries.OrchardCore/DependencyInjection/ServiceProviderExtensions.cs @@ -13,7 +13,7 @@ public static class ServiceProviderExtensions public static Task WithShellScopeAsync( this IServiceProvider serviceProvider, Func asyncAction, - string scopeName = "Default") => + string scopeName = ShellSettings.DefaultShellName) => serviceProvider.GetRequiredService().WithShellScopeAsync(asyncAction, scopeName); /// @@ -22,6 +22,6 @@ public static Task WithShellScopeAsync( public static Task GetWithShellScopeAsync( this IServiceProvider serviceProvider, Func> asyncFunc, - string scopeName = "Default") => + string scopeName = ShellSettings.DefaultShellName) => serviceProvider.GetRequiredService().GetWithShellScopeAsync(asyncFunc, scopeName); } diff --git a/Lombiq.HelpfulLibraries.OrchardCore/DependencyInjection/ShellHostExtensions.cs b/Lombiq.HelpfulLibraries.OrchardCore/DependencyInjection/ShellHostExtensions.cs index d9a36ea4..b68a98cb 100644 --- a/Lombiq.HelpfulLibraries.OrchardCore/DependencyInjection/ShellHostExtensions.cs +++ b/Lombiq.HelpfulLibraries.OrchardCore/DependencyInjection/ShellHostExtensions.cs @@ -12,7 +12,7 @@ public static class ShellHostExtensions public static async Task WithShellScopeAsync( this IShellHost shellHost, Func asyncAction, - string scopeName = "Default") + string scopeName = ShellSettings.DefaultShellName) { await using var shellScope = await shellHost.GetScopeAsync(scopeName); await shellScope.UsingAsync(asyncAction); @@ -24,7 +24,7 @@ public static async Task WithShellScopeAsync( public static async Task GetWithShellScopeAsync( this IShellHost shellHost, Func> asyncFunc, - string scopeName = "Default") + string scopeName = ShellSettings.DefaultShellName) { T result = default; diff --git a/Lombiq.HelpfulLibraries.OrchardCore/Mvc/TypedRoute.cs b/Lombiq.HelpfulLibraries.OrchardCore/Mvc/TypedRoute.cs index 594584d8..7ba71024 100644 --- a/Lombiq.HelpfulLibraries.OrchardCore/Mvc/TypedRoute.cs +++ b/Lombiq.HelpfulLibraries.OrchardCore/Mvc/TypedRoute.cs @@ -6,6 +6,7 @@ using Microsoft.Extensions.Options; using OrchardCore.Admin; using OrchardCore.Environment.Extensions; +using OrchardCore.Environment.Shell; using OrchardCore.Modules.Manifest; using OrchardCore.Mvc.Core.Utilities; using System; @@ -111,7 +112,7 @@ public override string ToString() /// route, and other arguments. /// public string ToString(string tenantName) => - string.IsNullOrWhiteSpace(tenantName) || tenantName.EqualsOrdinalIgnoreCase("Default") + string.IsNullOrWhiteSpace(tenantName) || tenantName.EqualsOrdinalIgnoreCase(ShellSettings.DefaultShellName) ? ToString() : $"/{tenantName}{this}";