.Net Code 3.1 appsettings.json reloadonchange problem

Cenk 1 Reputation point
2021-08-12T12:42:06.967+00:00

Hello guys,

I have this .net core 3.1 console app that is running on a windows task scheduler. The problem is if I change the appsettings.json, it does not make any changes. How can I fix this?

Here is my Program.cs:

class Program
    {
        static async Task Main(string[] args)
        {


            Log.Logger = new LoggerConfiguration()
                .MinimumLevel.Information()
                .Enrich.FromLogContext()
                .WriteTo.Console()
                .WriteTo.File("log-.txt",LogEventLevel.Information, rollingInterval: RollingInterval.Day)
                .WriteTo.Email(new EmailConnectionInfo
                {
                    FromEmail = "test@enggaming.com",
                    ToEmail = "anon@user ",
                    MailServer = "smtp.yandex.com.tr",
                    NetworkCredentials = new NetworkCredential
                    {
                        UserName = "test@test.com",
                        Password = "123"
                    },
                    EnableSsl = false,
                    Port = 587,
                    EmailSubject = "Game Purchase Service Error"
                }, restrictedToMinimumLevel: LogEventLevel.Error, batchPostingLimit: 1)
                .CreateLogger();

            var builder = new HostBuilder()
                .ConfigureServices((hostContext, services) =>
                {
                    //Serilog
                    services.AddLogging(loggingBuilder =>
                        loggingBuilder.AddSerilog(dispose: true));

                    //Setting up API Client
                    services.AddHttpClient("OrdersClient", client =>
                    {
                        client.BaseAddress =
                            new Uri("https://testapi.com/orders?status=Created");

                        client.DefaultRequestHeaders.Add("Accept", "application/json");
                    });

                    services.AddSingleton<IHostedService, BusinessService>();

                    //Setting up app settings configuration
                    var config = LoadConfiguration();    
                    services.AddSingleton(config);


                });

            await builder.RunConsoleAsync();
        }

        public static IConfiguration LoadConfiguration()
        {
            var builder = new ConfigurationBuilder()
                .SetBasePath(Directory.GetCurrentDirectory())
                .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
                .AddJsonFile(path: "serilog.json", optional: false, reloadOnChange: true);

            return builder.Build();
        }
    }

Here is the appsettings.json:

}
  "Size": {
    "Production": "1" 
  } 
}

Here is the portion I am using appsettings.json:

...
 var response = await httpClient.GetAsync("https://api.test.com/orders?status=Created&size="+ _configuration["Size:Production"]);
.NET CLI
.NET CLI
A cross-platform toolchain for developing, building, running, and publishing .NET applications.
326 questions
.NET Runtime
.NET Runtime
.NET: Microsoft Technologies based on the .NET software framework.Runtime: An environment required to run apps that aren't compiled to machine language.
1,136 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Michael Taylor 50,591 Reputation points
    2021-08-12T14:26:04.937+00:00

    The problem is likely in your dependency of _configuration. To support reloading you have to be using IOptionsSnapshot. IOptions doesn't support reloading data.

    0 comments No comments