我有以下单例类,我希望在我的应用程序中使用:
public sealed class AppSettings
{
private static readonly Lazy<AppSettings> _lazy =
new Lazy<AppSettings>(() => new AppSettings());
private AppSettings()
{
// I want to inject this
var builder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile($"appsettings.json", optional: true, reloadOnChange: true);
Config = builder.Build();
}
public static AppSettings Instance => _lazy.Value;
public IConfigurationRoot Config { get; }
}
我想注入IConfiguration而不是在其中构建它。我该怎么做?
services.AddSingleton<AppSettings>();
- ziomyslaw