如何解析类型 'IdentityServer4.EntityFramework.Options.OperationalStoreOptions' 的服务

How to resolve service for type 'IdentityServer4.EntityFramework.Options.OperationalStoreOptions'

我创建了一个 aspnet 核心 Web 应用程序并安装了 IdentityServer4 和 IdentityServer4.EntityFramework 包以使用数据库存储而不是 inMemory 进行客户端和资源配置。但是,当我在服务集合中添加 ConfigurationDbContext 和 PersistedGrantDbContext 时,如下图所示 我得到一个例外 "Unable to resolve service for type 'IdentityServer4.EntityFramework.Options.OperationalStoreOptions' while attempting to activate 'IdentityServer4.EntityFramework.DbContexts.PersistedGrantDbContext'." 如下面的命令行屏幕截图所示

如何修复抛出的异常

如何解决这个问题

首先,我在第一个屏幕截图中删除了从第 49 行到第 53 行手动添加的 DbContext。 其次,我在添加 .AddSigningCredentials() 加载证书时犯了一个错误,我应该使用 IHostingEnvironment 将 ContentRootPath 获取到我的证书。因此将 .AddSigningCredentials(..) 更改为:

.AddSigningCredential(new X509Certificate2(Path.Combine(_environment.ContentRootPath,
                "sample-cert.pfx"), "password")

添加这就是我如何解决抛出的异常并处理我想要的迁移。

我已通过在 StartUp.cs 文件中添加以下行解决了此错误

 public void ConfigureServices(IServiceCollection services)
    {

        var storeOptions = new ConfigurationStoreOptions();
        services.AddSingleton(storeOptions);
     }

希望对你也有帮助