c# - 无法解析类型“Microsoft.AspNetCore.DataProtection.IDataProtector”的服务
问题描述
我有一个自定义服务,我正在使用 Microsoft 的IDataProtector
. 我已经在启动时注册了我的服务,AddScoped
但我在使用IDataProtector
.
错误信息:
尝试激活“服务”时无法解析“Microsoft.AspNetCore.DataProtection.IDataProtector”类型的服务。
这是我的代码:
public class Service: IInjectable
{
private readonly IDataProtector _protector;
public Service(IDataProtector protector)
{
_protector = protector.CreateProtector("key");
}
other code ...
}
我注册服务的方式:
public static void RegisterScoped<T>(
this IServiceCollection services, params Assembly[] assemblies)
{
IEnumerable<Type> types = assemblies.SelectMany(a => a.GetExportedTypes())
.Where(c => c.IsClass && typeof(T).IsAssignableFrom(c));
foreach (var item in types)
services.AddScoped(item);
}
启动 :
services.RegisterScoped<IInjectable>(typeof(IInjectable).Assembly);
解决方案
最后,我发现了问题:
我必须在我的服务构造函数中使用 IDataProtectionProvider 而不是 IDataProtector
推荐阅读
- java - Servlets - Ajax 重定向到主页
- android - 底部导航查看所选项目上的文本样式
- java - 当无效值作为实体类型传递时如何返回自定义错误消息?
- python - 如何将 XML 转换为 python AST
- java - 如何计算时间戳范围之间的小时和分钟总数(在特定范围内)
- python - 卷积神经网络 50% 准确率问题
- c# - 仅在发行版中存在 UWP 绑定问题
- javascript - Nodejs数组过滤和修改
- powershell - 用于设置/更新 sharepoint 中现有托管属性的 Powershell 脚本(不创建或获取它们)
- apache-spark - 使用 Spark WholeTextFiles 阅读时忽略空文件夹