zoukankan      html  css  js  c++  java
  • identityserver4的项目升级成.netcore3.1后出现:Microsoft.Extensions.Logging.ILogger`1[Microsoft.AspNetCore.Identity.UserManager`1[Services.IdentityServer4.ApplicationUser]]

    把.net core2.1的identityServer4的项目升级成3.1后,出现如下错误:

    System.InvalidOperationException: Unable to resolve service for type 'Microsoft.Extensions.Logging.ILogger`1[Microsoft.AspNetCore.Identity.UserManager`1[Services.IdentityServer4.ApplicationUser]]' while attempting to activate 'Microsoft.AspNetCore.Identity.UserManager`1[Services.IdentityServer4.ApplicationUser]'.

    原来在core 2.1的时候不报错,升级为3.1后报错了。

    报错的代码如下:

                string connectionString = SysCore.ConfigHelper.GetSectionValue("ConnectionStrings:DefaultConnection");
                var services = new ServiceCollection();
                services.AddDbContext<ApplicationDbContext>(options => options.UseMySql(connectionString));
                services.AddIdentity<ApplicationUser, IdentityRole>(
                        options =>
                        {
                            options.Password.RequireUppercase = false;
                            options.Password.RequireLowercase = false;
                            options.Password.RequireDigit = false;
                            options.Password.RequiredLength = 0;
                            options.Password.RequireNonAlphanumeric = false;
                            options.Password.RequiredUniqueChars = 0;
                        }
                    )
                    .AddEntityFrameworkStores<ApplicationDbContext>()
                    .AddDefaultTokenProviders();
    
                var serviceProvider = services.BuildServiceProvider();
    
                var userMgr = serviceProvider.GetRequiredService<UserManager<ApplicationUser>>();

    添加一行后解决:

                string connectionString = SysCore.ConfigHelper.GetSectionValue("ConnectionStrings:DefaultConnection");
                var services = new ServiceCollection();
                services.AddDbContext<ApplicationDbContext>(options => options.UseMySql(connectionString));
                services.AddLogging(logging => logging.AddConsole());
                services.AddIdentity<ApplicationUser, IdentityRole>(
                        options =>
                        {
                            options.Password.RequireUppercase = false;
                            options.Password.RequireLowercase = false;
                            options.Password.RequireDigit = false;
                            options.Password.RequiredLength = 0;
                            options.Password.RequireNonAlphanumeric = false;
                            options.Password.RequiredUniqueChars = 0;
                        }
                    )
                    .AddEntityFrameworkStores<ApplicationDbContext>()
                    .AddDefaultTokenProviders();
    
                var serviceProvider = services.BuildServiceProvider();
    
                var userMgr = serviceProvider.GetRequiredService<UserManager<ApplicationUser>>();
  • 相关阅读:
    调整浏览器兼容性的利器条件判断注释器
    Automatic uninstall IE9 and roll back to IE8
    How to use cmd with C#
    [转]在设计和执行测试用例的时候的几点心得和经验
    Move all files in subfolders to another folder using c#
    数据驱动单元测试实例
    测试web开源项目时如何收集代码覆盖率
    2013春节期间玩CentOS 6.3记录
    当使用ckeditor控件时,需要校验输入内容是否为空的一种解决办法(转帖)
    Enterprise Architect 8.0 注册码
  • 原文地址:https://www.cnblogs.com/wjx-blog/p/13666906.html
Copyright © 2011-2022 走看看