หลังจากแก้ไขปัญหาเริ่มต้นของฉันด้วยการเพิ่ม UserManager ให้กับ API (การสร้างผู้ใช้ด้วย IdentityServer4 จากหลาย API) ที่ไม่ได้เริ่มต้น IdentityServer4 (ซึ่งจะเริ่มต้นภายในเว็บแอปที่รับผิดชอบเฉพาะการลงทะเบียนและการเข้าสู่ระบบของผู้ใช้เท่านั้น) ฉันพบปัญหาอื่น จาก API เดียวกัน ฉันต้องการรับลูกค้าและทรัพยากรจาก IConfigurationDbContext ของ IdentityServer4 ด้วย
สิ่งที่ฉันกำลังลองอยู่ตอนนี้มีดังต่อไปนี้: ฉันกำลังเพิ่ม ConfigurationDbContext ในการเริ่มต้นของ API จากนั้นผ่าน ClientsController และ ClientsRepository ฉันกำลังพยายามเข้าถึงไคลเอนต์ดังที่แสดงด้านล่าง
Startup.cs
services.AddDbContext<ApplicationDbContext>(options =>
options.UseNpgsql(defaultConnection, db => db.MigrationsAssembly("XXXXXXX"))
);
services.AddDbContext<ConfigurationDbContext>(options =>
options.UseNpgsql(defaultConnection, db => db.MigrationsAssembly("XXXXXXXX"))
);
services.AddIdentityCore<ApplicationUser>(options => {
options.Password.RequireNonAlphanumeric = false;
});
new IdentityBuilder(typeof(ApplicationUser), typeof(IdentityRole), services)
.AddRoleManager<RoleManager<IdentityRole>>()
.AddSignInManager<SignInManager<ApplicationUser>>()
.AddEntityFrameworkStores<ApplicationDbContext>();
ClientsRepository.cs (ใน .DataAccess):
private readonly IConfigurationDbContext _context;
public bool AutoSaveChanges { get; set; } = true;
public ClientRepository(IConfigurationDbContext context)
{
_context = context;
}
public Task<Client> GetClientAsync(int id)
{
return _context.Clients
.Include(x => x.AllowedGrantTypes)
.Include(x => x.RedirectUris)
.Include(x => x.PostLogoutRedirectUris)
.Include(x => x.AllowedScopes)
.Include(x => x.ClientSecrets)
.Include(x => x.Claims)
.Include(x => x.IdentityProviderRestrictions)
.Include(x => x.AllowedCorsOrigins)
.Include(x => x.Properties)
.Where(x => x.Id == id)
.SingleOrDefaultAsync();
}
อย่างไรก็ตาม ฉันได้รับข้อผิดพลาดดังต่อไปนี้:
System.InvalidOperationException: Unable to resolve service for type 'IdentityServer4.EntityFramework.Interfaces.IConfigurationDbContext' while attempting to activate 'XXXXXX.Data.Repositories.ClientRepository'.
ฉันเดาว่ามันจะต้องทำอะไรบางอย่างกับบริการเริ่มต้นอีกครั้ง แต่ดูเหมือนจะหามันไม่เจอ
มีใครแก้ไขสิ่งที่คล้ายกันบ้างไหม?
ดีที่สุด มาริออส