title: ASP.NET Core 2 - Identity - DI errors with custom Roles
body: <p>I got this code in my Startup.cs:</p>
  
  <pre><code> services.AddDbContext&lt;ApplicationDbContext&gt;(options =&gt;
              options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
  
          services.AddIdentity&lt;ApplicationUser, ApplicationRole&gt;()
              .AddEntityFrameworkStores&lt;ApplicationDbContext&gt;()
              .AddDefaultTokenProviders();
  </code></pre>
  
  <p>In that same file, I also replaced the <code>service.UseIdentity()</code> with <code>app.UseAuthentication();</code> as recommended by MS in the new version of ASP Core 2.</p>
  
  <p>My Db Context:</p>
  
  <pre><code> public class ApplicationDbContext : IdentityDbContext&lt;ApplicationUser, ApplicationRole, string&gt;
  {
      public ApplicationDbContext(DbContextOptions&lt;ApplicationDbContext&gt; options)
          : base(options)
      {
      }
  
      protected override void OnModelCreating(ModelBuilder builder)
      {
          base.OnModelCreating(builder);
          // Customize the ASP.NET Identity model and override the defaults if needed.
          // For example, you can rename the ASP.NET Identity table names and more.
          // Add your customizations after calling base.OnModelCreating(builder);
      }
  
  
      //public DbSet&lt;ApplicationUser&gt; ApplicationUser { get; set; }
  
      //public DbSet&lt;ApplicationRole&gt; ApplicationRole { get; set; }
  }
  </code></pre>
  
  <p>And my custom Role class:</p>
  
  <pre><code> public class ApplicationRole : IdentityRole
  {
      public ApplicationRole() : base() { }
  
      public ApplicationRole(string roleName) : base(roleName) { }
  
      public bool IsDefault { get; set; }
  }
  </code></pre>
  
  <p>When running the application, I got a SeedDatabase helper method that runs:</p>
  
  <pre><code>var roleManager = serviceProvider.GetService&lt;RoleManager&lt;ApplicationRole&gt;&gt;();
  </code></pre>
  
  <p>This was all working fine, but since updating VS 2017 to the lastest version and installing .NET Core 2.0, this last line of code now throws the following exception:</p>
  
  <pre><code>System.AggregateException occurred
  HResult=0x80131500
  Message=One or more errors occurred. (Cannot resolve scoped service 'Microsoft.AspNetCore.Identity.RoleManager`1[CspLicensingPortal.Models.ApplicationRole]' from root provider.)
  Source=&lt;Cannot evaluate the exception source&gt;
  StackTrace:
  at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
  at System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout, CancellationToken cancellationToken)
  at System.Threading.Tasks.Task.Wait()
  at CspLicensingPortal.Startup.Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) in D:\gsandorx\Documents\Visual Studio 2017\Projects\CspLicensingPortal\CspLicensingPortal\Startup.cs:line 275
  
  Inner Exception 1:
  InvalidOperationException: Cannot resolve scoped service 'Microsoft.AspNetCore.Identity.RoleManager`1[MyApplication.Models.ApplicationRole]' from root provider.
  </code></pre>
  
  <p>I'm not sure why the DI service manager is no longer able to find my ApplicationRole class. I have checked and all my references are using this class and not the default IdentityRole. </p>
  
  <p>Any ideas? </p>