UserSecretsId attribute not generated when M.E.C.UserSecrets reference transitively
See original GitHub issueOverview
The logic that injects the UserSecretsIdAttribute
appears to only work if the Microsoft.Extensions.Configuration.UserSecrets
package is reference directly (or through the ASP.NET Core runtime). The worker template references it transitively through Microsoft.Extensions.Hosting
and the attribute is never generated and user secrets are not loaded.
This was originally reported by customers: https://github.com/aspnet/Extensions/issues/2743 https://github.com/aspnet/AspNetCore.Docs/issues/14315
Repro Steps
Repro repo (just clone and skip to step 3): https://github.com/anurse/UserSecretsInWorkerRepro
dotnet new worker
- Edit
ExecuteAsync
inWorker.cs
to the following
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
while (!stoppingToken.IsCancellationRequested)
{
_logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now);
_logger.LogInformation("The secret is: {Secret}", _config["SecretName"]);
await Task.Delay(1000, stoppingToken);
}
}
- Set a user secret value:
dotnet user-secrets set "SecretName" "SecretValue"
- Run with
dotnet run
Expected Results
The secret value is written in a log message like:
info: UserSecretsInWorkerRepro.Worker[0]
Worker running at: 12/05/2019 11:51:07 -08:00
info: UserSecretsInWorkerRepro.Worker[0]
The secret is: SecretValue
Actual Results
The secret value is not present
info: UserSecretsInWorkerRepro.Worker[0]
Worker running at: 12/05/2019 11:51:07 -08:00
info: UserSecretsInWorkerRepro.Worker[0]
The secret is: (null)
Issue Analytics
- State:
- Created 4 years ago
- Reactions:8
- Comments:12 (2 by maintainers)
Top Results From Across the Web
Using Secret Manager in .NET Console Apps
See UserSecretsId attribute not generated when M.E.C.UserSecrets reference transitively. And, more info on what the shared framework is here ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Still an issue in .NET 6 RC2 with worker services. Adding a reference to M.E.Configuration.UserSecrets still works as a workaround.
Sorry, but this is not really a “fix”, having a transitive and a direct dependency on the same package makes it confusing to look at for developers who might look at the two and go, “hmm…I guess it was an oversight, I will get rid of the direct ref and remove the ‘duplication’”, and that will break local development/debugging for other developers on the team. I am a bit shocked that for around a year, this has been peddled as a “fix”!
In the absence of a real fix, the next best thing would be to either:
Manually add an
AssemblyInfo.cs
into the executable project and add theUserSecretsId
attribute (basically what should happen out of the box), or,Do what @hbermani did, that also works i.e.
config.AddUserSecrets(<UserSecretIdFromTheProject.csproj>)
If its in code then at least it wouldn’t be removed without somebody noticing it, redundant packages can easily be removed during refactors and they can easily sneak past PR reviews without raising suspicion.