|
4 | 4 | using System; |
5 | 5 | using System.Globalization; |
6 | 6 | using System.IO; |
| 7 | +using System.Linq; |
7 | 8 | using Microsoft.Extensions.Configuration.Json; |
8 | 9 | using Microsoft.Extensions.Configuration.Test; |
| 10 | +using Microsoft.Extensions.FileProviders; |
9 | 11 | using Xunit; |
10 | 12 |
|
11 | 13 | namespace Microsoft.Extensions.Configuration |
@@ -219,5 +221,34 @@ public void ThrowFormatExceptionWhenFileIsEmpty() |
219 | 221 | var exception = Assert.Throws<FormatException>(() => LoadProvider(@"")); |
220 | 222 | Assert.Contains("Could not parse the JSON file.", exception.Message); |
221 | 223 | } |
| 224 | + |
| 225 | + [Fact] |
| 226 | + public void AddJsonFile_FileProvider_Is_Not_Disposed_When_SourcesGetReloaded() |
| 227 | + { |
| 228 | + string filePath = Path.Combine(Path.GetTempPath(), $"{nameof(AddJsonFile_FileProvider_Is_Not_Disposed_When_SourcesGetReloaded)}.json"); |
| 229 | + File.WriteAllText(filePath, @"{ ""some"": ""value"" }"); |
| 230 | + |
| 231 | + IConfigurationBuilder builder = new ConfigurationManager(); |
| 232 | + |
| 233 | + builder.AddJsonFile(filePath, optional: false); |
| 234 | + |
| 235 | + FileConfigurationSource fileConfigurationSource = (FileConfigurationSource)builder.Sources.Last(); |
| 236 | + PhysicalFileProvider fileProvider = (PhysicalFileProvider)fileConfigurationSource.FileProvider; |
| 237 | + |
| 238 | + Assert.False(GetIsDisposed(fileProvider)); |
| 239 | + |
| 240 | + builder.Properties.Add("simplest", "repro"); |
| 241 | + |
| 242 | + Assert.False(GetIsDisposed(fileProvider)); |
| 243 | + |
| 244 | + fileProvider.Dispose(); |
| 245 | + Assert.True(GetIsDisposed(fileProvider)); |
| 246 | + } |
| 247 | + |
| 248 | + private static bool GetIsDisposed(PhysicalFileProvider fileProvider) |
| 249 | + { |
| 250 | + System.Reflection.FieldInfo isDisposedField = typeof(PhysicalFileProvider).GetField("_disposed", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic); |
| 251 | + return (bool)isDisposedField.GetValue(fileProvider); |
| 252 | + } |
222 | 253 | } |
223 | 254 | } |
0 commit comments