aspnetcore icon indicating copy to clipboard operation
aspnetcore copied to clipboard

dotnet SomeAssembly.dll not loading appsettings.json

Open livarcocc opened this issue 8 years ago • 8 comments

From @giridharprakash on February 2, 2018 4:46

Steps to reproduce

create new dotnet core web api project Get some configuration from appsettings.json in Startup.cs like eg connection string dotnet publish -c Release Now go to publish directory and run dotnet Assembly.dll

Expected behavior

While debugging in visual studio everything working fine but when i run from command line then it's not loading appsettings.json

Actual behavior

In startup.cs connection string is coming as null var connectionString = Configuration["AppSettings:ConnectionString"];

Environment data

dotnet --info output: dotnet --info .NET Command Line Tools (2.1.4)

Product Information: Version: 2.1.4 Commit SHA-1 hash: 5e8add2190

Runtime Environment: OS Name: Windows OS Version: 10.0.16299 OS Platform: Windows RID: win10-x64 Base Path: C:\Program Files\dotnet\sdk\2.1.4\

Microsoft .NET Core Shared Framework Host

Version : 2.0.5 Build : 17373eb129b3b05aa18ece963f8795d65ef8ea54

Copied from original issue: dotnet/cli#8523

livarcocc avatar Feb 06 '18 00:02 livarcocc

@giridharprakash you need to run the application from the correct folder. By default, the content root (the root path where config files are searched) is the current working directory.

davidfowl avatar Feb 06 '18 05:02 davidfowl

I would love to have a fix for this as well. Even if it's just an additional command line switch for now. My use case is a Makefile where I'd like to be able to run the app via make and have it pick up the setting appropriately.

run-api:  ## Builds the API then runs it. NOTE: Broken due to issue in dotnet command
	dotnet build ./app/api
	MONGO_CONNECTION=mongodb://127.0.0.1:27017/corp-hq dotnet ./app/api/bin/Debug/netcoreapp2.0/api.dll

FWIW the code here is my "example": https://github.com/fritogotlayed/corp-hq/blob/ba6400003fb2d854c50c2f0824434b955e2b6a0e/Makefile#L17-L19

fritogotlayed avatar Mar 10 '18 04:03 fritogotlayed

@fritogotlayed . Imho. You shouldnt access the Debug folder in that way for production purposes. The

dotnet publish

output folder should be referenced.

dcarr42 avatar Mar 10 '18 10:03 dcarr42

@dallancarr That Makefile reference is for local dev only. Production is done through a docker file. https://github.com/fritogotlayed/corp-hq/blob/mongo-demo/app/api/Dockerfile . The dotnet publish can be seen on line 12 :-). Definitely thanks for the tip though!

fritogotlayed avatar Mar 10 '18 16:03 fritogotlayed

For anyone else that stumbles across this thread I'll admit I am an idiot. I missed the -p flag when I was reading the docs on dotnet run. I'll update my previous comment link so the code doesn't change when I amend my branch.

The updated block look like this

run-api:  ## Builds the API then runs it. NOTE: Broken due to issue in dotnet command
	dotnet build ./app/api
	MONGO_CONNECTION=mongodb://127.0.0.1:27017/corp-hq dotnet run -p ./app/api

fritogotlayed avatar Mar 10 '18 16:03 fritogotlayed

Thank you for your feedback. We're closing this issue as the behavior discussed seems to be by design.

Eilon avatar Mar 12 '18 18:03 Eilon

take a look at: https://stackoverflow.com/questions/48429268/custom-keys-in-appsettings-json-not-working-in-asp-net-core-2-0/51376132#51376132

seaside2 avatar Jul 24 '18 06:07 seaside2

I have just hit this during my Docker workflow for new application. I see this is by design, but in my opinion it is pretty unintuitive behavior. Can it be improved ?

lkts avatar Sep 25 '18 07:09 lkts