asp.net – InvalidOperationException:在程序集上找不到“UserSecretsIdAttribute”

将ASP.NET Core应用程序部署到天蓝色并打开网站后,会收到以下错误信息:

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

异常详细信息还包括代码行在Startup.cs发生错误

builder.AddUserSecrets();

谢谢

解决方法

最近有一个用户密码模块的更新。版本1.0.1及以上版本要求您为用户密码的id指定一个程式集属性,或者指定在之前在project.json中的方式。

以下是GitHub:https://github.com/aspnet/Announcements/issues/209的公告

你可以这样定义.csproj中的秘密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用户密码配置,并调用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中找到它。然后(as commented)返回一个关于缺少的属性错误,因为它们不想再投诉project.json,因为它已被弃用。

相关文章

### 创建一个gRPC服务项目(grpc服务端)和一个 webapi项目(...
一、SiganlR 使用的协议类型 1.websocket即时通讯协议 2.Ser...
.Net 6 WebApi 项目 在Linux系统上 打包成Docker镜像,发布为...
一、 PD简介PowerDesigner 是一个集所有现代建模技术于一身的...
一、存储过程 存储过程就像数据库中运行的方法(函数) 优点:...
一、Ueditor的下载 1、百度编辑器下载地址:http://ueditor....