Reading configuration settings in the .NET Core 2.0 library

I have a library that I am converting from .NET 4.6.1 to .NET Standard 2.0.

I am having problems with the configuration settings. In our old library, we can very easily access configuration settings using System.Configuration.ConfigurationManager.AppSettings . I understand that there is no direct replacement for this namespace in .NET Standard, but is there any way to read such information. I believe that I could create a singleton in the library, load all the configuration parameters from a text file, and use this class to access all configuration settings. But I'm looking for an easier way here.

I know that I can pass the settings through the injection of dependencies to my classes from the consumer application, but there are inner classes that have never been accessed directly, which also need these settings.

+5
source share
1 answer

Please try like this.

 var builder = new ConfigurationBuilder() .AddJsonFile($"appsettings.json", true, true) .AddInMemoryCollection(); var configuration = builder.Build(); var value = configuration.GetSection(key).Value 
0
source

Source: https://habr.com/ru/post/1271666/


All Articles