title: Dependency Injection circular dependency .NET Core 2.0
body: <p>I want my <code>ApplicationContext</code> constructor to have the <code>UserManager</code> as a parameter, but I am having trouble with dependency injection.</p>
  
  <p>Code:</p>
  
  <pre><code>public class ApplicationContext : IdentityDbContext&lt;ApplicationUser&gt;
  {
      private IHttpContextAccessor _contextAccessor { get; set; }
      public ApplicationUser ApplicationUser { get; set; }
      private UserManager&lt;ApplicationUser&gt; _userManager;
  
      public ApplicationContext(DbContextOptions&lt;ApplicationContext&gt; options, IHttpContextAccessor contextAccessor, UserManager&lt;ApplicationUser&gt; userManager)
          : base(options)
      {
          _contextAccessor = contextAccessor;
          var user = _contextAccessor.HttpContext.User;
          _userManager = userManager;
          ApplicationUser = _userManager.Users.FirstOrDefault(u =&gt; u.Id == _userManager.GetUserId(user));
      }
  }
  </code></pre>
  
  <p>And in <code>startup.cs</code>:</p>
  
  <pre><code>public void ConfigureServices(IServiceCollection services)
  {
      // Add framework services.
      services.AddDbContext&lt;ApplicationContext&gt;(options =&gt;
          options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"), b =&gt; b.MigrationsAssembly("RCI.App")));
  
      services.AddIdentity&lt;ApplicationUser, IdentityRole&gt;()
          .AddEntityFrameworkStores&lt;ApplicationContext&gt;()
          .AddDefaultTokenProviders();
  
      services.AddAuthentication();
  
      services.AddMvc();
  
      // Add application services.
      services.AddTransient&lt;IEmailSender, AuthMessageSender&gt;();
      services.AddTransient&lt;ISmsSender, AuthMessageSender&gt;();
      services.AddTransient&lt;IHttpContextAccessor, HttpContextAccessor&gt;();
  
      services.AddOptions();
  
  }
  </code></pre>
  
  <p>Error message:</p>
  
  <blockquote>
    <p>A circular dependency was detected for the service of type
    'Microsoft.AspNetCore.Identity.UserManager`1[RCI.App.Models.ApplicationUser]'.</p>
  </blockquote>
  
  <p>Could anyone point out what I'm doing wrong?</p>