已发布的.Net Core 2应用程序不读取appsettings.json文件 [英] Published .Net Core 2 application does not read appsettings.json file

查看:389
本文介绍了已发布的.Net Core 2应用程序不读取appsettings.json文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have 2 apps, one console one web api both .net 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文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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