将 ASP.NET Core 应用程序部署到 azure 并打开该网站后,出现以下错误:
InvalidOperationException: Could not find 'UserSecretsIdAttribute' on assembly '******, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.
异常详细信息还包括错误发生在 Startup.cs 的这行代码上:
builder.AddUserSecrets();
谢谢
最佳答案
最近对用户 secret 模块进行了更新。版本 1.0.1 及更高版本现在要求您为用户 secret 的 id 指定程序集级属性,或者作为后备,就像以前在 project.json 中的方式一样。
以下是 GitHub 上的公告:https://github.com/aspnet/Announcements/issues/209
您可以在 .csproj 中定义 secret ID,如下所示:
<PropertyGroup>
<UserSecretsId>aspnet-TestApp-ce345b64-19cf-4972-b34f-d16f2e7976ed</UserSecretsId>
</PropertyGroup>
这会生成以下程序集级属性。或者,您当然可以自己添加,而不是将其添加到 .csproj 文件中,例如到 Startup.cs:
[assembly: UserSecretsId("aspnet-TestApp-ce345b64-19cf-4972-b34f-d16f2e7976ed")]
此外,您应该使用:
builder.AddUserSecrets<Startup>();
它将在给定类型的程序集中搜索该属性,在本例中我使用了 Startup 类。
注意:这将在 2.0 中被弃用:(1.0.2 和 1.1.1 已将其标记为过时)
builder.AddUserSecrets();
我检查了source code对于用户 secret 配置,调用不带类型的 AddUserSecrets()
会执行以下操作:
var attribute = entryAssembly.GetCustomAttribute<UserSecretsIdAttribute>();
if (attribute != null)
{
return AddUserSecrets(configuration, attribute.UserSecretsId);
}
// try fallback to project.json for legacy support
try
{
var fileProvider = configuration.GetFileProvider();
return AddSecretsFile(configuration, PathHelper.GetSecretsPath(fileProvider));
}
catch
{ }
// Show the error about missing UserSecretIdAttribute instead an error about missing
// project.json as PJ is going away.
throw MissingAttributeException(entryAssembly);
它尝试在程序集中查找 UserSecretsId
属性,如果失败,则检查是否可以在 project.json 中找到它。然后(如评论所述)返回有关缺少属性的错误,因为他们不想再提示project.json,因为它已被弃用。
关于asp.net - 无效操作异常 : Could not find 'UserSecretsIdAttribute' on assembly,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40704760/