среды, путей, переменных... в начале выполнения, а затем получать значения из других классов библиотеки.
Я создал класс «Настройки» с данными, включенными в json appsettings.
Это то, что у меня уже есть, просматривая учебники, но я не могу получить любое значение.
Код: Выделить всё
//Start.cs
public class Startup
{
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
.AddEnvironmentVariables();
Configuration = builder.Build();
}
public IConfiguration Configuration { get; }
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
}
//Settings.cs
public class Settings
{
public ConnectionStrings ConnectionStrings { get; set; }
public Logging Logging { get; set; }
public AppSettings AppSettings { get; set; }
...
//A class to use it in other lib
public class Extractor
{
private readonly IConfiguration _configuration;
public Extractor(IConfiguration configuration) : this()
{
_configuration = configuration;
Init();
}
public void Init()
{
// Extractor:Min is a variable included in appsettings.json
Min = _configuration.GetValue("Extractor:Min")
}
ПРИМЕЧАНИЕ: мне нужно получить эти переменные из другого класса библиотеки, а не из Main. Я не знаю, как инициализировать «конфигурацию» в других классах, чтобы ее использовать. Спасибо
Подробнее здесь: https://stackoverflow.com/questions/651 ... g-net-core
Мобильная версия