-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #293 from Lombiq/issue/OFFI-132
OFFI-132: Add ShellHostExtensions
- Loading branch information
Showing
3 changed files
with
43 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
Lombiq.HelpfulLibraries.OrchardCore/DependencyInjection/ShellHostExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
using OrchardCore.Environment.Shell.Scope; | ||
using System; | ||
using System.Threading.Tasks; | ||
|
||
namespace OrchardCore.Environment.Shell; | ||
|
||
public static class ShellHostExtensions | ||
{ | ||
/// <summary> | ||
/// Executes <paramref name="asyncAction"/> in the specified shell's scope. | ||
/// </summary> | ||
public static async Task WithShellScopeAsync( | ||
this IShellHost shellHost, | ||
Func<ShellScope, Task> asyncAction, | ||
string scopeName = ShellSettings.DefaultShellName) | ||
{ | ||
await using var shellScope = await shellHost.GetScopeAsync(scopeName); | ||
await shellScope.UsingAsync(asyncAction); | ||
} | ||
|
||
/// <summary> | ||
/// Executes <paramref name="asyncFunc"/> in the specified shell's scope and returns the resulting object. | ||
/// </summary> | ||
public static async Task<T> GetWithShellScopeAsync<T>( | ||
this IShellHost shellHost, | ||
Func<ShellScope, Task<T>> asyncFunc, | ||
string scopeName = ShellSettings.DefaultShellName) | ||
{ | ||
T result = default; | ||
|
||
await shellHost.WithShellScopeAsync(async scope => result = await asyncFunc(scope), scopeName); | ||
|
||
return result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters