When I run the project, I encounter this problem:
(I've used asp.net core 3
.)
Cannot resolve scoped service 'Microsoft.AspNetCore.Identity.UserManager`1[Alpha.Models.Identity.User]' from root provider.
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(IServiceProvider
serviceProvider, 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:
An error occurred while starting the application. AggregateException:
One or more errors occurred. (Cannot resolve scoped service
'Microsoft.AspNetCore.Identity.UserManager`1[Alpha.Models.Identity.User]'
from root provider.)
System.Threading.Tasks.Task.ThrowIfExceptional(bool
includeTaskCanceledExceptions)
InvalidOperationException: Cannot resolve scoped service
'Microsoft.AspNetCore.Identity.UserManager`1[Alpha.Models.Identity.User]'
from root provider.
Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteValidator.ValidateResolution(Type
serviceType, IServiceScope scope, IServiceScope rootScope)
AggregateException: One or more errors occurred. (Cannot resolve
scoped service
'Microsoft.AspNetCore.Identity.UserManager`1[Alpha.Models.Identity.User]'
from root provider.)
System.Threading.Tasks.Task.ThrowIfExceptional(bool
includeTaskCanceledExceptions) System.Threading.Tasks.Task.Wait(int
millisecondsTimeout, CancellationToken cancellationToken)
System.Threading.Tasks.Task.Wait()
Alpha.Web.App.Startup.Configure(IApplicationBuilder app,
IWebHostEnvironment env) in Startup.cs
+
ApplicationDbContext.CreateAdminAccount(app.ApplicationServices,
Configuration).Wait(); System.RuntimeMethodHandle.InvokeMethod(object
target, object[] arguments, Signature sig, bool constructor, bool
wrapExceptions) System.Reflection.RuntimeMethodInfo.Invoke(object obj,
BindingFlags invokeAttr, Binder binder, object[] parameters,
CultureInfo culture)
Microsoft.AspNetCore.Hosting.ConfigureBuilder.Invoke(object instance,
IApplicationBuilder builder)
Microsoft.AspNetCore.Hosting.ConfigureBuilder+<>c__DisplayClass4_0.b__0(IApplicationBuilder
builder)
Microsoft.AspNetCore.Hosting.GenericWebHostBuilder+<>c__DisplayClass13_0.b__2(IApplicationBuilder
app)
Microsoft.AspNetCore.Mvc.Filters.MiddlewareFilterBuilderStartupFilter+<>c__DisplayClass0_0.g__MiddlewareFilterBuilder|0(IApplicationBuilder
builder)
Microsoft.AspNetCore.Server.IIS.Core.IISServerSetupFilter+<>c__DisplayClass2_0.b__0(IApplicationBuilder
app)
Microsoft.AspNetCore.HostFilteringStartupFilter+<>c__DisplayClass0_0.b__0(IApplicationBuilder
app)
Microsoft.AspNetCore.Hosting.GenericWebHostService.StartAsync(CancellationToken
cancellationToken)
Microsoft.Extensions.Hosting.Internal.Host.StartAsync(CancellationToken
cancellationToken)
Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.RunAsync(IHost
host, CancellationToken token)
Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.RunAsync(IHost
host, CancellationToken token)
Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.Run(IHost
host) Alpha.Web.App.Program.Main(string[] args) in Program.cs
+
CreateHostBuilder(args).Build().Run();
Show raw exception details InvalidOperationException: Cannot resolve
scoped service
'Microsoft.AspNetCore.Identity.UserManager`1[Alpha.Models.Identity.User]'
from root provider.
Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteValidator.ValidateResolution(Type
serviceType, IServiceScope scope, IServiceScope rootScope)
Microsoft.Extensions.DependencyInjection.ServiceProvider.Microsoft.Extensions.DependencyInjection.ServiceLookup.IServiceProviderEngineCallback.OnResolve(Type
serviceType, IServiceScope scope)
Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngine.GetService(Type
serviceType, ServiceProviderEngineScope serviceProviderEngineScope)
Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngineScope.GetService(Type
serviceType)
Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider
provider, Type serviceType)
Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider
provider)
Alpha.DataAccess.ApplicationDbContext.CreateAdminAccount(IServiceProvider
serviceProvider, IConfiguration configuration) in
ApplicationDbContext.cs
+
UserManager userManager = serviceProvider.GetRequiredService>();
Show raw exception details System.InvalidOperationException: Cannot
resolve scoped service
'Microsoft.AspNetCore.Identity.UserManager`1[Alpha.Models.Identity.User]'
from root provider. at
Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteValidator.ValidateResolution(Type
serviceType, IServiceScope scope, IServiceScope rootScope) at
Microsoft.Extensions.DependencyInjection.ServiceProvider.Microsoft.Extensions.DependencyInjection.ServiceLookup.IServiceProviderEngineCallback.OnResolve(Type
serviceType, IServiceScope scope) at
Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngine.GetService(Type
serviceType, ServiceProviderEngineScope serviceProviderEngineScope)
at
Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngineScope.GetService(Type
serviceType) at
Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider
provider, Type serviceType) at
Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider
provider) at
Alpha.DataAccess.ApplicationDbContext.CreateAdminAccount(IServiceProvider
serviceProvider, IConfiguration configuration) in
E:ArchivesProjectsAlphaAlpha.DataAccessApplicationDbContext.cs:line
92 .NET Core 3.0.0 X64 v4.0.0.0 | Microsoft.AspNetCore.Hosting
version 3.0.0-rc2.19465.2 | Microsoft Windows 10.0.17763 |
Need help?
See Question&Answers more detail:
os