我正在使用一个.NET Core7 Worker项目,在该项目中,我希望将应用程序设置添加到我的program.cs
类中,但为此,我需要来自宿主上下文的IConfiguration
接口.
我如何才能将其作为单例注入到项目中?
以下是我的代码:
IHost host = Host.CreateDefaultBuilder(args)
.ConfigureServices(services =>
{
// Here I want to add a configuration from appsettings.json and then map it to my Configuration class
services.AddHostedService<Worker>();
})
.Build();
host.Run();
以下是我的appsettings.json
条:
{
"ServiceName": "Test service",
"connectionString": ""
}
public class Configuration
{
public string ServiceName { get; set; }
public string ConnectionString { get; set; }
}