C
C#12mo ago
Tiltyboi

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?
var builder = new ConfigurationBuilder()
.AddJsonFile("appsettings.json", true, true)
//.AddUserSecrets<Program>()
.AddEnvironmentVariables();
_config = builder.Build();
var builder = new ConfigurationBuilder()
.AddJsonFile("appsettings.json", true, true)
//.AddUserSecrets<Program>()
.AddEnvironmentVariables();
_config = builder.Build();
But when i user User Secrets it works fine.
72 Replies
Renn
Renn12mo ago
how are you trying to use it? and what do you mean by "it's not found"?
Unknown User
Unknown User12mo ago
Message Not Public
Sign In & Join Server To View
Tiltyboi
TiltyboiOP12mo ago
Alrighty, here it comes:
Unknown User
Unknown User12mo ago
Message Not Public
Sign In & Join Server To View
Tiltyboi
TiltyboiOP12mo ago
Yeah okay
Unknown User
Unknown User12mo ago
Message Not Public
Sign In & Join Server To View
Tiltyboi
TiltyboiOP12mo ago
What is already part of the builder?
Unknown User
Unknown User12mo ago
Message Not Public
Sign In & Join Server To View
Tiltyboi
TiltyboiOP12mo ago
?
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<UserSecretsId>my user secrets id</UserSecretsId>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Discord.Addons.Hosting" Version="5.1.0" />
<PackageReference Include="Discord.Net" Version="3.7.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="6.0.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="6.0.1" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.0" />
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.17.2" />
<PackageReference Include="TenorSharp" Version="1.6.1" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\SadBot.Core\SadBot.Core.csproj" />
<ProjectReference Include="..\SadBot.Logging\SadBot.Logging.csproj" />
</ItemGroup>
</Project>
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<UserSecretsId>my user secrets id</UserSecretsId>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Discord.Addons.Hosting" Version="5.1.0" />
<PackageReference Include="Discord.Net" Version="3.7.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="6.0.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="6.0.1" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.0" />
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.17.2" />
<PackageReference Include="TenorSharp" Version="1.6.1" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\SadBot.Core\SadBot.Core.csproj" />
<ProjectReference Include="..\SadBot.Logging\SadBot.Logging.csproj" />
</ItemGroup>
</Project>
Unknown User
Unknown User12mo ago
Message Not Public
Sign In & Join Server To View
Tiltyboi
TiltyboiOP12mo ago
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
Unknown User12mo ago
Message Not Public
Sign In & Join Server To View
Tiltyboi
TiltyboiOP12mo ago
So the placement in my csproj or the worker valuie is wrong?
Unknown User
Unknown User12mo ago
Message Not Public
Sign In & Join Server To View
Tiltyboi
TiltyboiOP12mo ago
Does worker exist for 6.0 or do i have to bump the .net version for my project?
Unknown User
Unknown User12mo ago
Message Not Public
Sign In & Join Server To View
Tiltyboi
TiltyboiOP12mo ago
And what is this worker thing? I didnt quite get the point why i should swap to it
Unknown User
Unknown User12mo ago
Message Not Public
Sign In & Join Server To View
Tiltyboi
TiltyboiOP12mo ago
Beecause its built into the worker thing?
Unknown User
Unknown User12mo ago
Message Not Public
Sign In & Join Server To View
Tiltyboi
TiltyboiOP12mo ago
oh okay ill try but do you think it might solve the configuration issue?
Renn
Renn12mo ago
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
Unknown User12mo ago
Message Not Public
Sign In & Join Server To View
Tiltyboi
TiltyboiOP12mo ago
For example like this:
public WorkerService(DiscordSocketClient client, CommandService commands, TenorClient tenorClient,
ILogger<DiscordClientService> logger, IOptions<DiscordKeyOptions> options)
public WorkerService(DiscordSocketClient client, CommandService commands, TenorClient tenorClient,
ILogger<DiscordClientService> logger, IOptions<DiscordKeyOptions> options)
then i can access like this to provide values when needed in thjat service:
_options.GuildId;
_options.GuildId;
Renn
Renn12mo ago
yeah OK that looks right. And options is null is what the problem is?
Unknown User
Unknown User12mo ago
Message Not Public
Sign In & Join Server To View
Tiltyboi
TiltyboiOP12mo ago
Yes its null because loading the appsettings.json isnt working
Renn
Renn12mo ago
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 null
Unknown User
Unknown User12mo ago
Message Not Public
Sign In & Join Server To View
MODiX
MODiX12mo ago
TeBeCo
is you repo open source <@230763299938107392> ?
React with ❌ to remove this embed.
Tiltyboi
TiltyboiOP12mo ago
Yes its open source
Unknown User
Unknown User12mo ago
Message Not Public
Sign In & Join Server To View
Tiltyboi
TiltyboiOP12mo ago
Maybe i should make it private repo prefer not to share
Unknown User
Unknown User12mo ago
Message Not Public
Sign In & Join Server To View
Tiltyboi
TiltyboiOP12mo ago
i know it would make this a lot easier yeah ik
Unknown User
Unknown User12mo ago
Message Not Public
Sign In & Join Server To View
Tiltyboi
TiltyboiOP12mo ago
But i dont want ppl to be able to contribute i think
Unknown User
Unknown User12mo ago
Message Not Public
Sign In & Join Server To View
Tiltyboi
TiltyboiOP12mo ago
I know but like
Unknown User
Unknown User12mo ago
Message Not Public
Sign In & Join Server To View
Tiltyboi
TiltyboiOP12mo ago
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
Unknown User12mo ago
Message Not Public
Sign In & Join Server To View
Tiltyboi
TiltyboiOP12mo ago
And i guess its not suuuper relevant since thats only for development
Unknown User
Unknown User12mo ago
Message Not Public
Sign In & Join Server To View
Tiltyboi
TiltyboiOP12mo ago
F5
Unknown User
Unknown User12mo ago
Message Not Public
Sign In & Join Server To View
Tiltyboi
TiltyboiOP12mo ago
Yeye imn looking into migrating my project rn
Unknown User
Unknown User12mo ago
Message Not Public
Sign In & Join Server To View
Tiltyboi
TiltyboiOP12mo ago
Yeah its on the same folder level as my console app
Tiltyboi
TiltyboiOP12mo ago
No description
Tiltyboi
TiltyboiOP12mo ago
uh
Tiltyboi
TiltyboiOP12mo ago
No description
Tiltyboi
TiltyboiOP12mo ago
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
Unknown User12mo ago
Message Not Public
Sign In & Join Server To View
Tiltyboi
TiltyboiOP12mo ago
after that the rebuild worked, nice But now when running i get the bug again
Tiltyboi
TiltyboiOP12mo ago
No description
Tiltyboi
TiltyboiOP12mo ago
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
Unknown User12mo ago
Message Not Public
Sign In & Join Server To View
Tiltyboi
TiltyboiOP12mo ago
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
Unknown User12mo ago
Message Not Public
Sign In & Join Server To View
Tiltyboi
TiltyboiOP12mo ago
okay but isnt my issue that this:
var builder = new ConfigurationBuilder()
.AddJsonFile("appsettings.json", true, true)
//.AddUserSecrets<Program>()
.AddEnvironmentVariables();
_config = builder.Build();
var builder = new ConfigurationBuilder()
.AddJsonFile("appsettings.json", true, true)
//.AddUserSecrets<Program>()
.AddEnvironmentVariables();
_config = builder.Build();
Doesnt load env values? or do need to access them another way?
Unknown User
Unknown User12mo ago
Message Not Public
Sign In & Join Server To View
Tiltyboi
TiltyboiOP12mo ago
?? what do you mean
Unknown User
Unknown User12mo ago
Message Not Public
Sign In & Join Server To View
Tiltyboi
TiltyboiOP12mo ago
var host = Host.CreateDefaultBuilder()
.ConfigureDiscordHost((context, config) =>
var host = Host.CreateDefaultBuilder()
.ConfigureDiscordHost((context, config) =>
??? Because: CS0117 'Host' does not contain a definition for 'CreateApplicationBuilder'
Unknown User
Unknown User12mo ago
Message Not Public
Sign In & Join Server To View
Tiltyboi
TiltyboiOP12mo ago
No i just migrated my existing app to 8.0
Unknown User
Unknown User12mo ago
Message Not Public
Sign In & Join Server To View
Tiltyboi
TiltyboiOP12mo ago
wait ong i realized
Unknown User
Unknown User12mo ago
Message Not Public
Sign In & Join Server To View
Tiltyboi
TiltyboiOP12mo ago
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 ThumbsUp
Unknown User
Unknown User12mo ago
Message Not Public
Sign In & Join Server To View
Want results from more Discord servers?
Add your server