Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Could not find 'UserSecretsIdAttribute' on assembly but it exists and correct package is added

I follow this tutorial: https://docs.microsoft.com/en-us/aspnet/core/security/app-secrets?view=aspnetcore-3.1&tabs=windows#access-a-secret

To a .NET Core 3.1 project I've added Microsoft.Extensions.Configuration.UserSecrets package, I have clicked on the project to manage secrets, the file has been created in the AppData directory and the UserSecretsId has been added automatically in .csproj file. Because the Host.CreateDefaultBuilder didn't load secrets I decided to add them manually

 if (hostContext.HostingEnvironment.IsDevelopment())
        {
            builder.AddUserSecrets(Assembly.GetExecutingAssembly());
        }
    })
  .Build();

But then I get

System.InvalidOperationException
  HResult=0x80131509
  Message=Could not find 'UserSecretsIdAttribute' on assembly 'XXX'.
Check that the project for 'XXX' has set the 'UserSecretsId' build property.
If the 'UserSecretsId' property is already set then add a reference to the Microsoft.Extensions.Configuration.UserSecrets package.
  Source=Microsoft.Extensions.Configuration.UserSecrets

I've checked the suggestion in the expecion's description and both predicaments are fulfilled.

like image 620
Yoda Avatar asked Sep 29 '20 09:09

Yoda


Video Answer


2 Answers

Same issue here. after check the source code in GitHub https://github.com/aspnet/Configuration/blob/f64994e0655659faefccead7ccb5c1edbfd4d4ba/src/Config.UserSecrets/UserSecretsConfigurationExtensions.cs#L94

Check if you have <GenerateAssemblyInfo>true</GenerateAssemblyInfo> in your .csproj file as well.

like image 169
Jun Avatar answered Oct 07 '22 21:10

Jun


You should have the UserSecretsId listed in your .csproj file. If you right click on the API layer and manage user secrets confirm that the GUID matches in the AppData directory with whats in the project.

<PropertyGroup>
<UserSecretsId>Your GUID</UserSecretsId>

Your Program.cs class should be using the CreateDefaultBuilder, you should see that there is a section to include Secrets when the environment is Development. Check your ASP "ASPNETCORE_ENVIRONMENT": "Development" is set to development. Lastly I'm assuming your executing assembly has the PropertyGroup for the user secrets mentioned above.

 public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder.UseStartup<Startup>();
            });
like image 45
Darren Avatar answered Oct 07 '22 23:10

Darren