diff --git a/src/OrchardCore/OrchardCore.Abstractions/Shell/Scope/ShellScope.cs b/src/OrchardCore/OrchardCore.Abstractions/Shell/Scope/ShellScope.cs index 6a7ae55a7db..52d67412d50 100644 --- a/src/OrchardCore/OrchardCore.Abstractions/Shell/Scope/ShellScope.cs +++ b/src/OrchardCore/OrchardCore.Abstractions/Shell/Scope/ShellScope.cs @@ -187,7 +187,7 @@ public static async Task UsingChildScopeAsync(string tenant, Func /// Execute a delegate using this shell scope. /// - public async Task UsingAsync(Func execute) + public async Task UsingAsync(Func execute, bool activateShell = true) { if (Current == this) { @@ -199,7 +199,10 @@ public async Task UsingAsync(Func execute) { StartAsyncFlow(); - await ActivateShellAsync(); + if (activateShell) + { + await ActivateShellAsync(); + } await execute(this); diff --git a/src/OrchardCore/OrchardCore.Data/SessionHelper.cs b/src/OrchardCore/OrchardCore.Data/SessionHelper.cs index ee6ada633e9..463f936f093 100644 --- a/src/OrchardCore/OrchardCore.Data/SessionHelper.cs +++ b/src/OrchardCore/OrchardCore.Data/SessionHelper.cs @@ -43,7 +43,7 @@ public SessionHelper(ISession session) { if (_loaded.TryGetValue(typeof(T), out var loaded)) { - _session.Detach(loaded); + throw new ArgumentException("Can't get for caching an object being mutated in the same scope"); } var document = await _session.Query().FirstOrDefaultAsync(); diff --git a/src/OrchardCore/OrchardCore.Recipes.Abstractions/Models/RecipeDescriptor.cs b/src/OrchardCore/OrchardCore.Recipes.Abstractions/Models/RecipeDescriptor.cs index 2d2235c0ffe..04d5f191942 100644 --- a/src/OrchardCore/OrchardCore.Recipes.Abstractions/Models/RecipeDescriptor.cs +++ b/src/OrchardCore/OrchardCore.Recipes.Abstractions/Models/RecipeDescriptor.cs @@ -15,7 +15,7 @@ public class RecipeDescriptor public DateTime? ExportUtc { get; set; } public string[] Categories { get; set; } public string[] Tags { get; set; } - public bool RequireNewScope { get; set; } = true; + public bool ActivateShell { get; set; } = true; /// /// The path of the recipe file for the property diff --git a/src/OrchardCore/OrchardCore.Recipes.Core/Services/RecipeExecutor.cs b/src/OrchardCore/OrchardCore.Recipes.Core/Services/RecipeExecutor.cs index 28ed023b48f..418369e556a 100644 --- a/src/OrchardCore/OrchardCore.Recipes.Core/Services/RecipeExecutor.cs +++ b/src/OrchardCore/OrchardCore.Recipes.Core/Services/RecipeExecutor.cs @@ -9,7 +9,6 @@ using Newtonsoft.Json; using Newtonsoft.Json.Linq; using OrchardCore.Environment.Shell; -using OrchardCore.Environment.Shell.Scope; using OrchardCore.Modules; using OrchardCore.Recipes.Events; using OrchardCore.Recipes.Models; @@ -147,9 +146,7 @@ public async Task ExecuteAsync(string executionId, RecipeDescriptor reci private async Task ExecuteStepAsync(RecipeExecutionContext recipeStep) { - var shellScope = recipeStep.RecipeDescriptor.RequireNewScope - ? await _shellHost.GetScopeAsync(_shellSettings) - : ShellScope.Current; + var shellScope = await _shellHost.GetScopeAsync(_shellSettings); await shellScope.UsingAsync(async scope => { @@ -179,7 +176,8 @@ await shellScope.UsingAsync(async scope => _logger.LogInformation("Finished executing recipe step '{RecipeName}'.", recipeStep.Name); } } - }); + }, + activateShell: recipeStep.RecipeDescriptor.ActivateShell); } /// diff --git a/src/OrchardCore/OrchardCore.Recipes.Core/Services/RecipeMigrator.cs b/src/OrchardCore/OrchardCore.Recipes.Core/Services/RecipeMigrator.cs index 5b54907c888..ceccea95329 100644 --- a/src/OrchardCore/OrchardCore.Recipes.Core/Services/RecipeMigrator.cs +++ b/src/OrchardCore/OrchardCore.Recipes.Core/Services/RecipeMigrator.cs @@ -5,6 +5,7 @@ using Microsoft.Extensions.Hosting; using OrchardCore.Data.Migration; using OrchardCore.Environment.Extensions; +using OrchardCore.Environment.Shell.Scope; namespace OrchardCore.Recipes.Services { @@ -35,10 +36,16 @@ public async Task ExecuteAsync(string recipeFileName, IDataMigration mig var recipeFilePath = Path.Combine(recipeBasePath, recipeFileName).Replace('\\', '/'); var recipeFileInfo = _hostingEnvironment.ContentRootFileProvider.GetFileInfo(recipeFilePath); var recipeDescriptor = await _recipeReader.GetRecipeDescriptor(recipeBasePath, recipeFileInfo, _hostingEnvironment.ContentRootFileProvider); - recipeDescriptor.RequireNewScope = false; + recipeDescriptor.ActivateShell = false; var executionId = Guid.NewGuid().ToString("n"); - return await _recipeExecutor.ExecuteAsync(executionId, recipeDescriptor, new object(), CancellationToken.None); + + ShellScope.AddDeferredTask(scope => + { + return _recipeExecutor.ExecuteAsync(executionId, recipeDescriptor, new object(), CancellationToken.None); + }); + + return executionId; } } }