0

I have this configuration in console worker service app:

IConfiguration configuration = new ConfigurationBuilder()
    .SetBasePath("/home/pi/config/")
    .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
    .AddJsonFile($"appsettings.{Environment.GetEnvironmentVariable("DOTNET_ENVIRONMENT") ?? "Production"}.json", true, true)
    .AddEnvironmentVariables()
    .Build();

Which I then use with host:

IHost host = Host.CreateDefaultBuilder(args)
        .ConfigureServices((cotext, services) =>
        {
            services.Configure<Settings>(configuration.GetSection(nameof(Settings)));
            services.AddSingleton<IMyService, MyService>();
            services.AddHostedService<Worker>();
        })
        .Build();

Then in MyService class instance, how can I get information about which config file has been used and its path? There don't seem to be any information about configuration files after injecting IConfiguration into MyService.

Kris
  • 328
  • 4
  • 15

1 Answers1

0

AppSettings do not load selectively based on the environment. But what happens is, the default file first loads, then any other files will override any existing default settings.

Based on your code:

  • appsettings.json will be added as the default file as non-optional. Meaning that if the file doesn't exist the app will fail.

  • then the environment-specific appsettings.{env}.json will be loaded as optional. If it doesn't exist, nothing happens. If does exist, any settings that it might contain will override the default settings.

Which basically means that there's no "which config file has been used" since multiple file could be loaded if the conditions are satisfied. But to check which environment is your app running in, you can use IHostEnvironment, for example to call IHostEnvironment.EnvironmentName

You can read more about the Configuration in .NET from the documentation which also has information about configuration providers and implementing a custom configuration provider.

You can also read this article about .NET Core Dependency Injection with Configuration.

Nomic
  • 21
  • 1
  • 4
  • 1
    OK, with IHostEnvironment I can check the environment and in my case I can assume which appsettings are being used. But what I really need is to get the property set by new ConfigurationBuilder().SetBasePath("....") in other classes. There will be classes or libraries that will modify the config file and need to know its location. I don't want to hard code this path into those classes as it may change with time. Can something be injected into those classes that will give me access to this BasePath property? – Kris Jan 24 '23 at 08:12
  • There's no way to get `AppSettings` path from `ConfigurationBuilder`. Besides, you're already hard coding the path into `SetBasePath`, so, why not just make it a constant and use it anywhere you want? – Nomic Jan 25 '23 at 03:35
  • And about modifying the `AppSettings` there's already a good enough solution [here](https://stackoverflow.com/questions/41653688/asp-net-core-appsettings-json-update-in-code). Otherwise, i'd really suggest you use an external provider like a database to inject your configuration. You can check [this](https://stackoverflow.com/questions/60330342/can-i-move-configuration-from-appsettings-json-to-database-in-an-asp-net-core-mv) out for how to do it. – Nomic Jan 25 '23 at 03:39
  • Then this is a bit of a flaw - it would be nice to see this implemented by Microsoft. Don't worry about other things, I've got the software ready. It can modify the appsettings.json and currently I have the path hard coded globally. I just wanted to confirm if this property is being passed somewhere through DI container and if perhaps there is more elegant way of getting it into services. – Kris Jan 25 '23 at 08:00
  • I wouldn't say it's a flaw since, as I said before, you can still create your own custom configuration provider and tweak it the way you want. This was even stated in the [documentation](https://learn.microsoft.com/en-us/dotnet/core/extensions/custom-configuration-provider). `There are many configuration providers available for common configuration sources such as JSON, XML, and INI files. You may need to implement a custom configuration provider when one of the available providers doesn't suit your application needs.` – Nomic Jan 25 '23 at 22:59