0

I am trying to seed my database and add roles to my ASP.NET Core Web Application. In the tutorial I am following, it advised to add the following to the Configure() method:

    app.UseAuthentication();
MyIdentityDataInitializer.SeedData(userManager, roleManager);

I had no issues with the first line, but with the second, I am getting an error saying userManager and roleManager doesn't exist which makes sense because if i were using .NET 5 my configure method would've looked like this and been fine:

public void Configure(IApplicationBuilder app, 
IHostingEnvironment env, 
UserManager<MyIdentityUser> userManager, 
RoleManager<MyIdentityRole> roleManager)
{
    ...
    app.UseAuthentication();

    MyIdentityDataInitializer.SeedData(userManager, roleManager);

    app.UseStaticFiles();

    app.UseMvc(routes =>
    {
      routes.MapRoute(
      name: "default",
      template: "{controller=Home}/{action=Index}/{id?}");
    });
}

But in .NET 6 I'm not sure how to.

Can someone advise on this please?

  • Are you trying to do this with the minimal API code that doesn't have StartUp.cs? I'm not sure how you do it with that but you can still use the old Startup.cs way of doing things and have the same configure method I think ....at least I've got a Configure method in Startup my .net 6 project and I'm doing DI there – tappetyclick Oct 20 '22 at 15:51
  • This might be of interest: https://www.codemag.com/Article/2201081/Minimal-APIs-in-.NET-6 – Matthew Watson Oct 20 '22 at 15:54
  • @tappetyclick When I created the application it didn't come with a startup.cs, didn't realise I could add it back in - ill try that thank you. – Megan Paterson Oct 20 '22 at 15:54

1 Answers1

5

In .NET 6, Microsoft has removed the Startup.cs class where your Configure() method was previously defined. Instead, configuration of both the DI service container (which was done previously in the ConfigureServices(IServiceCollection)) method) and the AspNet Core Application Builder are done directly in Program.cs

Now since there is nothing automatically calling the Configure() method for you (and injecting those values for you), you'll have to get references to those objects yourself using the ServiceProvider. Your Program.cs will probably look something like this:

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddControllers();

// add services that were previously added in ConfigureServices()

var app = builder.Build();

var userManager = app.Services.GetRequiredService<UserManager<MyIdentityUser>>();
var roleManager = app.Services.GetRequiredService<RoleManager<MyIdentityRole>>();
MyIdentityDataInitializer.SeedData(userManager, roleManager);

app.UseAuthentication();

app.UseStaticFiles();

app.UseMvc(routes =>
{
  routes.MapRoute(
    name: "default",
    template: "{controller=Home}/{action=Index}/{id?}");
});

*Edit: To fix your error, (and as the link @tappetyclick directed you to explains), you'll need to first create a scope in order to get references to your UserManager and RoleManager objects:

using (var scope = app.Services.CreateScope())
{
    var userManager = scope.ServiceProvider.GetRequiredService<UserManager<MyIdentityUser>>();
    var roleManager = scope.ServiceProvider.GetRequiredService<RoleManager<MyIdentityRole>>();
    MyIdentityDataInitializer.SeedData(userManager, roleManager);
}
Harris Spivack
  • 275
  • 1
  • 6
  • Thanks for your response! I am getting an error when running it: 'Cannot resolve scoped service 'Microsoft.AspNetCore.Identity.UserManager'1[Microsoft.AspNetCore.Identity.IdentityUser]' from root provider. I'm not sure why, do you have any ideas? – Megan Paterson Oct 21 '22 at 07:58
  • This seems be doing roughly the same as you (seeding) https://stackoverflow.com/questions/72447401/cannot-resolve-scoped-service-from-root-provider-in-asp-net-core-6 or try this: https://stackoverflow.com/questions/52487989/cannot-resolve-scoped-service-microsoft-aspnetcore-identity-usermanager1ident – tappetyclick Oct 21 '22 at 12:56