Port config fix to 2.2 (dotnet/extensions#1221)

- port of dotnet/extensions#1202
- with PR tweaks for 2.2
  - e.g. adjust Microsoft.Extensions.Configuration.FunctionalTests.csproj to match layout here
- update PatchConfig.props and NuGetPackageVerifier.json\n\nCommit migrated from 9ebff1a64e
This commit is contained in:
Hao Kung 2019-03-14 21:14:45 -07:00 committed by Doug Bunting
parent ec5506c67a
commit 1bfa807e48
3 changed files with 41 additions and 2 deletions

View File

@ -31,12 +31,13 @@ namespace Microsoft.Extensions.Configuration.KeyPerFile
/// </summary>
public override void Load()
{
Data = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
var data = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
if (Source.FileProvider == null)
{
if (Source.Optional)
{
Data = data;
return;
}
else
@ -63,10 +64,12 @@ namespace Microsoft.Extensions.Configuration.KeyPerFile
{
if (Source.IgnoreCondition == null || !Source.IgnoreCondition(file.Name))
{
Data.Add(NormalizeKey(file.Name), TrimNewLine(streamReader.ReadToEnd()));
data.Add(NormalizeKey(file.Name), TrimNewLine(streamReader.ReadToEnd()));
}
}
}
Data = data;
}
}
}

View File

@ -4,6 +4,8 @@ using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.FileProviders;
using Microsoft.Extensions.Primitives;
using Xunit;
@ -177,6 +179,39 @@ namespace Microsoft.Extensions.Configuration.KeyPerFile.Test
Assert.Equal("SecretValue1", config["ignore.Secret1"]);
Assert.Equal("SecretValue2", config["Secret2"]);
}
[Fact]
public void BindingDoesNotThrowIfReloadedDuringBinding()
{
var testFileProvider = new TestFileProvider(
new TestFile("Number", "-2"),
new TestFile("Text", "Foo"));
var config = new ConfigurationBuilder()
.AddKeyPerFile(o => o.FileProvider = testFileProvider)
.Build();
MyOptions options = null;
using (var cts = new CancellationTokenSource(TimeSpan.FromMilliseconds(250)))
{
_ = Task.Run(() => { while (!cts.IsCancellationRequested) config.Reload(); });
while (!cts.IsCancellationRequested)
{
options = config.Get<MyOptions>();
}
}
Assert.Equal(-2, options.Number);
Assert.Equal("Foo", options.Text);
}
private sealed class MyOptions
{
public int Number { get; set; }
public string Text { get; set; }
}
}
class TestFileProvider : IFileProvider

View File

@ -5,6 +5,7 @@
</PropertyGroup>
<ItemGroup>
<Reference Include="Microsoft.Extensions.Configuration.Binder" />
<Reference Include="Microsoft.Extensions.Configuration.KeyPerFile" />
</ItemGroup>