InvalidOperationException: A circular dependency was detected for the service of type 'Microsoft.AspNetCore.Identity.UserManager
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.
user2058413
Updated on June 04, 2022Comments
-
user2058413 almost 2 years
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?
-
Nkosi over 6 yearsit means that
UserManager<ApplicationUser>
has a dependency on a type that either directly depends on or has a dependency that depends onUserManager<ApplicationUser>
.ie:A => B, B => C, C => A
-
-
Steven over 6 years"It would have been nice if the framework shows at which line in my code made it to fail." If you are resolving a service directly from code, it is almost impossible for a DI Container to help you with this; the Service Locator anti-pattern blinds the DI Container from doing its job, which is one of the many reasons why Service Locator is called an anti-pattern.