Skip to content

Commit 5691c30

Browse files
HaoKdougbu
authored andcommitted
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 dotnet/extensions@9ebff1a
1 parent d14a080 commit 5691c30

File tree

3 files changed

+41
-2
lines changed

3 files changed

+41
-2
lines changed

src/Configuration/Config.KeyPerFile/src/KeyPerFileConfigurationProvider.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,13 @@ private static string TrimNewLine(string value)
3131
/// </summary>
3232
public override void Load()
3333
{
34-
Data = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
34+
var data = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
3535

3636
if (Source.FileProvider == null)
3737
{
3838
if (Source.Optional)
3939
{
40+
Data = data;
4041
return;
4142
}
4243
else
@@ -63,10 +64,12 @@ public override void Load()
6364
{
6465
if (Source.IgnoreCondition == null || !Source.IgnoreCondition(file.Name))
6566
{
66-
Data.Add(NormalizeKey(file.Name), TrimNewLine(streamReader.ReadToEnd()));
67+
data.Add(NormalizeKey(file.Name), TrimNewLine(streamReader.ReadToEnd()));
6768
}
6869
}
6970
}
71+
72+
Data = data;
7073
}
7174
}
7275
}

src/Configuration/Config.KeyPerFile/test/KeyPerFileTests.cs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
using System.IO;
55
using System.Linq;
66
using System.Text;
7+
using System.Threading;
8+
using System.Threading.Tasks;
79
using Microsoft.Extensions.FileProviders;
810
using Microsoft.Extensions.Primitives;
911
using Xunit;
@@ -177,6 +179,39 @@ public void CanUnIgnoreDefaultFiles()
177179
Assert.Equal("SecretValue1", config["ignore.Secret1"]);
178180
Assert.Equal("SecretValue2", config["Secret2"]);
179181
}
182+
183+
[Fact]
184+
public void BindingDoesNotThrowIfReloadedDuringBinding()
185+
{
186+
var testFileProvider = new TestFileProvider(
187+
new TestFile("Number", "-2"),
188+
new TestFile("Text", "Foo"));
189+
190+
var config = new ConfigurationBuilder()
191+
.AddKeyPerFile(o => o.FileProvider = testFileProvider)
192+
.Build();
193+
194+
MyOptions options = null;
195+
196+
using (var cts = new CancellationTokenSource(TimeSpan.FromMilliseconds(250)))
197+
{
198+
_ = Task.Run(() => { while (!cts.IsCancellationRequested) config.Reload(); });
199+
200+
while (!cts.IsCancellationRequested)
201+
{
202+
options = config.Get<MyOptions>();
203+
}
204+
}
205+
206+
Assert.Equal(-2, options.Number);
207+
Assert.Equal("Foo", options.Text);
208+
}
209+
210+
private sealed class MyOptions
211+
{
212+
public int Number { get; set; }
213+
public string Text { get; set; }
214+
}
180215
}
181216

182217
class TestFileProvider : IFileProvider

src/Configuration/Config.KeyPerFile/test/Microsoft.Extensions.Configuration.KeyPerFile.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
</PropertyGroup>
66

77
<ItemGroup>
8+
<Reference Include="Microsoft.Extensions.Configuration.Binder" />
89
<Reference Include="Microsoft.Extensions.Configuration.KeyPerFile" />
910
</ItemGroup>
1011

0 commit comments

Comments
 (0)