0

I have .net identity setup in my startup:

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

And this works fine in the controller with constructor injection.

However If I tell .net to inject another class, ie:

services.AddScoped<WebSocketEvent>();

and then in the class:

public class WebSocketEvent
{
    IHubContext<SignalHub> _hubcontext;
    UserManager<ApplicationUser> _userManager;

    public WebSocketEvent(IHubContext<SignalHub> hubcontext, UserManager<ApplicationUser> userManager)
    {
        _hubcontext = hubcontext;
        _userManager = userManager;     
    }
}

It cant make/inject the userManager and I get:

InvalidOperationException: Unable to resolve service for type 'Microsoft.AspNetCore.Identity.UserManager`1[WebSockets.Models.ApplicationUser]' while attempting to activate 'WebSockets.Services.WebSocketEvent'.

Kirk Larkin
  • 60,745
  • 11
  • 150
  • 162
CerIs
  • 477
  • 5
  • 13
  • 1
    Is `WebSockets.Models.ApplicationUser` the *only* class in your project that's called `ApplicationUser` or do you have another `ApplicationUser` in a different namespace? – Kirk Larkin Oct 15 '18 at 09:38
  • Yes only one (this is just the default site that .net creates from new in visual studio with .net identity and MVC) – CerIs Oct 15 '18 at 10:07
  • Is there an inner exception as part of this exception? – Cake or Death Oct 15 '18 at 10:30
  • Dont get the error in visual studio, only shows a webpage with : InvalidOperationException: Unable to resolve service for type 'Microsoft.AspNetCore.Identity.UserManager`1[WebSockets.Models.ApplicationUser]' while attempting to activate 'WebSockets.Services.WebSocketEvent'. - Cant see anything labeled inner exception – CerIs Oct 15 '18 at 10:35
  • @CerIs I have checked with the code you provided in a test project! Its working as expected! My project ASP.NET Core version is 2.2.0 Preview2 – TanvirArjel Oct 15 '18 at 10:51
  • 1
    Sorry, original answer is correct - I had a duplicate applicationUser class many levels below in project that causing the problem – CerIs Oct 15 '18 at 10:55

0 Answers0