I'm following the docs for Configuration in .Net, and having trouble getting it working with User Secrets in Visual Studio 2022 (.Net 6.0).
Thus far I've:
- Installed Microsoft.Extensions.Configuration.UserSecrets, and Microsoft.Extensions.Hosting.
- Confirmed that
<UserSecretsId>
was added to the .csproj file
Code
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
// Retrieve App Secrets
using IHost host = Host.CreateDefaultBuilder(args).Build();
IConfiguration config = host.Services.GetRequiredService<IConfiguration>();
string secret = config.GetValue<string>("DUMMY_VALUE");
...
await host.RunAsync();
secrets.json (Opened by right-clicking the project and choosing 'Manage User Secrets')
{
"DUMMY_VALUE": "dummy-test-value"
}
In the above, secret
is null. Based on this line from the docs, I thought the code above would create a default config capable of reading the secrets.json file.
It seems like the way this works has been updated since similar questions were asked and answered, like this one. I've also been referencing the docs on Secrets in ASP applications, but still having trouble spotting what I'm missing.