Update IdentityServerDbContextProvider.
This commit is contained in:
Родитель
1affc7d46a
Коммит
a6547a561d
|
@ -1,82 +1,31 @@
|
|||
using System;
|
||||
using System.Linq;
|
||||
using Abp;
|
||||
using Abp.Dependency;
|
||||
using Abp.Dependency;
|
||||
using Abp.Domain.Uow;
|
||||
using Abp.EntityFrameworkCore.Configuration;
|
||||
using JetBrains.Annotations;
|
||||
using Abp.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace IdentityServerWithEfCoreDemo.EntityFrameworkCore.IdentityServer
|
||||
{
|
||||
public class IdentityServerDbContextProvider : ITransientDependency
|
||||
{
|
||||
private readonly IIocResolver _iocResolver;
|
||||
private readonly IDbContextResolver _dbContextResolver;
|
||||
private readonly IConnectionStringResolver _connectionStringResolver;
|
||||
|
||||
public IdentityServerDbContextProvider(IIocResolver iocResolver, IConnectionStringResolver connectionStringResolver)
|
||||
public IdentityServerDbContextProvider(IDbContextResolver dbContextResolver,
|
||||
IConnectionStringResolver connectionStringResolver)
|
||||
{
|
||||
_iocResolver = iocResolver;
|
||||
_dbContextResolver = dbContextResolver;
|
||||
_connectionStringResolver = connectionStringResolver;
|
||||
}
|
||||
|
||||
public TDbContext GetDbContext<TDbContext>()
|
||||
where TDbContext : DbContext
|
||||
{
|
||||
var connectionStringResolveArgs = new ConnectionStringResolveArgs(); //MultiTenancySides
|
||||
var connectionStringResolveArgs = new ConnectionStringResolveArgs() //MultiTenancySides
|
||||
{
|
||||
["DbContextType"] = typeof(TDbContext)
|
||||
};
|
||||
var connectionString = _connectionStringResolver.GetNameOrConnectionString(connectionStringResolveArgs);
|
||||
var dbContextType = typeof(TDbContext);
|
||||
|
||||
try
|
||||
{
|
||||
return _iocResolver.Resolve<TDbContext>(new
|
||||
{
|
||||
options = CreateOptions<TDbContext>(connectionString),
|
||||
configurationStoreOptions = IdentityServerStoreOptionsProvider.Instance.ConfigurationStoreOptions,
|
||||
operationalStoreOptions = IdentityServerStoreOptionsProvider.Instance.OperationalStoreOptions
|
||||
});
|
||||
}
|
||||
catch (Castle.MicroKernel.Resolvers.DependencyResolverException ex)
|
||||
{
|
||||
var hasOptions = HasOptions(dbContextType);
|
||||
if (!hasOptions)
|
||||
{
|
||||
throw new AggregateException($"The parameter name of {dbContextType.Name}'s constructor must be 'options'", ex);
|
||||
}
|
||||
|
||||
throw;
|
||||
}
|
||||
|
||||
static bool HasOptions(Type contextType)
|
||||
{
|
||||
return contextType.GetConstructors().Any(ctor =>
|
||||
{
|
||||
var parameters = ctor.GetParameters();
|
||||
return parameters.Length == 1 && parameters.FirstOrDefault()?.Name == "options";
|
||||
});
|
||||
}
|
||||
}
|
||||
protected virtual DbContextOptions<TDbContext> CreateOptions<TDbContext>([NotNull] string connectionString)
|
||||
where TDbContext : DbContext
|
||||
{
|
||||
if (_iocResolver.IsRegistered<IAbpDbContextConfigurer<TDbContext>>())
|
||||
{
|
||||
var configuration = new AbpDbContextConfiguration<TDbContext>(connectionString, null);
|
||||
|
||||
using (var configurer = _iocResolver.ResolveAsDisposable<IAbpDbContextConfigurer<TDbContext>>())
|
||||
{
|
||||
configurer.Object.Configure(configuration);
|
||||
}
|
||||
|
||||
return configuration.DbContextOptions.Options;
|
||||
}
|
||||
|
||||
if (_iocResolver.IsRegistered<DbContextOptions<TDbContext>>())
|
||||
{
|
||||
return _iocResolver.Resolve<DbContextOptions<TDbContext>>();
|
||||
}
|
||||
|
||||
throw new AbpException($"Could not resolve DbContextOptions for {typeof(TDbContext).AssemblyQualifiedName}.");
|
||||
return _dbContextResolver.Resolve<TDbContext>(connectionString, null);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -6,6 +6,7 @@ using IdentityServer4.EntityFramework.Interfaces;
|
|||
using IdentityServer4.EntityFramework.Options;
|
||||
using IdentityServerWithEfCoreDemo.Authorization.Roles;
|
||||
using IdentityServerWithEfCoreDemo.Authorization.Users;
|
||||
using IdentityServerWithEfCoreDemo.EntityFrameworkCore.IdentityServer;
|
||||
using IdentityServerWithEfCoreDemo.MultiTenancy;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
|
@ -17,42 +18,33 @@ namespace IdentityServerWithEfCoreDemo.EntityFrameworkCore
|
|||
IPersistedGrantDbContext
|
||||
{
|
||||
/* Define a DbSet for each entity of the application */
|
||||
|
||||
public DbSet<Client> Clients { get; set; }
|
||||
public DbSet<IdentityResource> IdentityResources { get; set; }
|
||||
public DbSet<ApiResource> ApiResources { get; set; }
|
||||
public DbSet<PersistedGrant> PersistedGrants { get; set; }
|
||||
public DbSet<DeviceFlowCodes> DeviceFlowCodes { get; set; }
|
||||
|
||||
async Task<int> IPersistedGrantDbContext.SaveChangesAsync()
|
||||
{
|
||||
return await base.SaveChangesAsync();
|
||||
}
|
||||
|
||||
public DbSet<PersistedGrant> PersistedGrants { get; set; }
|
||||
public DbSet<DeviceFlowCodes> DeviceFlowCodes { get; set; }
|
||||
|
||||
async Task<int> IConfigurationDbContext.SaveChangesAsync()
|
||||
{
|
||||
return await base.SaveChangesAsync();
|
||||
}
|
||||
|
||||
public DbSet<Client> Clients { get; set; }
|
||||
public DbSet<IdentityResource> IdentityResources { get; set; }
|
||||
public DbSet<ApiResource> ApiResources { get; set; }
|
||||
|
||||
protected ConfigurationStoreOptions ConfigurationStoreOptions { get; }
|
||||
protected OperationalStoreOptions OperationalStoreOptions { get; }
|
||||
|
||||
public IdentityServerWithEfCoreDemoDbContext(
|
||||
DbContextOptions<IdentityServerWithEfCoreDemoDbContext> options,
|
||||
ConfigurationStoreOptions configurationStoreOptions,
|
||||
OperationalStoreOptions operationalStoreOptions)
|
||||
public IdentityServerWithEfCoreDemoDbContext(DbContextOptions<IdentityServerWithEfCoreDemoDbContext> options)
|
||||
: base(options)
|
||||
{
|
||||
ConfigurationStoreOptions = configurationStoreOptions;
|
||||
OperationalStoreOptions = operationalStoreOptions;
|
||||
}
|
||||
|
||||
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||
{
|
||||
modelBuilder.ConfigureClientContext(ConfigurationStoreOptions);
|
||||
modelBuilder.ConfigureResourcesContext(ConfigurationStoreOptions);
|
||||
|
||||
modelBuilder.ConfigurePersistedGrantContext(OperationalStoreOptions);
|
||||
modelBuilder.ConfigureClientContext(IdentityServerStoreOptionsProvider.Instance.ConfigurationStoreOptions);
|
||||
modelBuilder.ConfigureResourcesContext(IdentityServerStoreOptionsProvider.Instance.ConfigurationStoreOptions);
|
||||
modelBuilder.ConfigurePersistedGrantContext(IdentityServerStoreOptionsProvider.Instance.OperationalStoreOptions);
|
||||
|
||||
base.OnModelCreating(modelBuilder);
|
||||
}
|
||||
|
|
|
@ -2,7 +2,6 @@
|
|||
using Microsoft.EntityFrameworkCore.Design;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using IdentityServerWithEfCoreDemo.Configuration;
|
||||
using IdentityServerWithEfCoreDemo.EntityFrameworkCore.IdentityServer;
|
||||
using IdentityServerWithEfCoreDemo.Web;
|
||||
|
||||
namespace IdentityServerWithEfCoreDemo.EntityFrameworkCore
|
||||
|
@ -17,10 +16,7 @@ namespace IdentityServerWithEfCoreDemo.EntityFrameworkCore
|
|||
|
||||
IdentityServerWithEfCoreDemoDbContextConfigurer.Configure(builder, configuration.GetConnectionString(IdentityServerWithEfCoreDemoConsts.ConnectionStringName));
|
||||
|
||||
return new IdentityServerWithEfCoreDemoDbContext(
|
||||
builder.Options,
|
||||
IdentityServerStoreOptionsProvider.Instance.ConfigurationStoreOptions,
|
||||
IdentityServerStoreOptionsProvider.Instance.OperationalStoreOptions);
|
||||
return new IdentityServerWithEfCoreDemoDbContext(builder.Options);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Загрузка…
Ссылка в новой задаче