这就是完成这项工作所需的全部内容,但在 Program 中进行此类播种更为常见.这使播种问题与配置管道分开,并且还允许使用 async/await.举个例子:公共类程序{公共静态异步任务 Main(string[] args){var host = CreateHostBuilder(args).Build();使用 (var scope = host.Services.CreateScope()){var serviceProvider = scope.ServiceProvider;var config = serviceProvider.GetRequiredService();等待 ApplicationDbContext.CreateAdminAccount(serviceProvider, config);}等待主机.RunAsync();}公共静态 IHostBuilder CreateHostBuilder(string[] args) =>//...}您也可以解析 Main 中的 UserManager 和 RoleManager,并将它们传递给 CreateAdminAccount 而不是让它使用服务定位器方法.When I run the project, I encounter this problem:(I've used asp.net core 3.)How can I solve this problem?ApplicationDbContext class:public class ApplicationDbContext : IdentityDbContext<User, Role, int,UserClaim, UserRole, UserLogin, RoleClaim, UserToken>{ public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options) { }public static async Task CreateAdminAccount(IServiceProviderserviceProvider, IConfiguration configuration) { UserManager<User> userManager =serviceProvider.GetRequiredService<UserManager<User>>(); RoleManager<Role> roleManager =serviceProvider.GetRequiredService<RoleManager<Role>>(); string userName = configuration["Data:AdminUser:Name"]; string email = configuration["Data:AdminUser:Email"]; string password = configuration["Data:AdminUser:Password"]; string role = configuration["Data:AdminUser:Role"]; if (await userManager.FindByNameAsync(userName) == null) { if (await roleManager.FindByNameAsync(role) == null) { await roleManager.CreateAsync(new Role(role)); } User user = new User { Email = email, UserName = userName }; var result = userManager.CreateAsync(user, password); if (result.IsCompletedSuccessfully) { await userManager.AddToRoleAsync(user, role); } } }}Details of error: 解决方案 From the screenshot you've included, it shows that you've got the following line in Startup.Configure:ApplicationDbContext.CreateAdminAccount(app.ApplicationServices, Configuration) .Wait();The IServiceProvider instance stored in IApplicationBuilder.ApplicationServices is the root service provider for your application. The error message states that it cannot resolve a scoped service from the root service provider.This issue comes up often, but the easiest solution for your scenario is to inject IServiceProvider into your Configure method and pass that into CreateAdminAccount:public void Configure(IApplicationBuilder app, IServiceProvider serviceProvider){ ApplicationDbContext.CreateAdminAccount(serviceProvider, Configuration) .Wait();}The instance of IServiceProvider that gets passed into Configure is scoped, which means that you can use it to create scoped services.That's all you need to make this work, but it's more typical to do this type of seeding in Program. That keeps seeding concerns separate from configuring the pipeline and it also allows the use of async/await. Here's an example:public class Program{ public static async Task Main(string[] args) { var host = CreateHostBuilder(args).Build(); using (var scope = host.Services.CreateScope()) { var serviceProvider = scope.ServiceProvider; var config = serviceProvider.GetRequiredService<IConfiguration>(); await ApplicationDbContext.CreateAdminAccount(serviceProvider, config); } await host.RunAsync(); } public static IHostBuilder CreateHostBuilder(string[] args) => // ...}You could resolve UserManager<User> and RoleManager<Role> in Main too, and pass those in to CreateAdminAccount rather than having it use the service-locator approach. 这篇关于ASP.NET Core 3:无法从根提供程序解析范围服务“Microsoft.AspNetCore.Identity.UserManager`1[Alpha.Models.Identity.User]"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-04 06:16
查看更多