title: ASP.NET CORE 2.1 Identity has been registered
body: <p>I have problem with Identity ASP.net Core 2.1. I create Role in startup.cs. When running display error</p>
  
  <blockquote>
    <p>AggregateException: One or more errors occurred. (No service for type 'Microsoft.AspNetCore.Identity.RoleManager`1[Microsoft.AspNetCore.Identity.IdentityRole]' has been registered.)
    System.Threading.Tasks.Task.Wait(int millisecondsTimeout, CancellationToken cancellationToken)
    System.Threading.Tasks.Task.Wait()
    ContosoUniversity.Startup.Configure(IApplicationBuilder app, IHostingEnvironment env, IServiceProvider serviceProvider) in Startup.cs
    +
                CreateRoles(serviceProvider).Wait();
    Microsoft.AspNetCore.Hosting.ConventionBasedStartup.Configure(IApplicationBuilder app)
    Microsoft.AspNetCore.Server.IISIntegration.IISSetupFilter+&lt;>c__DisplayClass4_0.b__0(IApplicationBuilder app)
    Microsoft.AspNetCore.HostFilteringStartupFilter+&lt;>c__DisplayClass0_0.b__0(IApplicationBuilder app)
    Microsoft.AspNetCore.Hosting.Internal.AutoRequestServicesStartupFilter+&lt;>c__DisplayClass0_0.b__0(IApplicationBuilder builder)
    Microsoft.AspNetCore.Hosting.Internal.WebHost.BuildApplication()</p>
  </blockquote>
  
  <p>Statup.cs file</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Threading.Tasks;
  using Microsoft.AspNetCore.Builder;
  using Microsoft.AspNetCore.Hosting;
  using Microsoft.AspNetCore.Http;
  using Microsoft.AspNetCore.HttpsPolicy;
  using Microsoft.AspNetCore.Mvc;
  using Microsoft.Extensions.Configuration;
  using Microsoft.Extensions.DependencyInjection;
  using Microsoft.EntityFrameworkCore;
  using ContosoUniversity.Models;
  using Microsoft.AspNetCore.Identity;
  using ContosoUniversity.Areas.Identity.Data;
  
  namespace ContosoUniversity
  {
      public class Startup
      {
          public Startup(IConfiguration configuration)
          {
              Configuration = configuration;
  
          }
          public IConfiguration Configuration { get; }
  
          // This method gets called by the runtime. Use this method to add services to the container.
          public void ConfigureServices(IServiceCollection services)
          {
              services.Configure&lt;CookiePolicyOptions&gt;(options =&gt;
              {
                  // This lambda determines whether user consent for non-essential cookies is needed for a given request.
                  options.CheckConsentNeeded = context =&gt; true;
                  options.MinimumSameSitePolicy = SameSiteMode.None;
              });
  
              services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
  
              services.AddDbContext&lt;SchoolContext&gt;(options =&gt;
                      options.UseSqlServer(Configuration.GetConnectionString("SchoolContext")));
  
              //services.AddIdentity&lt;ContosoUniversityUser, IdentityRole&gt;()
              //    .AddEntityFrameworkStores&lt;IdentityContext&gt;()
              //     .AddDefaultTokenProviders();
  
  
          }
          // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
          public void Configure(IApplicationBuilder app, IHostingEnvironment env, IServiceProvider serviceProvider)
          {
              if (env.IsDevelopment())
              {
                  app.UseDeveloperExceptionPage();
              }
              else
              {
                  app.UseExceptionHandler("/Error");
                  app.UseHsts();
              }
              app.UseHttpsRedirection();
              app.UseStaticFiles();
              app.UseAuthentication();
              app.UseMvc();
              CreateRoles(serviceProvider).Wait();
          }
          public async Task CreateRoles(IServiceProvider serviceProvider)
          {
  
              //adding custom roles
              var RoleManager = serviceProvider.GetRequiredService&lt;RoleManager&lt;IdentityRole&gt;&gt;();
              //serviceProvider.GetService&lt;RoleManager&lt;IdentityRole&gt;&gt;();
              var UserManager = serviceProvider.GetRequiredService&lt;UserManager&lt;ContosoUniversityUser&gt;&gt;();
              string[] roleNames = { "Admin", "Manager", "Member" };
              IdentityResult roleResult;
              foreach (var roleName in roleNames)
              {
                  var roleExist = await RoleManager.RoleExistsAsync(roleName);
                  if (!roleExist)
                  {
                      roleResult = await RoleManager.CreateAsync(new IdentityRole(roleName));
                  }
              }
              var poweruser = new ContosoUniversityUser
              {
                  UserName = Configuration.GetSection("UserSettings")["UserEmail"],
                  Email = Configuration.GetSection("UserSettings")["UserEmail"]
              };
  
              string UserPassword = Configuration.GetSection("UserSettings")["UserPassword"];
              var _user = await UserManager.FindByEmailAsync(Configuration.GetSection("UserSettings")["UserEmail"]);
  
              if (_user == null)
              {
                  var createPowerUser = await UserManager.CreateAsync(poweruser, UserPassword);
                  if (createPowerUser.Succeeded)
                  {
                      await UserManager.AddToRoleAsync(poweruser, "Admin");
                  }
              }
          }
      }
  }
  </code></pre>