如何获取.NET Core 3单文件应用程序以查找appsettings.json文件?

编程入门 行业动态 更新时间:2024-10-28 05:27:11
本文介绍了如何获取.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:\Users\USERNAME\AppData\Local\Temp\\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\\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文件?

本文发布于:2023-11-16 17:12:46,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1606246.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:应用程序   文件   Core   NET   单文

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!