如何获取.NET Core 3单文件应用程序以查找appsettings.json文件? [英] How can I get my .NET Core 3 single file app to find the appsettings.json file?
问题描述
应如何配置单个文件.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:\Users\USERNAME\AppData\Local\Temp\.net\APPNAME\kyl3yc02.5zs\appsettings.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:\Users\USERNAME\AppData\Local\Temp\.net\APPNAME\kyl3yc02.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,不包括无法正常使用的应用设置
。
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屋!