C# 如何解决类型为';IdentityServer4.EntityFramework.Options.OperationalStoreOptions';

C# 如何解决类型为';IdentityServer4.EntityFramework.Options.OperationalStoreOptions';,c#,asp.net-mvc,entity-framework,identityserver4,C#,Asp.net Mvc,Entity Framework,Identityserver4,我创建了一个aspnet核心web应用程序,并安装了IdentityServer4和IdentityServer4.EntityFramework包,以使用数据库存储来配置客户端和资源,而不是内存。但是,当我在服务集合中添加ConfigurationDbContext和PersistedGrandDBContext时,如下图所示 我在尝试激活“IdentityServer4.EntityFramework.DbContexts.PersistedGrandDBContext”时遇到异常,提示“无

我创建了一个aspnet核心web应用程序,并安装了IdentityServer4和IdentityServer4.EntityFramework包,以使用数据库存储来配置客户端和资源,而不是内存。但是,当我在服务集合中添加ConfigurationDbContext和PersistedGrandDBContext时,如下图所示 我在尝试激活“IdentityServer4.EntityFramework.DbContexts.PersistedGrandDBContext”时遇到异常,提示“无法解析类型为“IdentityServer4.EntityFramework.Options.OperationalStoreOptions”的服务。” 如下面的命令行屏幕截图所示


如何修复引发的异常

如何解决此问题

首先,在第一个屏幕截图中,我删除了从第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);
     }

希望这也能帮助您

您能澄清为什么您手动注册类型,而不使用
服务。AddIdentityServer().AddConfigurationStore(builder=>…)
根据文档的扩展方法吗?当我不手动时,我会发现一个错误,比如没有找到名为“PersistedGrandDBContext”的DbContext。如果你看第63行,我确实使用了AddConfigurationStore扩展方法,但在添加迁移时,它抛出了一个错误,我认为它应该这样做。您确定要同时使用
AddOperationalStore
AddConfigurationStore
来配置持久存储吗?是的,我正在使用它们both@moritzg非常感谢,我使用了try-catch,并且能够解决问题。我发布了我的答案。这对我没有任何帮助。