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:
parent
ec5506c67a
commit
1bfa807e48
|
|
@ -31,12 +31,13 @@ namespace Microsoft.Extensions.Configuration.KeyPerFile
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public override void Load()
|
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.FileProvider == null)
|
||||||
{
|
{
|
||||||
if (Source.Optional)
|
if (Source.Optional)
|
||||||
{
|
{
|
||||||
|
Data = data;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
|
@ -63,10 +64,12 @@ namespace Microsoft.Extensions.Configuration.KeyPerFile
|
||||||
{
|
{
|
||||||
if (Source.IgnoreCondition == null || !Source.IgnoreCondition(file.Name))
|
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,8 @@ using System.Collections.Generic;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
using System.Threading;
|
||||||
|
using System.Threading.Tasks;
|
||||||
using Microsoft.Extensions.FileProviders;
|
using Microsoft.Extensions.FileProviders;
|
||||||
using Microsoft.Extensions.Primitives;
|
using Microsoft.Extensions.Primitives;
|
||||||
using Xunit;
|
using Xunit;
|
||||||
|
|
@ -177,6 +179,39 @@ namespace Microsoft.Extensions.Configuration.KeyPerFile.Test
|
||||||
Assert.Equal("SecretValue1", config["ignore.Secret1"]);
|
Assert.Equal("SecretValue1", config["ignore.Secret1"]);
|
||||||
Assert.Equal("SecretValue2", config["Secret2"]);
|
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
|
class TestFileProvider : IFileProvider
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,7 @@
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<Reference Include="Microsoft.Extensions.Configuration.Binder" />
|
||||||
<Reference Include="Microsoft.Extensions.Configuration.KeyPerFile" />
|
<Reference Include="Microsoft.Extensions.Configuration.KeyPerFile" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue