如何在 .Net 3.1 的 Worker Service 中使用 appsettings.json
How to use appsettings.json in Worker Service in .Net 3.1
当我尝试 运行 我的 windows 服务时,该服务没有启动并且出现以下错误。
.NET Core Version: 3.1.11
Description: The process was terminated due to an unhandled exception.
Exception Info: System.IO.FileLoadException: Could not load file or assembly 'Microsoft.Extensions.Configuration.Abstractions, Version=5.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'. The located assembly's manifest definition does not match the assembly reference. (0x80131040)
appsettings.json
{
"myservice": {
"production": "No",
"staging": "No",
"development": "Yes",
}
}
public class Options
{
public string Production { get; set; }
public string Staging { get; set; }
public string Development { get; set; }
}
Program.cs
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureServices((hostContext, services) =>
{
services.Configure<Options>(hostContext.Configuration.GetSection("myservice"));
services.AddHostedService<Worker>();
services.AddTransient<Options>(_ => _.GetRequiredService<IOptions<Options>>().Value);
})
.UseWindowsService();
}
Worker.cs
public class Worker : BackgroundService
{
private readonly ILogger<Worker> _logger;
private BackgroundJobServer _server;
private readonly Options _options;
public Worker(ILogger<Worker> logger, IOptions<Options> options)
{
_logger = logger;
_options = options.Value;
}
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
_server = new BackgroundJobServer();
while (!stoppingToken.IsCancellationRequested)
{
await Task.Delay(1000, stoppingToken);
}
}
}
我尝试了不同的选择,但无法解决问题。感谢您的帮助。
似乎您将 nuget 包更新到与您的框架版本不兼容的版本(.net core 5.0 而不是 3.1)
当我尝试 运行 我的 windows 服务时,该服务没有启动并且出现以下错误。
.NET Core Version: 3.1.11 Description: The process was terminated due to an unhandled exception. Exception Info: System.IO.FileLoadException: Could not load file or assembly 'Microsoft.Extensions.Configuration.Abstractions, Version=5.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'. The located assembly's manifest definition does not match the assembly reference. (0x80131040)
appsettings.json
{
"myservice": {
"production": "No",
"staging": "No",
"development": "Yes",
}
}
public class Options
{
public string Production { get; set; }
public string Staging { get; set; }
public string Development { get; set; }
}
Program.cs
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureServices((hostContext, services) =>
{
services.Configure<Options>(hostContext.Configuration.GetSection("myservice"));
services.AddHostedService<Worker>();
services.AddTransient<Options>(_ => _.GetRequiredService<IOptions<Options>>().Value);
})
.UseWindowsService();
}
Worker.cs
public class Worker : BackgroundService
{
private readonly ILogger<Worker> _logger;
private BackgroundJobServer _server;
private readonly Options _options;
public Worker(ILogger<Worker> logger, IOptions<Options> options)
{
_logger = logger;
_options = options.Value;
}
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
_server = new BackgroundJobServer();
while (!stoppingToken.IsCancellationRequested)
{
await Task.Delay(1000, stoppingToken);
}
}
}
我尝试了不同的选择,但无法解决问题。感谢您的帮助。
似乎您将 nuget 包更新到与您的框架版本不兼容的版本(.net core 5.0 而不是 3.1)