diff --git a/src/libraries/Microsoft.Extensions.Configuration.FileExtensions/ref/Microsoft.Extensions.Configuration.FileExtensions.cs b/src/libraries/Microsoft.Extensions.Configuration.FileExtensions/ref/Microsoft.Extensions.Configuration.FileExtensions.cs index 9314e23ef311e..57e46ec6dcac2 100644 --- a/src/libraries/Microsoft.Extensions.Configuration.FileExtensions/ref/Microsoft.Extensions.Configuration.FileExtensions.cs +++ b/src/libraries/Microsoft.Extensions.Configuration.FileExtensions/ref/Microsoft.Extensions.Configuration.FileExtensions.cs @@ -8,7 +8,7 @@ namespace Microsoft.Extensions.Configuration { public static partial class FileConfigurationExtensions { - public static System.Action GetFileLoadExceptionHandler(this Microsoft.Extensions.Configuration.IConfigurationBuilder builder) { throw null; } + public static System.Action? GetFileLoadExceptionHandler(this Microsoft.Extensions.Configuration.IConfigurationBuilder builder) { throw null; } public static Microsoft.Extensions.FileProviders.IFileProvider GetFileProvider(this Microsoft.Extensions.Configuration.IConfigurationBuilder builder) { throw null; } public static Microsoft.Extensions.Configuration.IConfigurationBuilder SetBasePath(this Microsoft.Extensions.Configuration.IConfigurationBuilder builder, string basePath) { throw null; } public static Microsoft.Extensions.Configuration.IConfigurationBuilder SetFileLoadExceptionHandler(this Microsoft.Extensions.Configuration.IConfigurationBuilder builder, System.Action handler) { throw null; } @@ -27,10 +27,11 @@ public override void Load() { } public abstract partial class FileConfigurationSource : Microsoft.Extensions.Configuration.IConfigurationSource { protected FileConfigurationSource() { } - public Microsoft.Extensions.FileProviders.IFileProvider FileProvider { get { throw null; } set { } } - public System.Action OnLoadException { get { throw null; } set { } } + public Microsoft.Extensions.FileProviders.IFileProvider? FileProvider { get { throw null; } set { } } + public System.Action? OnLoadException { get { throw null; } set { } } public bool Optional { get { throw null; } set { } } - public string Path { get { throw null; } set { } } + [System.Diagnostics.CodeAnalysis.DisallowNull] + public string? Path { get { throw null; } set { } } public int ReloadDelay { get { throw null; } set { } } public bool ReloadOnChange { get { throw null; } set { } } public abstract Microsoft.Extensions.Configuration.IConfigurationProvider Build(Microsoft.Extensions.Configuration.IConfigurationBuilder builder); diff --git a/src/libraries/Microsoft.Extensions.Configuration.FileExtensions/ref/Microsoft.Extensions.Configuration.FileExtensions.csproj b/src/libraries/Microsoft.Extensions.Configuration.FileExtensions/ref/Microsoft.Extensions.Configuration.FileExtensions.csproj index f7a69d4e76c85..2e8aa6b1a125f 100644 --- a/src/libraries/Microsoft.Extensions.Configuration.FileExtensions/ref/Microsoft.Extensions.Configuration.FileExtensions.csproj +++ b/src/libraries/Microsoft.Extensions.Configuration.FileExtensions/ref/Microsoft.Extensions.Configuration.FileExtensions.csproj @@ -1,6 +1,8 @@ + $(NetCoreAppCurrent);$(NetCoreAppMinimum);netstandard2.0;$(NetFrameworkMinimum) + enable diff --git a/src/libraries/Microsoft.Extensions.Configuration.FileExtensions/src/FileConfigurationExtensions.cs b/src/libraries/Microsoft.Extensions.Configuration.FileExtensions/src/FileConfigurationExtensions.cs index 4ce12bd020caf..815905130036a 100644 --- a/src/libraries/Microsoft.Extensions.Configuration.FileExtensions/src/FileConfigurationExtensions.cs +++ b/src/libraries/Microsoft.Extensions.Configuration.FileExtensions/src/FileConfigurationExtensions.cs @@ -43,9 +43,9 @@ public static IFileProvider GetFileProvider(this IConfigurationBuilder builder) throw new ArgumentNullException(nameof(builder)); } - if (builder.Properties.TryGetValue(FileProviderKey, out object provider)) + if (builder.Properties.TryGetValue(FileProviderKey, out object? provider)) { - return provider as IFileProvider; + return (IFileProvider)provider; } return new PhysicalFileProvider(AppContext.BaseDirectory ?? string.Empty); @@ -94,14 +94,14 @@ public static IConfigurationBuilder SetFileLoadExceptionHandler(this IConfigurat /// /// The . /// The . - public static Action GetFileLoadExceptionHandler(this IConfigurationBuilder builder) + public static Action? GetFileLoadExceptionHandler(this IConfigurationBuilder builder) { if (builder == null) { throw new ArgumentNullException(nameof(builder)); } - if (builder.Properties.TryGetValue(FileLoadExceptionHandlerKey, out object handler)) + if (builder.Properties.TryGetValue(FileLoadExceptionHandlerKey, out object? handler)) { return handler as Action; } diff --git a/src/libraries/Microsoft.Extensions.Configuration.FileExtensions/src/FileConfigurationProvider.cs b/src/libraries/Microsoft.Extensions.Configuration.FileExtensions/src/FileConfigurationProvider.cs index aef7acc13e5e9..60811023f6b38 100644 --- a/src/libraries/Microsoft.Extensions.Configuration.FileExtensions/src/FileConfigurationProvider.cs +++ b/src/libraries/Microsoft.Extensions.Configuration.FileExtensions/src/FileConfigurationProvider.cs @@ -17,7 +17,7 @@ namespace Microsoft.Extensions.Configuration /// public abstract class FileConfigurationProvider : ConfigurationProvider, IDisposable { - private readonly IDisposable _changeTokenRegistration; + private readonly IDisposable? _changeTokenRegistration; /// /// Initializes a new instance with the specified source. @@ -30,7 +30,7 @@ public FileConfigurationProvider(FileConfigurationSource source) if (Source.ReloadOnChange && Source.FileProvider != null) { _changeTokenRegistration = ChangeToken.OnChange( - () => Source.FileProvider.Watch(Source.Path), + () => Source.FileProvider.Watch(Source.Path!), () => { Thread.Sleep(Source.ReloadDelay); @@ -53,12 +53,12 @@ public override string ToString() private void Load(bool reload) { - IFileInfo file = Source.FileProvider?.GetFileInfo(Source.Path); + IFileInfo? file = Source.FileProvider?.GetFileInfo(Source.Path ?? string.Empty); if (file == null || !file.Exists) { if (Source.Optional || reload) // Always optional on reload { - Data = new Dictionary(StringComparer.OrdinalIgnoreCase); + Data = new Dictionary(StringComparer.OrdinalIgnoreCase); } else { @@ -100,7 +100,7 @@ static Stream OpenRead(IFileInfo fileInfo) { if (reload) { - Data = new Dictionary(StringComparer.OrdinalIgnoreCase); + Data = new Dictionary(StringComparer.OrdinalIgnoreCase); } var exception = new InvalidDataException(SR.Format(SR.Error_FailedToLoad, file.PhysicalPath), ex); HandleException(ExceptionDispatchInfo.Capture(exception)); diff --git a/src/libraries/Microsoft.Extensions.Configuration.FileExtensions/src/FileConfigurationSource.cs b/src/libraries/Microsoft.Extensions.Configuration.FileExtensions/src/FileConfigurationSource.cs index 1122e6f171553..d58c265f406a9 100644 --- a/src/libraries/Microsoft.Extensions.Configuration.FileExtensions/src/FileConfigurationSource.cs +++ b/src/libraries/Microsoft.Extensions.Configuration.FileExtensions/src/FileConfigurationSource.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. using System; +using System.Diagnostics.CodeAnalysis; using System.IO; using Microsoft.Extensions.FileProviders; @@ -15,12 +16,13 @@ public abstract class FileConfigurationSource : IConfigurationSource /// /// Used to access the contents of the file. /// - public IFileProvider FileProvider { get; set; } + public IFileProvider? FileProvider { get; set; } /// /// The path to the file. /// - public string Path { get; set; } + [DisallowNull] + public string? Path { get; set; } /// /// Determines if loading the file is optional. @@ -41,7 +43,7 @@ public abstract class FileConfigurationSource : IConfigurationSource /// /// Will be called if an uncaught exception occurs in FileConfigurationProvider.Load. /// - public Action OnLoadException { get; set; } + public Action? OnLoadException { get; set; } /// /// Builds the for this source. @@ -56,8 +58,8 @@ public abstract class FileConfigurationSource : IConfigurationSource /// The . public void EnsureDefaults(IConfigurationBuilder builder) { - FileProvider = FileProvider ?? builder.GetFileProvider(); - OnLoadException = OnLoadException ?? builder.GetFileLoadExceptionHandler(); + FileProvider ??= builder.GetFileProvider(); + OnLoadException ??= builder.GetFileLoadExceptionHandler(); } /// @@ -70,8 +72,8 @@ public void ResolveFileProvider() !string.IsNullOrEmpty(Path) && System.IO.Path.IsPathRooted(Path)) { - string directory = System.IO.Path.GetDirectoryName(Path); - string pathToFile = System.IO.Path.GetFileName(Path); + string? directory = System.IO.Path.GetDirectoryName(Path); + string? pathToFile = System.IO.Path.GetFileName(Path); while (!string.IsNullOrEmpty(directory) && !Directory.Exists(directory)) { pathToFile = System.IO.Path.Combine(System.IO.Path.GetFileName(directory), pathToFile); @@ -84,6 +86,5 @@ public void ResolveFileProvider() } } } - } } diff --git a/src/libraries/Microsoft.Extensions.Configuration.FileExtensions/src/FileLoadExceptionContext.cs b/src/libraries/Microsoft.Extensions.Configuration.FileExtensions/src/FileLoadExceptionContext.cs index a87c82339f58c..1d619335d5e88 100644 --- a/src/libraries/Microsoft.Extensions.Configuration.FileExtensions/src/FileLoadExceptionContext.cs +++ b/src/libraries/Microsoft.Extensions.Configuration.FileExtensions/src/FileLoadExceptionContext.cs @@ -13,12 +13,12 @@ public class FileLoadExceptionContext /// /// The that caused the exception. /// - public FileConfigurationProvider Provider { get; set; } + public FileConfigurationProvider Provider { get; set; } = null!; /// /// The exception that occurred in Load. /// - public Exception Exception { get; set; } + public Exception Exception { get; set; } = null!; /// /// If true, the exception will not be rethrown. diff --git a/src/libraries/Microsoft.Extensions.Configuration.FileExtensions/src/Microsoft.Extensions.Configuration.FileExtensions.csproj b/src/libraries/Microsoft.Extensions.Configuration.FileExtensions/src/Microsoft.Extensions.Configuration.FileExtensions.csproj index c256903f01cc2..e2c041ade1833 100644 --- a/src/libraries/Microsoft.Extensions.Configuration.FileExtensions/src/Microsoft.Extensions.Configuration.FileExtensions.csproj +++ b/src/libraries/Microsoft.Extensions.Configuration.FileExtensions/src/Microsoft.Extensions.Configuration.FileExtensions.csproj @@ -2,6 +2,7 @@ $(NetCoreAppCurrent);$(NetCoreAppMinimum);netstandard2.0;$(NetFrameworkMinimum) + enable true false