【问题标题】:ASP.NET core 3.1 : UseWindowsService not setting content root as expected when publishing app with "produce single file"ASP.NET core 3.1:使用“生成单个文件”发布应用程序时,UseWindowsService 未按预期设置内容根目录
【发布时间】:2020-10-11 08:37:32
【问题描述】:

在使用 HTTP.SYS 的 Windows 服务中创建了一个自托管的 ASP.NET 核心 3.1 应用程序,并且一切正常。我的 CreateHostBuilder 方法开始如下

public static IHostBuilder CreateHostBuilder(string[] args) =>
    Host.CreateDefaultBuilder(args)
        .UseWindowsService()
        .ConfigureAppConfiguration((context, config) =>
        {
            // configure the app here.
            config.AddJsonFile("TEST.json");
        })

并且“TEST.json”文件已正确找到并从内容根文件夹(包含 Windows 服务 exe 和其他程序集的文件夹)中读取。但是,如果我尝试将应用程序作为单个文件发布(其他一切都相同)

“TEST.json”文件加载失败并出现以下错误,好像内容根目录未设置为 Windows 服务可执行文件所在的位置(请注意,文件完整路径应为“C:\Windows\ServiceProfiles\ LocalService\AppData\Local\Temp.net\TestNetCore\544bgjpl.0mf\TEST.json')

Application: TestNetCore.exe
CoreCLR Version: 4.700.20.26901
.NET Core Version: 3.1.5
Description: The process was terminated due to an unhandled exception.
Exception Info: System.IO.FileNotFoundException: The configuration file 'TEST.json' was not found and is not optional. The physical path is 'C:\Windows\ServiceProfiles\LocalService\AppData\Local\Temp\.net\TestNetCore\544bgjpl.0mf\TEST.json'.
   at Microsoft.Extensions.Configuration.FileConfigurationProvider.HandleException(ExceptionDispatchInfo info)
   at Microsoft.Extensions.Configuration.FileConfigurationProvider.Load(Boolean reload)
   at Microsoft.Extensions.Configuration.FileConfigurationProvider.Load()
   at Microsoft.Extensions.Configuration.ConfigurationRoot..ctor(IList`1 providers)
   at Microsoft.Extensions.Configuration.ConfigurationBuilder.Build()
   at Microsoft.Extensions.Hosting.HostBuilder.BuildAppConfiguration()
   at Microsoft.Extensions.Hosting.HostBuilder.Build()
   at TestNetCore.Program.Main(String[] args)

知道为什么会这样吗?

【问题讨论】:

    标签: asp.net-core .net-core


    【解决方案1】:

    在 Visual Studio 中右键单击文件,选择“属性”选项并在“复制到输出目录”下选择“始终复制”

    【讨论】:

    • 我相信您完全误解了所描述的问题。这不是因为 json 文件不存在,而是因为在 'C:\Windows\ServiceProfiles\LocalService\AppData\Local\Temp.net\TestNetCore\544bgjpl.0mf\'中预期该文件。
    【解决方案2】:

    这似乎是一个已知问题,有关详细信息,请参阅https://github.com/dotnet/runtime/issues/36065。问题是 AppContext.BaseDirectory 不是单文件 exe 的原始目录,它是 exe 中的程序集被提取到的目录。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-04-08
      • 1970-01-01
      • 2022-11-01
      • 2020-12-05
      • 2021-05-24
      • 2021-03-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多