【问题标题】:Get appsettings.json values on .net core 3.0 worker service after publish as windows service发布为 Windows 服务后,在 .net core 3.0 worker 服务上获取 appsettings.json 值
【发布时间】:2019-10-09 16:26:39
【问题描述】:

按照答案: .NET Core 3 Worker Service Settings Dependency Injection

我可以在 Worker.cs 类的 Debug 或 Release 中获取设置 但是当部署为 Windows 服务时,此值返回为 null

Worker.cs

public class Worker : BackgroundService
{
    private readonly ILogger<Worker> _logger;
    private readonly ServiceSettings _serviceSettings;
    private readonly ServiceConfigurations _serviceConfigurations;

    public Worker(ILogger<Worker> logger, IOptions<ServiceConfigurations> serviceConfigurations, IOptions<ServiceSettings> serviceSettings) 
    {
        _logger = logger;
        _serviceConfigurations = serviceConfigurations.Value;

        _logger.LogInformation($"Worker running at: {DateTime.Now}");
        _serviceSettings = serviceSettings.Value;

        string retornoPathLog = null;
        string PathLog = _serviceSettings.PathLog;

        if (!Directory.Exists(PathLog))
        {
            retornoPathLog = "Diretório de LOG " + PathLog;
            Directory.CreateDirectory(PathLog);
        }

        //Configure Serilo for Logging
        Log.Logger = new LoggerConfiguration()
            .MinimumLevel.Information()
            .Enrich.FromLogContext()
            .WriteTo.File(PathLog + "log.txt", rollingInterval: RollingInterval.Day)
            .CreateLogger();

        if (string.IsNullOrEmpty(retornoPathLog) == false)
        {
            Log.Information("[WFoneService - Watch Event] " + retornoPathLog);
        }
    }
}

appsettings.json:

{
  "ConnectionStrings": {
    "WFoneConnection": ""
  },
  "ServiceConfigurations": {
    "UrlSignalrNotification": "urlValue",
    "WatchIp": "ipValue",
    "WatchPort": "portValue"
  },
  "ServiceSettings": {
    "PathLog": "C:\\Log\\"
  },
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  }
}

【问题讨论】:

  • 显示windows服务的启动代码。

标签: c# asp.net-core


【解决方案1】:

有几种方法可以处理这个问题。

  1. 您可以使用 dotnet new worker 模板

dotnet new worker

  1. 在 .csproj 中手动将项目类型更改为 Microsoft.NET.Sdk.Worker

&lt;Project Sdk="Microsoft.NET.Sdk.Worker"&gt;

  1. 在您的 .csproj 中添加您的 appsettings.json 文件并将其复制到输出目录
  <ItemGroup>
    <None Update="appsettings.json">
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </None>
  </ItemGroup>

如果您有一个现有项目,我建议#2 将您的 csproj 转换为我们的 Worker Sdk,或者如果创建一个新项目很简单,请使用 #1。 #3 只是将 appsettings.json 文件复制到您的输出中是一种可怕的黑客攻击,我不推荐这样做。

【讨论】:

  • 您是否有任何指向显示 Worker SDK 默认包含 appsettings.json 文件的文档的链接?
【解决方案2】:

由于缺乏 .NET core 3.0 的经验,我停止向项目添加包,并按照 Microsoft 网站上的文档找到了解决方案。 我解决了如下问题

在 Program.cs 文件中,我添加了 .UseWindowsService () 并添加了 Microsoft.Extensions.Hosting.WindowsServices 包,如下面的链接所述。

https://docs.microsoft.com/en-us/aspnet/core/host-and-deploy/windows-service?view=aspnetcore-3.0&tabs=visual-studio

【讨论】:

  • 这解决了我的问题。我错过了:Microsoft.Extensions.Hosting.WindowsServices.UseWindowsService() 我在这上面浪费了 3 个小时......我想在创建一个 Worker Service 项目之后,我就可以开始运行 web 服务了。用VS测试时确实运行,但是在Windows 10的服务窗口中添加exe并启动exe后,它总是说,Error1503 not及时响应....
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-02-29
  • 1970-01-01
  • 1970-01-01
  • 2020-05-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多