InvalidOperationException:在程序集上找不到“UserSecretsIdAttribute" [英] InvalidOperationException: Could not find 'UserSecretsIdAttribute' on assembly

查看:32
本文介绍了InvalidOperationException:在程序集上找不到“UserSecretsIdAttribute"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将 ASP.NET Core 应用部署到 azure 并打开网站后,出现以下错误:

After deploying ASP.NET Core app to azure and opening the site, I get the following error:

InvalidOperationException: 找不到 'UserSecretsIdAttribute'程序集 '*****,版本=1.0.0.0,文化=中性,PublicKeyToken=null'.

InvalidOperationException: Could not find 'UserSecretsIdAttribute' on assembly '******, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.

异常细节还包括错误发生在 Startup.cs 这行代码上:

The exception details also include that the error happens at Startup.cs on this line of code:

builder.AddUserSecrets();

builder.AddUserSecrets();

谢谢

推荐答案

最近对用户机密模块进行了更新.1.0.1 及更高版本现在要求您为用户机密的 id 指定程序集级别的属性,或者作为回退,就像以前在 project.json 中一样.

There was an update to the user secrets module just recently. Version 1.0.1 and up now requires you specify an assembly-level attribute for the id of the user secrets, or as a fallback, the way it was previously in project.json.

这是 GitHub 上的公告:https://github.com/aspnet/Announcements/issues/209

Here is the announcement on GitHub: https://github.com/aspnet/Announcements/issues/209

您可以像这样在 .csproj 中定义机密 ID:

You can define the secrets id in the .csproj like this:

<PropertyGroup>
  <UserSecretsId>aspnet-TestApp-ce345b64-19cf-4972-b34f-d16f2e7976ed</UserSecretsId>
</PropertyGroup>

这会生成以下程序集级属性.或者,您当然可以自己添加,而不是将其添加到 .csproj 文件中,例如到 Startup.cs:

This generates the following assembly-level attribute. Alternatively, instead of adding it in the .csproj file, you can of course add it yourself e.g. to Startup.cs:

[assembly: UserSecretsId("aspnet-TestApp-ce345b64-19cf-4972-b34f-d16f2e7976ed")]

另外,你应该使用:

builder.AddUserSecrets<Startup>();

它将在给定类型的程序集中搜索该属性,在本例中我使用了 Startup 类.

It will search for that attribute in the assembly of the given type, in this case I used the Startup class.

注意:这将在 2.0 中弃用:(1.0.2 和 1.1.1 已将其标记为过时)

builder.AddUserSecrets();

我检查了 source code 用于用户机密配置,并在没有类型的情况下调用 AddUserSecrets() 执行以下操作:

I checked the source code for the user secrets configuration, and calling AddUserSecrets() without the type does this:

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,因为它已被弃用.

It's trying to find the UserSecretsId attribute on your assembly, and failing that, checking if it could find it in project.json. Then (as commented) returns an error about the missing attribute as they wouldn't want to complain about project.json anymore as it is being deprecated.

这篇关于InvalidOperationException:在程序集上找不到“UserSecretsIdAttribute"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆