InvalidOperationException: A circular dependency was detected for the service of type 'Microsoft.AspNetCore.Identity.UserManager

user2058413 picture user2058413 · Jul 30, 2017 · Viewed 8.5k times · Source

This happens after I customized the asp.net core identity services to support multi-tenancy based on This article. I simplified it to suite my needs.

Here is my basic setup.

1) Custom application user

public class ApplicationUser : IdentityUser<int>, IEntityBase{}

2) Custom Role

public class ApplicationRole : IdentityRole<int>, IEntityBase{}

3) Custom Role store

public class RoleStoreMultiTenant<TRole> : RoleStore<TRole, ApplicationDbContext, int>{}

4) Custom User Store

public class UserStoreMultiTenant<TUser, TRole, TKey> : UserStore<TUser, TRole, ApplicationDbContext, int>{}

5) My role service inheriting from above (3). This is just to separate my code from RoleStore overridden code.

public class ApplicationRoleStore : RoleStoreMultiTenant<ApplicationRole>{}

6) My User service inheriting from above (4). This is just to separate my code from UserStore overridden code.

public class ApplicationUserStore : UserStoreMultiTenant<ApplicationUser, ApplicationRole, int>{}

7) My ApplicationDbContext is;

public class ApplicationDbContext : IdentityDbContext<ApplicationUser, ApplicationRole, int>{}

8) My startup configurations related to identity (in ConfigureServices).

services.AddScoped<IRoleStore<ApplicationRole>, ApplicationRoleStore>();
services.AddScoped<IUserStore<ApplicationUser>, ApplicationUserStore>();
services.AddIdentity<ApplicationUser, ApplicationRole>(o =>
{
o.User.RequireUniqueEmail = true; 
//options code
}).AddUserStore<ApplicationUserStore>()
.AddEntityFrameworkStores<ApplicationDbContext, int>();

9) In Startup Configure method I have;

//other code
app.UseIdentity();
//other code

10) I have a basecontroller expecting below via the constructor injection

public BaseController(ApplicationDbContext dbContext, 
    UserManager<ApplicationUser> userManager,
    SignInManager<ApplicationUser> signInManager,
    IMessageServices messageServices, ILoggerFactory loggerFactory,
    AppTenant currentTenant, IMapper mapper)
{
    _dbContext = dbContext;
    _signInManager = signInManager;
    _userManager = userManager;
    _messageServices = messageServices;
    _logger = loggerFactory.CreateLogger<BaseController>();
    _currentTenant = currentTenant;
    _mapper = mapper;
}

All other controllers are inheriting from this base.

My Database migrations works fine and identity db structures getting created with my custom properties without any issues. However when I run the application I get the error shown in the topic. Which is;

InvalidOperationException: A circular dependency was detected for the service of type 'Microsoft.AspNetCore.Identity.UserManager`1[Registrar.Data.MultitenantIdentity.Models.ApplicationUser]'.

Stack trace shows all framework code and I am finding it difficult to figure out the circular reference .

Can anyone point me to the right direction?

Answer

user2058413 picture user2058413 · Jul 30, 2017

Found the problem. In the class;

public class ApplicationUserStore : UserStoreMultiTenant<ApplicationUser, ApplicationRole, int>{}

I was requesting UserManager in the constructor which was causing the issue. It would have been nice if the framework shows at which line in my code made it to fail.