2

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();
nick cubric
  • 193
  • 1
  • 9
  • Is the `appsettings.json` file present in the `publish` directory? (Just trying to work out whether this is a publish problem or an execution-time problem.) – Jon Skeet May 29 '18 at 07:07
  • @DaisyShipton yes it is – nick cubric May 29 '18 at 07:08
  • Could you edit your `Startup` constructor into the question? (That's where the settings file is typically configured. You may have structured your code differently of course.) Also, do you have multiple appsettings files, or just the one? – Jon Skeet May 29 '18 at 07:21
  • @DaisyShipton just 1. And it's not about Startup because there's no startup on console app but the same problem goes. I am adding some more code now, you should see it soon – nick cubric May 29 '18 at 07:28
  • I don't know what you mean by "there's no startup on console app but the same problem goes". (The code you've posted has typically in the constructor for a `Startup` class in my experience, although things have moved around a bit over the course of ASP.NET Core's lifetime.) – Jon Skeet May 29 '18 at 07:57
  • @DaisyShipton I thought you meant the Startup.cs file. Of course there is a startup for every program right? :) However, i figured out the problem. I'll be posting that soon. Thank you for everything. – nick cubric May 29 '18 at 08:01
  • Yes, I did mean the `Startup.cs` file, which is where I'd expect to see that code, although as I mentioned, it's moved around a bit over time. – Jon Skeet May 29 '18 at 08:02

3 Answers3

16

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
nick cubric
  • 193
  • 1
  • 9
  • Sample usage for start-stop-daemon start-stop-daemon --start --background --make-pidfile --chdir $APPDIR --pidfile $PID --exec $DAEMON – Gencebay Sep 11 '18 at 19:38
  • Good find. Another option is to ensure that the current working directory is set properly, which will ensure that the files are loaded. In VSCode this can be set in the `launch.json` file, option `cwd` – Darbio Oct 22 '18 at 03:58
2

What I did is to change the Directory.GetCurrentDirectory() for AppContext.BaseDirectory and this fixed all errors related to the appsettings and config.

1

For anyone trying to setup a Linux daemon, my issue was that I didn't have the 'WorkingDirectory' specified correctly inside my app.service file.

[Unit]
Description=Cool App

[Service]
WorkingDirectory=/var/www/CoolApp
ExecStart=/usr/bin/dotnet /var/www/CoolApp/CoolApp.dll --urls http://localhost:5002
Restart=always
# Restart service after 10 seconds if the dotnet service crashes:
RestartSec=10
KillSignal=SIGINT
SyslogIdentifier=coolApp
User=CoolAppAdminUser
Environment=ASPNETCORE_ENVIRONMENT=Production

[Install]
WantedBy=multi-user.target
BLong
  • 69
  • 4