如何让我的 .NET Core 3 单文件应用程序找到 appsettings.json 文件? [英] How can I get my .NET Core 3 single file app to find the appsettings.json file?

查看:21
本文介绍了如何让我的 .NET Core 3 单文件应用程序找到 appsettings.json 文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

应如何配置单文件 .Net Core 3.0 Web API 应用程序以查找与构建单文件应用程序位于同一目录中的 appsettings.json 文件?

How should a single-file .Net Core 3.0 Web API application be configured to look for the appsettings.json file that is in the same directory that the single-file application is built to?

运行后

dotnet publish -r win-x64 -c Release /p:PublishSingleFile=true

目录如下:

XX/XX/XXXX  XX:XX PM    <DIR>          .
XX/XX/XXXX  XX:XX PM    <DIR>          ..
XX/XX/XXXX  XX:XX PM               134 appsettings.json
XX/XX/XXXX  XX:XX PM        92,899,983 APPNAME.exe
XX/XX/XXXX  XX:XX PM               541 web.config
               3 File(s)     92,900,658 bytes

然而,尝试运行 APPNAME.exe 会导致以下错误

However, attempting to run APPNAME.exe results in the following error

An exception occurred, System.IO.FileNotFoundException: The configuration file 'appsettings.json' was not found and is not optional. The physical path is 'C:UsersUSERNAMEAppDataLocalTemp.netAPPNAMEkyl3yc02.5zsappsettings.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.AspNetCore.Hosting.WebHostBuilder.BuildCommonServices(AggregateException& hostingStartupErrors)
   at Microsoft.AspNetCore.Hosting.WebHostBuilder.Build()
...

我尝试了 类似但不同的问题,以及其他 Stack Overflow 问题.

I tried solutions from a similar, but distinct question, as well as other Stack Overflow questions.

我试图将以下内容传递给 SetBasePath()

I attempted to pass the following to SetBasePath()

  • Directory.GetCurrentDirectory()

environment.ContentRootPath

Path.GetDirectoryName(Assembly.GetEntryAssembly().Location)

每个都导致相同的错误.

Each led to the same error.

问题的根源在于 PublishSingleFile 二进制文件被解压并从 temp 目录运行.

The root of the issue is that the PublishSingleFile binary is unzipped and run from a temp directory.

对于这个单文件应用程序,它查找的位置 appsettings.json 是以下目录:

In the case of this single file app, the location it was looking appsettings.json was the following directory:

C:UsersUSERNAMEAppDataLocalTemp.netAPPNAMEkyl3yc02.5zs

以上所有方法都指向解压文件的位置,与运行的位置不同.

All of the above methods point to the place that the file is unzipped to, which is different than the place it was run from.

推荐答案

我在 GitHub 上发现了一个问题 这里标题为PublishSingleFile,不包括appsettings not working as expected.

I found an issue on GitHub here titled PublishSingleFile excluding appsettings not working as expected.

这指向了此处的另一个问题,标题为单个文件发布:AppContext.BaseDirectory 不指向 apphost 目录

That pointed to another issue here titled single file publish: AppContext.BaseDirectory doesn't point to apphost directory

在其中,一个解决方案是尝试 Process.GetCurrentProcess().MainModule.FileName

In it, a solution was to try Process.GetCurrentProcess().MainModule.FileName

以下代码将应用程序配置为查看单个可执行应用程序的运行目录,而不是二进制文件被提取到的位置.

The following code configured the application to look at the directory that the single-executable application was run from, rather than the place that the binaries were extracted to.

config.SetBasePath(GetBasePath());
config.AddJsonFile("appsettings.json", false);

GetBasePath() 实现:

private string GetBasePath()
{
    using var processModule = Process.GetCurrentProcess().MainModule;
    return Path.GetDirectoryName(processModule?.FileName);
}

这篇关于如何让我的 .NET Core 3 单文件应用程序找到 appsettings.json 文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆