Skip to content

UserSecretsId attribute not generated when M.E.C.UserSecrets reference transitively #4007

Closed

Description

Overview

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: dotnet/extensions#2743 dotnet/AspNetCore.Docs#14315

Repro Steps

Repro repo (just clone and skip to step 3): https://github.com/anurse/UserSecretsInWorkerRepro

  1. dotnet new worker
  2. Edit ExecuteAsync in Worker.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);
    }
}
  1. Set a user secret value: dotnet user-secrets set "SecretName" "SecretValue"
  2. 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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions