diff --git a/src/Mockaco/Mockaco.csproj b/src/Mockaco/Mockaco.csproj index 39269ad..a701726 100644 --- a/src/Mockaco/Mockaco.csproj +++ b/src/Mockaco/Mockaco.csproj @@ -13,6 +13,7 @@ + diff --git a/src/Mockaco/Templating/Providers/TemplateFileProvider.cs b/src/Mockaco/Templating/Providers/TemplateFileProvider.cs index 5ffbb1f..83bb1fe 100644 --- a/src/Mockaco/Templating/Providers/TemplateFileProvider.cs +++ b/src/Mockaco/Templating/Providers/TemplateFileProvider.cs @@ -2,6 +2,8 @@ using Microsoft.Extensions.FileProviders; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Primitives; +using Polly; +using Polly.Retry; using System; using System.Collections.Generic; using System.IO; @@ -14,6 +16,18 @@ public sealed class TemplateFileProvider : ITemplateProvider, IDisposable { public event EventHandler OnChange; + private const string DefaultTemplateFolder = "Mocks"; + private const string DefaultTemplateSearchPattern = "*.json"; + + private static readonly RetryPolicy _retryPolicy = Policy + .Handle() + .WaitAndRetry(new[] + { + TimeSpan.FromSeconds(1), + TimeSpan.FromSeconds(2), + TimeSpan.FromSeconds(3) + }); + private readonly ILogger _logger; private readonly PhysicalFileProvider _fileProvider; private readonly IMemoryCache _memoryCache; @@ -26,12 +40,12 @@ public TemplateFileProvider(IMemoryCache memoryCache, ILogger LoadTemplatesFromDirectory() { - var directory = new DirectoryInfo("Mocks"); + var directory = new DirectoryInfo(DefaultTemplateFolder); - foreach (var file in directory.GetFiles("*.json", SearchOption.AllDirectories) + foreach (var file in directory.GetFiles(DefaultTemplateSearchPattern, SearchOption.AllDirectories) .OrderBy(f => f.FullName)) { var name = Path.GetRelativePath(directory.FullName, file.FullName); + var rawContent = string.Empty; - var rawContent = File.ReadAllText(file.FullName); + _retryPolicy.Execute(() => + { + using (var stream = File.Open(file.FullName, FileMode.Open, FileAccess.Read, FileShare.Read)) + using (var streamReader = new StreamReader(stream)) + { + rawContent = streamReader.ReadToEnd(); + } + }); yield return new RawTemplate(name, rawContent); }