diff --git a/src/libraries/Microsoft.Extensions.Configuration/tests/FunctionalTests/ConfigurationTests.cs b/src/libraries/Microsoft.Extensions.Configuration/tests/FunctionalTests/ConfigurationTests.cs index 7c03f5f45f634..f9f8b2a62cf20 100644 --- a/src/libraries/Microsoft.Extensions.Configuration/tests/FunctionalTests/ConfigurationTests.cs +++ b/src/libraries/Microsoft.Extensions.Configuration/tests/FunctionalTests/ConfigurationTests.cs @@ -100,7 +100,7 @@ public void ThrowsOnFileNotFoundWhenNotIgnored() Assert.Throws(() => configurationBuilder.Build()); } - + [Fact] public void CanHandleExceptionIfFileNotFound() { @@ -689,7 +689,7 @@ await WaitForChange( Assert.Equal("IniValue1", config["Key"]); Assert.True(token.HasChanged); } - + [Theory] [ActiveIssue("File watching is flaky (particularly on non windows. https://github.com/dotnet/runtime/issues/33992")] [InlineData(false)] @@ -987,6 +987,41 @@ void ReloadLoop() } } + [Fact] + public async Task ReloadingFileFiresOnChangeEvent() + { + _fileSystem.WriteFile("reload.json", @"{""JsonKey1"": ""JsonValue1""}"); + _fileSystem.WriteFile("reload2.json", @"{""JsonKey2"": ""JsonValue2""}"); + + var config = CreateBuilder() + .AddJsonFile("./reload.json", optional: false, reloadOnChange: true) + .Build(); + + var config2 = CreateBuilder() + .AddJsonFile("reload2.json", optional: false, reloadOnChange: true) + .Build(); + + Assert.Equal("JsonValue1", config["JsonKey1"]); + Assert.Equal("JsonValue2", config2["JsonKey2"]); + + var token = config.GetReloadToken(); + var token2 = config2.GetReloadToken(); + + // Update files + _fileSystem.WriteFile("reload.json", @"{""JsonKey1"": ""JsonValue3""}"); + _fileSystem.WriteFile("reload2.json", @"{""JsonKey2"": ""JsonValue4""}"); + + await WaitForChange( + () => config["JsonKey1"] == "JsonValue3" + && config2["JsonKey2"] == "JsonValue4", + "Reload failed after touching files."); + + Assert.Equal("JsonValue3", config["JsonKey1"]); + Assert.Equal("JsonValue4", config2["JsonKey2"]); + Assert.True(token.HasChanged); + Assert.True(token2.HasChanged); + } + public void Dispose() { _fileProvider.Dispose(); diff --git a/src/libraries/Microsoft.Extensions.FileProviders.Physical/src/PhysicalFilesWatcher.cs b/src/libraries/Microsoft.Extensions.FileProviders.Physical/src/PhysicalFilesWatcher.cs index 89d9bf7eadce0..a714f066291c2 100644 --- a/src/libraries/Microsoft.Extensions.FileProviders.Physical/src/PhysicalFilesWatcher.cs +++ b/src/libraries/Microsoft.Extensions.FileProviders.Physical/src/PhysicalFilesWatcher.cs @@ -175,6 +175,10 @@ private IChangeToken GetOrAddChangeToken(string pattern) internal IChangeToken GetOrAddFilePathChangeToken(string filePath) { +#if NETCOREAPP + var absolutePath = Path.GetFullPath(filePath, _root); + filePath = absolutePath.Substring(_root.Length); +#endif if (!_filePathTokenLookup.TryGetValue(filePath, out ChangeTokenInfo tokenInfo)) { var cancellationTokenSource = new CancellationTokenSource();