简体   繁体   中英

What is the default for AddJsonFile options in configuration under .NET Core 2.2?

According to some wise source , there's a method called AddJsonFile for reading in a config file. One of its signatures allows to specify optional and reloadOnChange .

IConfigurationRoot config = new ConfigurationBuilder()
  .SetBasePath(Directory.GetCurrentDirectory())
  .AddJsonFile("config.json", false, false)
  .Build();

However, I can't find info on what's the default value for that in case I use a signature without those explicitly specified. I can assume that it's false , basing it on the fact that it's the default value of a boolean (ie default(bool) is false ).

But I prefer to have stuff explicitly and linkably stated.

Refer the source code present at: https://github.com/aspnet/Configuration/blob/master/src/Config.Json/JsonConfigurationExtensions.cs

As per this, if you dont provide parameters they are false. Please note below method:

        /// <summary>
        /// Adds the JSON configuration provider at <paramref name="path"/> to <paramref name="builder"/>.
        /// </summary>
        /// <param name="builder">The <see cref="IConfigurationBuilder"/> to add to.</param>
        /// <param name="path">Path relative to the base path stored in 
        /// <see cref="IConfigurationBuilder.Properties"/> of <paramref name="builder"/>.</param>
        /// <returns>The <see cref="IConfigurationBuilder"/>.</returns>
        public static IConfigurationBuilder AddJsonFile(this IConfigurationBuilder builder, string path)
        {
            return AddJsonFile(builder, provider: null, path: path, optional: false, reloadOnChange: false);
        }
}

As you mentioned earlier in the question, you can use another overload of this method to set reloadOnChange value. Below is sample code from MSDN :

public class Program
{

    public static void Main(string[] args)
    {
        CreateWebHostBuilder(args).Build().Run();
    }

    public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .ConfigureAppConfiguration((hostingContext, config) =>
            {
                config.AddJsonFile("appsettings.json", optional: false, reloadOnChange: false);

                config.AddCommandLine(args);
            })
            .UseStartup<Startup>();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM