Mvc: No service for type 'Microsoft.AspNetCore.Identity.UserManager has been registered. ??

Created on 1 Jun 2016  Â·  14Comments  Â·  Source: aspnet/Mvc

migrating UserManagement in different class library gives this Error:
No service for type 'Microsoft.AspNetCore.Identity.UserManager has been registered. ??

no repro

Most helpful comment

My guess is there's a type mismatch somewhere, i.e. asking for UserManager<IdentityUser> instead of UserManager<ApplicationUser> or something along those lines which causes the service not to be found. Without any more relevant code there's nothing we can do other than speculate...

All 14 comments

@HaoK does this sound familiar to you?

Are you calling AddIdentity in ConfigureServices on Startup.cs?

Yes
In ConfigureService on Startup.cs i have called

services.AddIdentity`<ApplicationUser,ApplicationRole>`()
            .AddEntityFrameworkStores<ApplicationDbContext>()
            .AddDefaultTokenProviders();

what should i do for this

Hard to say, can you include the relevant code in the ctor and startup?

@HaoK any further idea on this?

My guess is there's a type mismatch somewhere, i.e. asking for UserManager<IdentityUser> instead of UserManager<ApplicationUser> or something along those lines which causes the service not to be found. Without any more relevant code there's nothing we can do other than speculate...

So it is.

I hope this doesn't reopen this issue, but I can comment and explain what happened (or at least assume). I ran into the same issue and it was because during my testing, I renamed ApplicationUser to my own name (SomethingUser) and then decided to move the class to another class library. The problem was that I left the SomethingUser defined in my web project thus the constructor for the UserManager didn't know which one I was targeting when in my ConfigureServices I identified it as the SomethingUser in my class library. Great hint HaoK, it helped me correct my own issue. It was a type mismatch.

It seems @ermithun you need to take care of scope as it is scoped:

using (var serviceScope = _serviceProvider.GetRequiredService<IServiceScopeFactory>().CreateScope())
{
    var userManager = serviceScope.ServiceProvider.GetService<UserManager<ApplicationUser>>();
}

In _LoginPartial.cshtml replace the following:
SignInManager<IdentityUser> and UserManager<IdentityUser>
with:
SignInManager<ApplicationUser> and SignInManager<ApplicationUser>.

_LoginPartial.cshtml change to:

@*@inject SignInManager<IdentityUser> SignInManager
  @inject UserManager<IdentityUser> UserManager*@
@inject SignInManager<ApplicationUser> SignInManager
@inject UserManager<ApplicationUser> UserManager

@HaoK I thought if ApplicationUser is deriving IdentityUser the an is-a will work???? but it doesnt, you will have to put the type explicitly

@andersonphiri That's not the case, you need to have the specific user type for DI purposes

Noted and thanks you. Yes DI workis with specific type

On Fri, Oct 5, 2018 at 2:21 AM Javier Calvarro Nelson <
[email protected]> wrote:

@andersonphiri https://github.com/andersonphiri That's not the case,
you need to have the specific user type for DI purposes

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/aspnet/Mvc/issues/4783#issuecomment-427120378, or mute
the thread
https://github.com/notifications/unsubscribe-auth/ALZITXxzdH3nvhA0t8V04qBGPEG1wGPMks5uhlG3gaJpZM4IrLfu
.

--
Anderson Phiri
Honors in Business Studies and Computing Science Technology(BSCT) - 2013 to
2017
Institution: University Of Zimbabwe

Contacts: +263 777 619 600

Noted and thanks you. Yes DI works with specific type

On Tue, Oct 16, 2018 at 8:20 AM Anderson Phiri phirianderson23@gmail.com
wrote:

Noted and thanks you. Yes DI workis with specific type

On Fri, Oct 5, 2018 at 2:21 AM Javier Calvarro Nelson <
[email protected]> wrote:

@andersonphiri https://github.com/andersonphiri That's not the case,
you need to have the specific user type for DI purposes

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/aspnet/Mvc/issues/4783#issuecomment-427120378, or mute
the thread
https://github.com/notifications/unsubscribe-auth/ALZITXxzdH3nvhA0t8V04qBGPEG1wGPMks5uhlG3gaJpZM4IrLfu
.

--
Anderson Phiri
Honors in Business Studies and Computing Science Technology(BSCT) - 2013
to 2017
Institution: University Of Zimbabwe

Contacts: +263 777 619 600

--
Anderson Phiri
Honors in Business Studies and Computing Science Technology(BSCT) - 2013 to
2017
Institution: University Of Zimbabwe

Contacts: +263 777 619 600

Was this page helpful?
0 / 5 - 0 ratings