2017-08-21 21 views
12

Tôi có mã này trong Startup.cs tôi:ASP.NET Core 2 - Identity - lỗi DI với vai trò tùy chỉnh

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

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

Trong đó cùng một tập tin, tôi cũng thay thế service.UseIdentity() với app.UseAuthentication(); theo khuyến cáo của MS trong phiên bản mới của ASP Core 2.

My Db Bối cảnh:

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; } 
} 

Và lớp tùy chỉnh Vai trò của tôi:

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

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

    public bool IsDefault { get; set; } 
} 

Khi chạy ứng dụng, tôi nhận được một phương pháp helper SeedDatabase chạy:

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

Đây là tất cả làm việc tốt, nhưng kể từ khi cập nhật VS 2017 lên phiên bản mới nhất và cài đặt .NET Lõi 2.0, điều này cuối cùng dòng mã bây giờ ném ngoại lệ sau:

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. 

Tôi không chắc chắn tại sao trình quản lý dịch vụ DI không thể tìm thấy lớp ApplicationRole của tôi nữa. Tôi đã kiểm tra và tất cả các tài liệu tham khảo của tôi đang sử dụng lớp này và không phải là IdentityRole mặc định.

Bất kỳ ý tưởng nào?

Trả lời

15

Bạn phải tự mình tạo IServiceScope.

Để thực hiện điều này, bạn phải thay thế

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

với

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

Cảm ơn! Tôi đoán rất nhiều thứ đã thay đổi trong 2.0 và tài liệu hiện có vẫn còn trong tã :) – Bmelca

+1

Không tã, giống như rác có thể. Tôi SO thất vọng trong nhóm .NET Core với bản phát hành mới nhất. Nó rất đáng thương. – Keith

+0

@Manos, cảm ơn rất nhiều vì câu trả lời này. Điều điên rồ là, tôi đã ném lên một chút trong miệng của tôi trong khi suy nghĩ về thực tế là đội ngũ NET Core. Nghĩ rằng nó sẽ là khôn ngoan để thêm nhiều dòng mã để thực hiện một chức năng yêu cầu một dòng mã. Một lần nữa, do đó, thất vọng (theo bình luận của tôi ở trên) – Keith

Các vấn đề liên quan