Is there a way to retrieve the path of the appSettings.json in order to load them in a FileSystemWatcher instance?
Asked
Active
Viewed 465 times
0
-
1https://stackoverflow.com/questions/49886920/net-core-2-0-appsettings-json-file-location-when-dealing-with-multiple-projects – MD. RAKIB HASAN Jun 07 '21 at 06:36
-
Why???? What you ask is already done by default. Besides, the configuration system loads settings from *multiple* sources. `appsettings.json` is just a default file name used by the Generic host, loaded with `AddJsonFile`. If you used Configuration directly you'd have make the `AddJsonFile` call yourself – Panagiotis Kanavos Jun 07 '21 at 07:55
-
The Configuration middleware already supports reloading on change, including for files. Some [AddJson overloads](https://learn.microsoft.com/en-us/dotnet/api/microsoft.extensions.configuration.jsonconfigurationextensions.addjsonfile?view=dotnet-plat-ext-5.0#Microsoft_Extensions_Configuration_JsonConfigurationExtensions_AddJsonFile_Microsoft_Extensions_Configuration_IConfigurationBuilder_System_String_System_Boolean_System_Boolean_) allow this by passing `true` in the `reloadOnChange` setting. The Generic Host already sets this flag – Panagiotis Kanavos Jun 07 '21 at 07:57
-
[Detect changes with change tokens in ASP.NET Core](https://learn.microsoft.com/en-us/aspnet/core/fundamentals/change-tokens?view=aspnetcore-5.0) explains the mechanism and shows that [the generic host already tracks changes to appsettings](https://learn.microsoft.com/en-us/aspnet/core/fundamentals/change-tokens?view=aspnetcore-5.0#monitor-for-configuration-changes) – Panagiotis Kanavos Jun 07 '21 at 08:00
1 Answers
0
I found the solution! For all those who are curious I have solved it as follows:
string value;
IConfigurationRoot config = new ConfigurationBuilder().AddJsonFile("appsettings.json", false, true).Build();
IConfigurationProvider provider = config.Providers.ToList().First(); //There could be several providers but in my case I have only one
this.provider.Load();//reloads the content of the appSettings.json
this.provider.TryGet("section:subProperty", out value);
If someone has a better approach, please post that. I'll be happy to read it. :)

littlebit
- 1
- 1
-
There's no need for this. You're doing what Configuration (or rather, [the generic host](https://learn.microsoft.com/en-us/aspnet/core/fundamentals/change-tokens?view=aspnetcore-5.0#monitor-for-configuration-changes)) does by default. If you use Configuration directly, yes, you need to use the correct `AddJsonFile` class. There's no need for the rest of the code though. The whole point of the Configuration middleware is that you *shouldn't* have to know where the settings come from. Just use `config.GetSection("section")["subProperty"]` – Panagiotis Kanavos Jun 07 '21 at 08:01
-
@PanagiotisKanavos: that is an interesting solution and is very efficient. Yet the example in the documentation is applied to the scenario of an asp .net project and what I'm working is a WorkerService, which doesn't have anything to do with 'WebHostEnvironment'. – littlebit Jun 09 '21 at 10:24
-
On the contrary, the configuration subsystem works independently. It can be used by both console and web applications. That's what your own code is doing here - creating the configuration root directly. `IConfiguration` and `IConfigurationRoot` work the same no matter who their clients are – Panagiotis Kanavos Jun 09 '21 at 11:13
-
To put it another way - have you tried to just use `config.GetSection("section")["subProperty"]` ? – Panagiotis Kanavos Jun 09 '21 at 11:13
-
@PanagiotisKanavos: I tried the code>config.GetSection("section")["subProperty"] and that doesn't change when I change that subProperty in the appsettings.json – littlebit Jun 14 '21 at 07:53