NET 6:Windows 具有强类型配置的服务
NET 6: Windows Service with strongly typed configuration
我正在尝试使用 NET 6 和找到的文档创建 Windows 服务 here。
我想使用强类型配置模式,所以我这样修改了启动代码:
using IHost host = Host.CreateDefaultBuilder(args)
.UseWindowsService(options =>
{
options.ServiceName = "My Service";
})
.ConfigureAppConfiguration((hostingContext, configuration) =>
{
configuration.Sources.Clear();
IHostEnvironment env = hostingContext.HostingEnvironment;
configuration.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
})
.ConfigureServices((hostingContext, services) =>
{
services.AddSingleton<MyService>();
services.Configure<AppSettings>(hostingContext.Configuration.GetSection("AppSettings"));
services.AddHostedService<WindowsBackgroundService>();
})
.Build();
await host.RunAsync();
然后在MyService.cs:
private AppSettings _appSettings;
public MyClass(AppSettings appSettings)
{
_appSettings = appSettings;
}
这给了我以下异常:
System.InvalidOperationException: 'Unable to resolve service for type 'StatSveglia.Service.AppSettings' while attempting to activate 'StatSveglia.Service.SvegliaService'.'
看来这一行没有作用:
services.Configure<AppSettings>(hostingContext.Configuration.GetSection("AppSettings"));
我应该如何更改我的代码以使用配置注入?
附带问题:在文档中找到的示例服务中,services.AddHttpClient<JokeService>();
用于添加服务。我的服务不是 HTTP 客户端,所以我更喜欢 .AddSingleton<>
。这是一个不错的选择吗?
进一步阅读后,我发现该行:
services.Configure<AppSettings>(hostingContext.Configuration.GetSection("AppSettings"));
为 class IOptions<AppSettings>
而不是 AppSettings
本身注册依赖注入。
因此正确的用法是:
private IOptions<AppSettings> _appSettings;
public MyClass(IOptions<AppSettings> appSettings)
{
_appSettings = appSettings;
}
private void SomeMethod()
{
var mySetting = _appSettings.Value.MySetting
}
我正在尝试使用 NET 6 和找到的文档创建 Windows 服务 here。
我想使用强类型配置模式,所以我这样修改了启动代码:
using IHost host = Host.CreateDefaultBuilder(args)
.UseWindowsService(options =>
{
options.ServiceName = "My Service";
})
.ConfigureAppConfiguration((hostingContext, configuration) =>
{
configuration.Sources.Clear();
IHostEnvironment env = hostingContext.HostingEnvironment;
configuration.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
})
.ConfigureServices((hostingContext, services) =>
{
services.AddSingleton<MyService>();
services.Configure<AppSettings>(hostingContext.Configuration.GetSection("AppSettings"));
services.AddHostedService<WindowsBackgroundService>();
})
.Build();
await host.RunAsync();
然后在MyService.cs:
private AppSettings _appSettings;
public MyClass(AppSettings appSettings)
{
_appSettings = appSettings;
}
这给了我以下异常:
System.InvalidOperationException: 'Unable to resolve service for type 'StatSveglia.Service.AppSettings' while attempting to activate 'StatSveglia.Service.SvegliaService'.'
看来这一行没有作用:
services.Configure<AppSettings>(hostingContext.Configuration.GetSection("AppSettings"));
我应该如何更改我的代码以使用配置注入?
附带问题:在文档中找到的示例服务中,services.AddHttpClient<JokeService>();
用于添加服务。我的服务不是 HTTP 客户端,所以我更喜欢 .AddSingleton<>
。这是一个不错的选择吗?
进一步阅读后,我发现该行:
services.Configure<AppSettings>(hostingContext.Configuration.GetSection("AppSettings"));
为 class IOptions<AppSettings>
而不是 AppSettings
本身注册依赖注入。
因此正确的用法是:
private IOptions<AppSettings> _appSettings;
public MyClass(IOptions<AppSettings> appSettings)
{
_appSettings = appSettings;
}
private void SomeMethod()
{
var mySetting = _appSettings.Value.MySetting
}