C# IConfiguration not loading appsettings
Anyone knows why my appsettings isnt found?
It exists in the same project in my solution as i run?
Its a simple console application with my appsettings.json in it but its not found?
But when i user User Secrets it works fine.
72 Replies
how are you trying to use it?
and what do you mean by "it's not found"?
Unknown User•12mo ago
Message Not Public
Sign In & Join Server To View
Alrighty, here it comes:
Unknown User•12mo ago
Message Not Public
Sign In & Join Server To View
Yeah okay
Unknown User•12mo ago
Message Not Public
Sign In & Join Server To View
What is already part of the builder?
Unknown User•12mo ago
Message Not Public
Sign In & Join Server To View
?
Unknown User•12mo ago
Message Not Public
Sign In & Join Server To View
I dont think im missing anything
like when i enable to load from usersecrets it works
Both when building to linux and docker + building in windows and running in Visual studio terminal without docker it fails at samee thing
cannot load the configuration file
Unknown User•12mo ago
Message Not Public
Sign In & Join Server To View
So the placement in my csproj or the worker valuie is wrong?
Unknown User•12mo ago
Message Not Public
Sign In & Join Server To View
Does worker exist for 6.0 or do i have to bump the .net version for my project?
Unknown User•12mo ago
Message Not Public
Sign In & Join Server To View
And what is this worker thing? I didnt quite get the point why i should swap to it
Unknown User•12mo ago
Message Not Public
Sign In & Join Server To View
Beecause its built into the worker thing?
Unknown User•12mo ago
Message Not Public
Sign In & Join Server To View
oh okay
ill try
but do you think it might solve the configuration issue?
how are you consuming your configuration in services? I see you're using
services.Configure<>()
are you using IOptions<DiscordKeyOptions>
in your consumer classes?Unknown User•12mo ago
Message Not Public
Sign In & Join Server To View
For example like this:
then i can access like this to provide values when needed in thjat service:
yeah OK that looks right. And
options
is null is what the problem is?Unknown User•12mo ago
Message Not Public
Sign In & Join Server To View
Yes its null because loading the appsettings.json isnt working
pretty sure
IOptions<T>
can come up null if you have no configuration for that class. I mean, the actual variable won't be null, but .Value
will be nullUnknown User•12mo ago
Message Not Public
Sign In & Join Server To View
TeBeCo
is you repo open source <@230763299938107392> ?
Quoted by
<@689473681302224947> from #C# IConfiguration not loading appsettings (click here)
React with ❌ to remove this embed.
Yes its open source
Unknown User•12mo ago
Message Not Public
Sign In & Join Server To View
Maybe i should make it private repo
prefer not to share
Unknown User•12mo ago
Message Not Public
Sign In & Join Server To View
i know it would make this a lot easier
yeah ik
Unknown User•12mo ago
Message Not Public
Sign In & Join Server To View
But i dont want ppl to be able to contribute i think
Unknown User•12mo ago
Message Not Public
Sign In & Join Server To View
I know but like
Unknown User•12mo ago
Message Not Public
Sign In & Join Server To View
Yes
But then its diff
you see what i want you to see of my code
But i mean any console app that tries to load the configuration should fail
Dunno if i need to set path or soemthing for that file?
Unknown User•12mo ago
Message Not Public
Sign In & Join Server To View
And i guess its not suuuper relevant since thats only for development
Unknown User•12mo ago
Message Not Public
Sign In & Join Server To View
F5
Unknown User•12mo ago
Message Not Public
Sign In & Join Server To View
Yeye imn looking into migrating my project rn
Unknown User•12mo ago
Message Not Public
Sign In & Join Server To View
Yeah its on the same folder level as my console app
uh
i did download sdk 8.0
which got all runtime and other stuff
dotnet --list-sdks gives me 8.0.100 and a path to
C:/Program Files/dotnet/sdk
and i set net8.0
as my TargetFramework
in my csproj files.Unknown User•12mo ago
Message Not Public
Sign In & Join Server To View
after that the rebuild worked, nice
But now when running i get the bug again
That is beecause as ive understood it the IOptions gets a null value into CTOR due to the underlying issue which is what im trying to solve ===> Loading appsettings or somehow .env variables
That isnt user secrets
Unknown User•12mo ago
Message Not Public
Sign In & Join Server To View
Like create a seperate class/service => register it as a hosted service, and have that keep all values etc?
Instead of using directly the IConfiguration like i did there?
Unknown User•12mo ago
Message Not Public
Sign In & Join Server To View
okay
but isnt my issue that this:
Doesnt load env values?
or do need to access them another way?
Unknown User•12mo ago
Message Not Public
Sign In & Join Server To View
??
what do you mean
Unknown User•12mo ago
Message Not Public
Sign In & Join Server To View
???
Because:
CS0117 'Host' does not contain a definition for 'CreateApplicationBuilder'
Unknown User•12mo ago
Message Not Public
Sign In & Join Server To View
No i just migrated my existing app
to 8.0
Unknown User•12mo ago
Message Not Public
Sign In & Join Server To View
wait
ong
i realized
Unknown User•12mo ago
Message Not Public
Sign In & Join Server To View
u said earlier to swap in csproj
i did that and now it boots!
Host still doesnt allow anything but DefaultBuilder, but it boots now anyway..
I will consider refactoring the service instead of using IConfiguration values.. Thanks
Unknown User•12mo ago
Message Not Public
Sign In & Join Server To View