2017-08-21 25 views
12

Mam ten kod w moim Startup.cs:ASP.NET Core 2 - Tożsamość - DI błędy z niestandardowych ról

services.AddDbContext<ApplicationDbContext>(options => 
      options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"))); 

     services.AddIdentity<ApplicationUser, ApplicationRole>() 
      .AddEntityFrameworkStores<ApplicationDbContext>() 
      .AddDefaultTokenProviders(); 

W tym samym pliku, ja też zastąpiła service.UseIdentity() z app.UseAuthentication(); zalecane przez MS w nowa wersja Jądra ASP 2.

moim DB Kontekst:

public class ApplicationDbContext : IdentityDbContext<ApplicationUser, ApplicationRole, string> 
{ 
    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> 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<ApplicationUser> ApplicationUser { get; set; } 

    //public DbSet<ApplicationRole> ApplicationRole { get; set; } 
} 

A moja klasa zwyczaj Rola:

public class ApplicationRole : IdentityRole 
{ 
    public ApplicationRole() : base() { } 

    public ApplicationRole(string roleName) : base(roleName) { } 

    public bool IsDefault { get; set; } 
} 

Po uruchomieniu aplikacji, mam SeedDatabase metody pomocnika, który działa:

var roleManager = serviceProvider.GetService<RoleManager<ApplicationRole>>(); 

To wszystko działa dobrze, ale skoro aktualizacji VS 2017 do najnowszej wersji i zainstalowanie .NET 2.0 Rdzeń ten ostatni linia kodu teraz rzuca następujący wyjątek:

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=<Cannot evaluate the exception source> 
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. 

nie jestem pewien, dlaczego kierownik usługa DI nie jest już w stanie znaleźć moją klasę ApplicationRole. Sprawdziłem i wszystkie moje referencje używają tej klasy, a nie domyślnej IdentityRole.

Wszelkie pomysły?

Odpowiedz

15

Musisz samodzielnie utworzyć IServiceScope.

Aby to zrobić, trzeba zastąpić

var roleManager = serviceProvider.GetService<RoleManager<ApplicationRole>>(); 

z

IServiceScopeFactory scopeFactory = app.ApplicationServices.GetRequiredService<IServiceScopeFactory>(); 

using (IServiceScope scope = scopeFactory.CreateScope()) { 
    RoleManager<IdentityRole> roleManager = scope.ServiceProvider.GetRequiredService<RoleManager<IdentityRole>>(); 

    // Seed database code goes here 
} 
+2

Dzięki! Wydaje mi się, że wiele rzeczy zmieniło się w wersji 2.0, a dokumentacja jest nadal w pieluchach :) – Bmelca

+1

Nie pieluszki, bardziej jak pojemnik na śmieci. Jestem bardzo rozczarowany zespołem .NET Core z najnowszą wersją. To żałosna śliwka. – Keith

+0

@ Manos, wielkie dzięki za tę odpowiedź. Szaloną rzeczą jest to, że trochę zwymiotowałem, myśląc o tym, że zespół .NET Core uważał, że rozsądniej byłoby dodać więcej linii kodu, aby wykonać funkcję wymagającą pojedynczej linii kodu. Ponownie, tak rozczarowany (zgodnie z moim komentarzem powyżej) – Keith

Powiązane problemy