From 9ee8f952728c1670be1c121755aa0afa2f285a3c Mon Sep 17 00:00:00 2001 From: Mike Alhayek Date: Fri, 8 Dec 2023 10:57:11 -0800 Subject: [PATCH] Dispose FileReader asynchronously --- .../OrchardCore.Media/Recipes/MediaStep.cs | 2 +- .../Modules/FileProviders/FileInfoExtensions.cs | 8 ++++++-- .../LiquidViewTemplate.cs | 8 +++----- .../OrchardCore.Recipes.Core/Services/RecipeExecutor.cs | 2 +- .../OrchardCore.Recipes.Core/Services/RecipeReader.cs | 2 +- test/OrchardCore.Tests/Utilities/FileInfoExtensions.cs | 9 +++++++-- 6 files changed, 19 insertions(+), 12 deletions(-) diff --git a/src/OrchardCore.Modules/OrchardCore.Media/Recipes/MediaStep.cs b/src/OrchardCore.Modules/OrchardCore.Media/Recipes/MediaStep.cs index 6234f883e10..7ffa0f2879f 100644 --- a/src/OrchardCore.Modules/OrchardCore.Media/Recipes/MediaStep.cs +++ b/src/OrchardCore.Modules/OrchardCore.Media/Recipes/MediaStep.cs @@ -79,7 +79,7 @@ public async Task ExecuteAsync(RecipeExecutionContext context) } finally { - stream?.Dispose(); + await stream.DisposeAsync(); } } } diff --git a/src/OrchardCore/OrchardCore.Abstractions/Modules/FileProviders/FileInfoExtensions.cs b/src/OrchardCore/OrchardCore.Abstractions/Modules/FileProviders/FileInfoExtensions.cs index 05495079278..8e3c9826b0c 100644 --- a/src/OrchardCore/OrchardCore.Abstractions/Modules/FileProviders/FileInfoExtensions.cs +++ b/src/OrchardCore/OrchardCore.Abstractions/Modules/FileProviders/FileInfoExtensions.cs @@ -1,5 +1,6 @@ using System.Collections.Generic; using System.IO; +using System.Threading.Tasks; using Microsoft.Extensions.FileProviders; namespace OrchardCore.Modules.FileProviders @@ -7,16 +8,19 @@ namespace OrchardCore.Modules.FileProviders public static class FileInfoExtensions { public static IEnumerable ReadAllLines(this IFileInfo fileInfo) + => ReadAllLinesAsync(fileInfo).GetAwaiter().GetResult(); + + public static async Task> ReadAllLinesAsync(this IFileInfo fileInfo) { var lines = new List(); if (fileInfo?.Exists ?? false) { - using var reader = fileInfo.CreateReadStream(); + await using var reader = fileInfo.CreateReadStream(); using var sr = new StreamReader(reader); string line; - while ((line = sr.ReadLine()) != null) + while ((line = await sr.ReadLineAsync()) != null) { lines.Add(line); } diff --git a/src/OrchardCore/OrchardCore.DisplayManagement.Liquid/LiquidViewTemplate.cs b/src/OrchardCore/OrchardCore.DisplayManagement.Liquid/LiquidViewTemplate.cs index c34b203c158..63be8f7837e 100644 --- a/src/OrchardCore/OrchardCore.DisplayManagement.Liquid/LiquidViewTemplate.cs +++ b/src/OrchardCore/OrchardCore.DisplayManagement.Liquid/LiquidViewTemplate.cs @@ -86,17 +86,15 @@ public static Task ParseAsync(LiquidViewParser parser, strin entry.ExpirationTokens.Add(fileProvider.Watch(path)); } - using var stream = fileInfo.CreateReadStream(); + await using var stream = fileInfo.CreateReadStream(); using var sr = new StreamReader(stream); if (parser.TryParse(await sr.ReadToEndAsync(), out var template, out var errors)) { return new LiquidViewTemplate(template); } - else - { - throw new Exception($"Failed to parse liquid file {path}: {string.Join(System.Environment.NewLine, errors)}"); - } + + throw new Exception($"Failed to parse liquid file {path}: {string.Join(System.Environment.NewLine, errors)}"); }); } } diff --git a/src/OrchardCore/OrchardCore.Recipes.Core/Services/RecipeExecutor.cs b/src/OrchardCore/OrchardCore.Recipes.Core/Services/RecipeExecutor.cs index d9a3c11a8aa..b9896f39a27 100644 --- a/src/OrchardCore/OrchardCore.Recipes.Core/Services/RecipeExecutor.cs +++ b/src/OrchardCore/OrchardCore.Recipes.Core/Services/RecipeExecutor.cs @@ -53,7 +53,7 @@ public async Task ExecuteAsync(string executionId, RecipeDescriptor reci var result = new RecipeResult { ExecutionId = executionId }; - using (var stream = recipeDescriptor.RecipeFileInfo.CreateReadStream()) + await using (var stream = recipeDescriptor.RecipeFileInfo.CreateReadStream()) { using var file = new StreamReader(stream); using var reader = new JsonTextReader(file); diff --git a/src/OrchardCore/OrchardCore.Recipes.Core/Services/RecipeReader.cs b/src/OrchardCore/OrchardCore.Recipes.Core/Services/RecipeReader.cs index b7f0d18c42f..ab9871837a2 100644 --- a/src/OrchardCore/OrchardCore.Recipes.Core/Services/RecipeReader.cs +++ b/src/OrchardCore/OrchardCore.Recipes.Core/Services/RecipeReader.cs @@ -13,7 +13,7 @@ public async Task GetRecipeDescriptorAsync(string recipeBasePa { // TODO: Try to optimize by only reading the required metadata instead of the whole file. - using var stream = recipeFileInfo.CreateReadStream(); + await using var stream = recipeFileInfo.CreateReadStream(); using var reader = new StreamReader(stream); using var jsonReader = new JsonTextReader(reader); diff --git a/test/OrchardCore.Tests/Utilities/FileInfoExtensions.cs b/test/OrchardCore.Tests/Utilities/FileInfoExtensions.cs index 618909f7e65..5eab13e5f95 100644 --- a/test/OrchardCore.Tests/Utilities/FileInfoExtensions.cs +++ b/test/OrchardCore.Tests/Utilities/FileInfoExtensions.cs @@ -3,10 +3,15 @@ namespace OrchardCore.Tests.Utilities public static class FileInfoExtensions { public static string ReadToEnd(this IFileInfo fileInfo) + => ReadToEndAsync(fileInfo).GetAwaiter().GetResult(); + + public static async Task ReadToEndAsync(this IFileInfo fileInfo) { - using var stream = fileInfo.CreateReadStream(); + await using var stream = fileInfo.CreateReadStream(); + using var streamReader = new StreamReader(stream); - return streamReader.ReadToEnd(); + + return await streamReader.ReadToEndAsync(); } } }