title: Mediatr unable to resolve UserManager in ASP.Net Core
body: <p>I am building ASP.Net Core App depending on this clean <a href="https://github.com/JasonGT/NorthwindTraders" rel="nofollow noreferrer">architecture</a>  example which is using MediatR to execute commands.
  And i want to use ASP.Net Core Identity in my app, so in my CreateUserCommandHandler i want to use UserManager to add new user, but when i add UserManager to Command contractor MediatR unable to create the handler and fail with this exception:</p>
  
  <pre><code>System.InvalidOperationException: Error constructing handler for request of type MediatR.IRequestHandler`2[GoGYM.Application.Identity.Commands.CreateUser.CreateUserCommand,MediatR.Unit]. Register your handlers with the container. See the samples in GitHub for examples. ---&gt; System.InvalidOperationException: Unable to resolve service for type 'GoGYM.Persistence.GoGYMDbContext' while attempting to activate 'Microsoft.AspNetCore.Identity.EntityFrameworkCore.UserStore`9[GoGYM.Domain.Entities.ApplicationUser,GoGYM.Domain.Entities.ApplicationRole,GoGYM.Persistence.GoGYMDbContext,System.String,Microsoft.AspNetCore.Identity.IdentityUserClaim`1[System.String],Microsoft.AspNetCore.Identity.IdentityUserRole`1[System.String],Microsoft.AspNetCore.Identity.IdentityUserLogin`1[System.String],Microsoft.AspNetCore.Identity.IdentityUserToken`1[System.String],Microsoft.AspNetCore.Identity.IdentityRoleClaim`1[System.String]]'.
  </code></pre>
  
  <p>In Configure services i register my DBContext and MediatR like this:</p>
  
  <pre><code>// Add AutoMapper
          services.AddAutoMapper(new Assembly[] { typeof(AutoMapperProfile).GetTypeInfo().Assembly });
          // Add MediatR
          services.AddMediatR(typeof(GetUsersListQueryHandler).GetTypeInfo().Assembly);
          // Add DbContext using SQL Server Provider
          services.AddDbContext&lt;IGoGYMDbContext, GoGYMDbContext&gt;(options =&gt;
              options.UseSqlServer(Configuration.GetConnectionString("NorthwindDatabase")));
  
          services.AddIdentity&lt;ApplicationUser, ApplicationRole&gt;()
              .AddDefaultTokenProviders()
               .AddEntityFrameworkStores&lt;GoGYMDbContext&gt;();
          services.AddMvc();
  ....
  </code></pre>
  
  <p>And this my command handler code:</p>
  
  <pre><code>public class CreateUserCommandHandler : IRequestHandler&lt;CreateUserCommand, Unit&gt;
  {
      private readonly UserManager&lt;ApplicationUser&gt; _userManager;
      private readonly IGoGYMDbContext _context;
  
      public CreateUserCommandHandler(IGoGYMDbContext context, UserManager&lt;ApplicationUser&gt; userManager)
      {
          _context = context;
          _userManager = userManager;
      }
      public Task&lt;Unit&gt; Handle(CreateUserCommand request, CancellationToken cancellationToken)
      {
          throw new NotImplementedException();
      }
  }
  </code></pre>
  
  <p>And my controller</p>
  
  <pre><code>[HttpPost]
      [ProducesResponseType(StatusCodes.Status204NoContent)]
      [ProducesDefaultResponseType]
      public async Task&lt;IActionResult&gt; Create(string values)
      {
          await Mediator.Send(new CreateUserCommand(values));
  
          return NoContent();
      }
  </code></pre>
  
  <p>I have tried a lot of things and nothing work, only if i remove UserManager from command handler it gets executed then.</p>