IdentityServer4 .net core 3.1找不到名为“ ConfigurationDbContext”的DbContext

时间:2019-12-21 16:48:38

标签: c# identityserver4 ef-migrations .net-core-3.1

我正在尝试使用 IdentityServer4(3.1.0) .net core 3.1 实现身份提供程序 当我尝试为ConfigurationDbContext添加迁移时,它给了我以下错误:

An error occurred while accessing the Microsoft.Extensions.Hosting services. Continuing without the application service provider. Error: Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType: IdentityServer4.Endpoints.AuthorizeCallbackEndpoint Lifetime: Transient ImplementationType: IdentityServer4.Endpoints.AuthorizeCallbackEndpoint': Cannot consume scoped service 'Guayaba.IDP2.Entities.UserContext' from singleton 'Guayaba.IDP2.Services.IGuayabaUserRepository'.) 
(Error while validating the service descriptor 'ServiceType: IdentityServer4.Endpoints.AuthorizeEndpoint Lifetime: Transient ImplementationType: IdentityServer4.Endpoints.AuthorizeEndpoint': Cannot consume scoped service 'Guayaba.IDP2.Entities.UserContext' from singleton 'Guayaba.IDP2.Services.IGuayabaUserRepository'.) 
(Error while validating the service descriptor 'ServiceType: IdentityServer4.Endpoints.EndSessionCallbackEndpoint Lifetime: Transient ImplementationType: IdentityServer4.Endpoints.EndSessionCallbackEndpoint': Cannot consume scoped service 'Guayaba.IDP2.Entities.UserContext' from singleton 'Guayaba.IDP2.Services.IGuayabaUserRepository'.) 
(Error while validating the service descriptor 'ServiceType: IdentityServer4.Endpoints.EndSessionEndpoint Lifetime: Transient ImplementationType: IdentityServer4.Endpoints.EndSessionEndpoint': Cannot consume scoped service 'Guayaba.IDP2.Entities.UserContext' from singleton 'Guayaba.IDP2.Services.IGuayabaUserRepository'.) 
(Error while validating the service descriptor 'ServiceType: IdentityServer4.Endpoints.IntrospectionEndpoint Lifetime: Transient ImplementationType: IdentityServer4.Endpoints.IntrospectionEndpoint': Cannot consume scoped service 'Guayaba.IDP2.Entities.UserContext' from singleton 'Guayaba.IDP2.Services.IGuayabaUserRepository'.) 
(Error while validating the service descriptor 'ServiceType: IdentityServer4.Endpoints.TokenEndpoint Lifetime: Transient ImplementationType: IdentityServer4.Endpoints.TokenEndpoint': Cannot consume scoped service 'Guayaba.IDP2.Entities.UserContext' from singleton 'Guayaba.IDP2.Services.IGuayabaUserRepository'.) (Error while validating the service descriptor 'ServiceType: IdentityServer4.Endpoints.UserInfoEndpoint Lifetime: Transient ImplementationType: IdentityServer4.Endpoints.UserInfoEndpoint': Cannot consume scoped service 'Guayaba.IDP2.Entities.UserContext' from singleton 'Guayaba.IDP2.Services.IGuayabaUserRepository'.) 
(Error while validating the service descriptor 'ServiceType: IdentityServer4.Services.ITokenService Lifetime: Transient ImplementationType: IdentityServer4.Services.DefaultTokenService': Cannot consume scoped service 'Guayaba.IDP2.Entities.UserContext' from singleton 'Guayaba.IDP2.Services.IGuayabaUserRepository'.) (Error while validating the service descriptor 'ServiceType: IdentityServer4.Services.IClaimsService Lifetime: Transient ImplementationType: IdentityServer4.Services.DefaultClaimsService': Cannot consume scoped service 'Guayaba.IDP2.Entities.UserContext' from singleton 'Guayaba.IDP2.Services.IGuayabaUserRepository'.) 
(Error while validating the service descriptor 'ServiceType: IdentityServer4.Validation.IEndSessionRequestValidator Lifetime: Transient ImplementationType: IdentityServer4.Validation.EndSessionRequestValidator': Cannot consume scoped service 'Guayaba.IDP2.Entities.UserContext' from singleton 'Guayaba.IDP2.Services.IGuayabaUserRepository'.) 
(Error while validating the service descriptor 'ServiceType: IdentityServer4.Validation.ITokenRequestValidator Lifetime: Transient ImplementationType: IdentityServer4.Validation.TokenRequestValidator': Cannot consume scoped service 'Guayaba.IDP2.Entities.UserContext' from singleton 'Guayaba.IDP2.Services.IGuayabaUserRepository'.) 
(Error while validating the service descriptor 'ServiceType: IdentityServer4.Validation.ITokenValidator Lifetime: Transient ImplementationType: IdentityServer4.Validation.TokenValidator': Cannot consume scoped service 'Guayaba.IDP2.Entities.UserContext' from singleton 'Guayaba.IDP2.Services.IGuayabaUserRepository'.) 
(Error while validating the service descriptor 'ServiceType: IdentityServer4.Validation.IIntrospectionRequestValidator Lifetime: Transient ImplementationType: IdentityServer4.Validation.IntrospectionRequestValidator': Cannot consume scoped service 'Guayaba.IDP2.Entities.UserContext' from singleton 'Guayaba.IDP2.Services.IGuayabaUserRepository'.) 
(Error while validating the service descriptor 'ServiceType: IdentityServer4.Validation.IUserInfoRequestValidator Lifetime: Transient ImplementationType: IdentityServer4.Validation.UserInfoRequestValidator': Cannot consume scoped service 'Guayaba.IDP2.Entities.UserContext' from singleton 'Guayaba.IDP2.Services.IGuayabaUserRepository'.) 
(Error while validating the service descriptor 'ServiceType: IdentityServer4.Validation.IDeviceCodeValidator Lifetime: Transient ImplementationType: IdentityServer4.Validation.DeviceCodeValidator': Cannot consume scoped service 'Guayaba.IDP2.Entities.UserContext' from singleton 'Guayaba.IDP2.Services.IGuayabaUserRepository'.) 
(Error while validating the service descriptor 'ServiceType: IdentityServer4.ResponseHandling.ITokenResponseGenerator Lifetime: Transient ImplementationType: IdentityServer4.ResponseHandling.TokenResponseGenerator': Cannot consume scoped service 'Guayaba.IDP2.Entities.UserContext' from singleton 'Guayaba.IDP2.Services.IGuayabaUserRepository'.) 
(Error while validating the service descriptor 'ServiceType: IdentityServer4.ResponseHandling.IUserInfoResponseGenerator Lifetime: Transient ImplementationType: IdentityServer4.ResponseHandling.UserInfoResponseGenerator': Cannot consume scoped service 'Guayaba.IDP2.Entities.UserContext' from singleton 'Guayaba.IDP2.Services.IGuayabaUserRepository'.) 
(Error while validating the service descriptor 'ServiceType: IdentityServer4.ResponseHandling.IAuthorizeInteractionResponseGenerator Lifetime: Transient ImplementationType: IdentityServer4.ResponseHandling.AuthorizeInteractionResponseGenerator': Cannot consume scoped service 'Guayaba.IDP2.Entities.UserContext' from singleton 'Guayaba.IDP2.Services.IGuayabaUserRepository'.) 
(Error while validating the service descriptor 'ServiceType: IdentityServer4.ResponseHandling.IAuthorizeResponseGenerator Lifetime: Transient ImplementationType: IdentityServer4.ResponseHandling.AuthorizeResponseGenerator': Cannot consume scoped service 'Guayaba.IDP2.Entities.UserContext' from singleton 'Guayaba.IDP2.Services.IGuayabaUserRepository'.) 
(Error while validating the service descriptor 'ServiceType: Guayaba.IDP2.Services.IGuayabaUserRepository Lifetime: Singleton ImplementationType: Guayaba.IDP2.Services.GuayabaUserRepository': Cannot consume scoped service 'Guayaba.IDP2.Entities.UserContext' from singleton 'Guayaba.IDP2.Services.IGuayabaUserRepository'.) 
(Error while validating the service descriptor 'ServiceType: IdentityServer4.Services.IProfileService Lifetime: Transient ImplementationType: Guayaba.IDP2.Services.GuayabaUserProfileService': Cannot consume scoped service 'Guayaba.IDP2.Entities.UserContext' from singleton 'Guayaba.IDP2.Services.IGuayabaUserRepository'.)
No DbContext named 'ConfigurationDbContext' was found.

在我的Startup.cs中,ConfigureServicesConfigure方法如下:

public void ConfigureServices(IServiceCollection services)
        {
            var connectionString = Configuration["connectionStrings:UserDBConnectionString"];
            services.AddDbContext<UserContext>(o => o.UseSqlServer(connectionString));

            services.AddScoped<IGuayabaUserRepository, GuayabaUserRepository>();

            var identityServerDataDBConnectionString =
               Configuration["connectionStrings:identityServerDataDBConnectionString"];

            var migrationsAssembly = typeof(Startup)
                .GetTypeInfo().Assembly.GetName().Name;

            services.AddControllersWithViews();

            services.AddIdentityServer()
                .AddSigningCredential(LoadCertificateFromStore())
                .AddGuayabaUserStore()
                .AddConfigurationStore(options =>
                    options.ConfigureDbContext = builder =>
                    builder.UseSqlServer(identityServerDataDBConnectionString, sqlOptions => sqlOptions.MigrationsAssembly(migrationsAssembly)))
                .AddOperationalStore(options =>
                    options.ConfigureDbContext = builder =>
                    builder.UseSqlServer(identityServerDataDBConnectionString, sqlOptions => sqlOptions.MigrationsAssembly(migrationsAssembly)));
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ConfigurationDbContext configurationDbContext,
            PersistedGrantDbContext persistedGrantDbContext)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
                // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                app.UseHsts();
            }

            configurationDbContext.Database.Migrate();
            configurationDbContext.EnsureSeedDataForContext();

            persistedGrantDbContext.Database.Migrate();

            app.UseIdentityServer();
            app.UseHttpsRedirection();
            app.UseStaticFiles();

            app.UseRouting();

            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllerRoute(
                    name: "default",
                    pattern: "{controller=Home}/{action=Index}/{id?}");
            });
        }

我安装了以下nuget软件包: IdentityServer4(3.1.0)
IdentityServer4.AccessTokenValidation(3.0.1)
IdentityServer4.EntityFrameworkCore(3.1.0)
Microsoft.EntityFrameworkCore.SqlServer(3.1.0)
Microsoft.EntityFrameworkCore.Tools(3.1.0)

当我尝试在包管理器控制台中执行以下命令后,发生了所描述的错误:
Add-migration IdentityServerInit -context ConfigurationDbContext

从昨天开始,我一直在网上搜索,但仍然没有答案。我已经在stackoverflow和github中阅读了许多与同一问题有关的问题,但到目前为止还没有运气。我什至从头开始创建了整个项目,只是看我是否在某个地方出错了,什么也没有(至今)。该错误消息对我而言毫无意义,因为我没有将任何内容注册为单例,至少不是我的所有存储库。

但是,值得注意的是,如果不对ConfigureServices上的services.AddIdentityServer部分进行注释,则无法为自己的上下文进行添加迁移。

你们会对如何解决这个问题有任何想法吗? 谢谢!

0 个答案:

没有答案