question

Cenk-6470 avatar image
0 Votes"
Cenk-6470 asked cooldadtx answered

.Net Code 3.1 appsettings.json reloadonchange problem

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"]);


dotnet-runtimedotnet-cli
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

cooldadtx avatar image
0 Votes"
cooldadtx answered

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

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.