【问题标题】:appSettings.json not read when launching .net core windows service启动 .net 核心 Windows 服务时未读取 appSettings.json
【发布时间】:2022-06-21 01:06:04
【问题描述】:

我跟随微软guide搭建一个窗口服务。

appSettings.json 文件是从 Visual Studio 或命令行运行时读取的,但从服务管理器运行服务 dll 时会失败。 从配置中填充的所有变量都保持为空。

问题是windows服务管理器将默认目录设置为c:\windows\system32

如何读取安装路径?

【问题讨论】:

    标签: .net-core windows-services appsettings


    【解决方案1】:

    服务启动时,将工作路径设置为安装路径:

    //program.cs
    IHost host = Host.CreateDefaultBuilder(args)
      .ConfigureAppConfiguration(conf =>
      {
          conf.SetBasePath(AppDomain.CurrentDomain.BaseDirectory);
      })
      ...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-07
      • 1970-01-01
      • 2019-11-05
      • 1970-01-01
      相关资源
      最近更新 更多