我一直在尝试寻找最好的/首选的方法是让我的RoleService
获得ConfigurationDbContext
(IdentityServer4)。
我真的很想解耦,以便我的RoleService可以测试。
我发现访问ConfigurationDbContext
的唯一方法是通过在public static IServiceProvider
中创建Startup.cs
:
public class Startup
{
private readonly IHostingEnvironment _environment;
// THIS IS THE PROPERTY I USED
public static IServiceProvider ServiceProvider { get; private set; }
public ConfigurationDbContext GetConfigurationDbContext()
{
return null;
}
public Startup(ILoggerFactory loggerFactory, IHostingEnvironment environment)
{
loggerFactory.AddConsole(LogLevel.Debug);
_environment = environment;
}
public void ConfigureServices(IServiceCollection services)
{
var connectionString = DbSettings.IdentityServerConnectionString;
var migrationsAssembly = typeof(Startup).GetTypeInfo().Assembly.GetName().Name;
// configure identity server with in-memory stores, keys, clients and scopes
var identityServerConfig = services.AddIdentityServer()
.AddConfigurationStore(builder =>
builder.UseSqlServer(connectionString, options =>
options.MigrationsAssembly(migrationsAssembly)))
.AddOperationalStore(builder =>
builder.UseSqlServer(connectionString, options =>
options.MigrationsAssembly(migrationsAssembly)))
.AddSigningCredential(new X509Certificate2(Path.Combine(_environment.ContentRootPath, "certs", "IdentityServer4Auth.pfx"), "test"));
identityServerConfig.Services.AddTransient<IResourceOwnerPasswordValidator, ActiveDirectoryPasswordValidator>();
identityServerConfig.Services.AddTransient<IProfileService, CustomProfileService>();
services.AddDbContext<ConfigurationDbContext>(options => options.UseSqlServer(connectionString));
services.AddMvc();
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
ServiceProvider = app.ApplicationServices;
// other code emitted
}
}
然后在
RoleService.cs
中使用以下代码:public class RoleService : IRoleService
{
public async Task<ApiResource[]> GetApiResourcesByIds(int[] ids)
{
ApiResource[] result;
using (var serviceScope = Startup.ServiceProvider.GetService<IServiceScopeFactory>().CreateScope())
{
var context = serviceScope.ServiceProvider.GetRequiredService<ConfigurationDbContext>();
result =
context.ApiResources.Where(x => ids.Contains(x.Id))
.Include(x => x.Scopes)
.Include(x => x.UserClaims)
.ToArray();
return result;
}
}
}
这是在
RoleService.cs
中获得依赖项的最佳方法吗?是否有一种抽象
serviceScope
的方法(由于它在using语句中,并且可能是IDisposable,所以我真的不知道是否有一种抽象获取context
的方法吗?还有其他建议或最佳做法吗?
最佳答案
您可以通过使用所需的依赖项作为构造函数参数来创建一个构造函数,从而将依赖项传递给RoleService
。
根据服务RoleService
的生命周期(无法根据提供的代码判断),如果服务已经是ConfigurationDbContext
,则可以将RoleService
直接传递给Scoped
。或者,您可以通过IServiceScopeFactory
在服务中手动创建范围。
就像是:
private readonly IServiceScopeFactory _scopeFactory;
public RoleService(IServiceScopeFactory scopeFactory)
{
_scopeFactory = scopeFactory;
}
public async Task<ApiResource[]> GetApiResourcesByIds(int[] ids)
{
using (var scope = _scopeFactory.CreateScope())
{
var context = scope.ServiceProvider.GetRequiredService<ConfigurationDbContext>();
}
}
由于您的方法已与DbContext耦合(这不一定是坏事),您可能最终会创建内存中的
ConfigurationDbContext
。您使用的IdentityServer4.EntityFramework存储库包含一些示例,用于测试here。
关于c# - ASP.NET Core-依赖注入(inject)-IdentityServer4 DbContext,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44124768/