繁体   English   中英

无法解析类型“Microsoft.AspNetCore.DataProtection.IDataProtector”的服务

[英]Unable to resolve service for type '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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM