asp.net-core-2.0.net-core-2.0

Published .Net Core 2 application does not read appsettings.json file


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

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.

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

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.


Edit:

How i use/read config file:

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

Solution

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

    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.

    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.


    Detailed explanation here:

    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
    

    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