已发布的.Net Core 2应用程序不读取appsettings.json文件

编程入门 行业动态 更新时间:2024-10-27 03:32:40
本文介绍了已发布的.Net Core 2应用程序不读取appsettings.json文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有2个应用程序,一个控制台一个Web api,两个都是 core v2.0.两者都有相同的问题.

I have 2 apps, one console one web api both core v2.0 . Both has the same problem.

我曾经通过源文件运行我的应用程序.我在Linux机器上构建和运行它们.一切都很好,一切都很棒,但是现在我正在通过发布输出运行相同的应用程序.

I used to run my application thru source files. I was building and running them on a linux machine. Everything was good, everything was great, but now i'm running the same app thru publish output.

现在,它无法正常运行,因为该应用无法读取appsettings.json.

Now it doesn't work as it suppose to, because the app can't read appsettings.json.

configuration["Settings:Port"] != null ? int.Parse(configuration["Settings:Port"]) //i should be getting this - 1234 : default(int) //but instead i got this - 0

我损坏了appsettings.json文件,希望看到一个异常,但是什么也没有.根本不值一提.我检查了文件几次,只是因为我绝望而修改了文件几次.没有.什么也没发生,什么也没有改变.

I have corrupted the appsettings.json file and i was expecting to see an exception but, nothing. Just nothing. I checked the file a couple of times, i modified the file a couple of times just because i was desperate. Nothing. Nothing happens, nothing changes.

我尝试了调试构建,尝试了发布构建,尝试更改了该文件的构建操作...尝试了,尝试了...什么都没有用,我也不知道为什么.

I tried debug-build, i tried release-build, i tried to change build action of that file... I tried this, i tried that... Nothing works and i have no idea why.

您可能会猜到,我需要一些建议.谢谢大家.

And as you can guess, i need a few advices. Thank you all.

我如何使用/读取配置文件:

How i use/read config file:

IConfiguration configuration = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("appsettings.json", true, true).Build();

推荐答案

问题是我从中运行应用程序的路径.

The problem is the path i was running the application from.

我正在运行命令以从根(/)启动应用程序.应用程序运行正常,但显然找不到appsettings.json文件.

I was running the command to start the app from the root (/). Application was running fine but obviously couldn't locate the appsettings.json file.

我试图从Project路径启动该应用程序,所以我将其CD到Project目录中,运行命令,一切恢复正常.

I tried to start the app from the Project path so i cd'ed into the Project directory, ran the command and everything went back to normal.

此处有详细说明:

当我运行上面提到的那些应用程序时,我曾经使用cd进入它们的目录并执行"dotnet run".

When i was running those apps i've been mentioning above, i used to cd into their directories and execute "dotnet run".

cd /blabla/etcetc dotnet run

但是,在发布它们之后,我无法使用相同的命令.我必须指定它为dll.然后我想出了,我可以直接运行它们而无需CD-进入它们的目录.

However, after i publish them, i couldn't use the same command. I had to specify it's dll. Then i figured, i could just run them without cd-ing into their directories.

dotnet /blabla/etcetc/my.project.dll

这就是问题所在.现在我正在这样做,而不是这样做;

And that was the problem. Instead of doing so, now i'm doing this;

cd /blabla/etcetc dotnet my.project.dll

更多推荐

已发布的.Net Core 2应用程序不读取appsettings.json文件

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

发布评论

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

>www.elefans.com

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