title: InvalidOperationException: A circular dependency was detected for the service of type 'Microsoft.AspNetCore.Identity.UserManager
body: <p>This happens after I customized the asp.net core identity services to support multi-tenancy based on <a href="https://github.com/Avareto/Asp.Net-Core-Identity-Multitenancy/tree/master/AspNetCoreMultitenancy/Models" rel="nofollow noreferrer">This article</a>. I simplified it to suite my needs.</p>
  
  <p>Here is my basic setup.</p>
  
  <p>1) Custom application user</p>
  
  <pre><code>public class ApplicationUser : IdentityUser&lt;int&gt;, IEntityBase{}
  </code></pre>
  
  <p>2) Custom Role</p>
  
  <pre><code>public class ApplicationRole : IdentityRole&lt;int&gt;, IEntityBase{}
  </code></pre>
  
  <p>3) Custom Role store</p>
  
  <pre><code>public class RoleStoreMultiTenant&lt;TRole&gt; : RoleStore&lt;TRole, ApplicationDbContext, int&gt;{}
  </code></pre>
  
  <p>4) Custom User Store</p>
  
  <pre><code>public class UserStoreMultiTenant&lt;TUser, TRole, TKey&gt; : UserStore&lt;TUser, TRole, ApplicationDbContext, int&gt;{}
  </code></pre>
  
  <p>5) My role service inheriting from above (3). This is just to separate my code from RoleStore overridden code.</p>
  
  <pre><code>public class ApplicationRoleStore : RoleStoreMultiTenant&lt;ApplicationRole&gt;{}
  </code></pre>
  
  <p>6) My User service inheriting from above (4). This is just to separate my code from UserStore overridden code.</p>
  
  <pre><code>public class ApplicationUserStore : UserStoreMultiTenant&lt;ApplicationUser, ApplicationRole, int&gt;{}
  </code></pre>
  
  <p>7) My ApplicationDbContext is;</p>
  
  <pre><code>public class ApplicationDbContext : IdentityDbContext&lt;ApplicationUser, ApplicationRole, int&gt;{}
  </code></pre>
  
  <p>8) My startup configurations related to identity (in ConfigureServices).</p>
  
  <pre><code>services.AddScoped&lt;IRoleStore&lt;ApplicationRole&gt;, ApplicationRoleStore&gt;();
  services.AddScoped&lt;IUserStore&lt;ApplicationUser&gt;, ApplicationUserStore&gt;();
  services.AddIdentity&lt;ApplicationUser, ApplicationRole&gt;(o =&gt;
  {
  o.User.RequireUniqueEmail = true; 
  //options code
  }).AddUserStore&lt;ApplicationUserStore&gt;()
  .AddEntityFrameworkStores&lt;ApplicationDbContext, int&gt;();
  </code></pre>
  
  <p>9) In Startup Configure method I have;</p>
  
  <pre><code>//other code
  app.UseIdentity();
  //other code
  </code></pre>
  
  <p>10) I have a basecontroller expecting below via the constructor injection</p>
  
  <pre><code>public BaseController(ApplicationDbContext dbContext, 
      UserManager&lt;ApplicationUser&gt; userManager,
      SignInManager&lt;ApplicationUser&gt; signInManager,
      IMessageServices messageServices, ILoggerFactory loggerFactory,
      AppTenant currentTenant, IMapper mapper)
  {
      _dbContext = dbContext;
      _signInManager = signInManager;
      _userManager = userManager;
      _messageServices = messageServices;
      _logger = loggerFactory.CreateLogger&lt;BaseController&gt;();
      _currentTenant = currentTenant;
      _mapper = mapper;
  }
  </code></pre>
  
  <p>All other controllers are inheriting from this base.</p>
  
  <p>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;</p>
  
  <pre><code>InvalidOperationException: A circular dependency was detected for the service of type 'Microsoft.AspNetCore.Identity.UserManager`1[Registrar.Data.MultitenantIdentity.Models.ApplicationUser]'.
  </code></pre>
  
  <p>Stack trace shows all framework code and I am finding it difficult to figure out the <strong>circular reference</strong> .</p>
  
  <p>Can anyone point me to the right direction?</p>