I am using asp.net core
along with Entity Framework Core
. My scenario here is, I want to change the connection string at runtime based on HttpContext
query string value.
I am trying to pass ResolvedParameter
with Reflection components
as documented. But, It is not getting registered when I resolve this. Here below, I have attached my code snippet.
Autofac Registration class:
public class DependencyRegistrar : IDependencyRegistrar
{
public virtual void Register(ContainerBuilder builder)
{
builder.RegisterType(typeof(DbContextOptionsFactory))
.As(typeof(IDbContextOptionsFactory))
.InstancePerRequest();
builder.RegisterType<AppDbContext>()
.As(typeof(IDbContext))
.WithParameter(
new ResolvedParameter(
(pi, cc) => pi.Name == "options",
(pi, cc) => cc.Resolve<IDbContextOptionsFactory>().Get()));
builder.RegisterGeneric(typeof(Repository<>))
.As(typeof(IRepository<>))
.SingleInstance();
}
}
public interface IDbContextOptionsFactory
{
DbContextOptions<AppDbContext> Get();
}
public class DbContextOptionsFactory : IDbContextOptionsFactory
{
public DbContextOptions<AppDbContext> Get()
{
try
{
IConfigurationRoot configuration = new ConfigurationBuilder()
.SetBasePath(AppDomain.CurrentDomain.BaseDirectory)
.AddJsonFile("appsettings.json")
.Build();
var builder = new DbContextOptionsBuilder<AppDbContext>();
if (EngineContext.Current.Resolve<IHttpContextAccessor>().HttpContext.Request.QueryString.ToString().ToLower().Contains("app1"))
DbContextConfigurer.Configure(builder, configuration.GetConnectionString("app1"));
else if (EngineContext.Current.Resolve<IHttpContextAccessor>().HttpContext.Request.QueryString.ToString().ToLower().Contains("app2"))
DbContextConfigurer.Configure(builder, configuration.GetConnectionString("app2"));
return builder.Options;
}
catch (Exception)
{
throw;
}
}
}
public class DbContextConfigurer
{
public static void Configure(DbContextOptionsBuilder<AppDbContext> builder, string connectionString)
{
builder.UseSqlServer(connectionString);
}
}
AppDbContext:
public class AppDbContext : DbContext, IDbContext
{
public AppDbContext(DbContextOptions<AppDbContext> options)
: base(options)
{
}
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
Assembly assemblyWithConfigurations = typeof(IDbContext).Assembly;
builder.ApplyConfigurationsFromAssembly(assemblyWithConfigurations);
}
//..
//..
}
During runtime, I am getting below error.
An unhandled exception occurred while processing the request.
DependencyResolutionException: None of the constructors found with
'Autofac.Core.Activators.Reflection.DefaultConstructorFinder' on type
'AppDbContext' can be invoked with the available
services and parameters: Cannot resolve parameter
'Microsoft.EntityFrameworkCore.DbContextOptions1[AppDbContext]
options' of constructor 'Void
.ctor(Microsoft.EntityFrameworkCore.DbContextOptions
1[AppDbContext])'.
Autofac.Core.Activators.Reflection.ReflectionActivator.GetValidConstructorBindings(ConstructorInfo[]
availableConstructors, IComponentContext context,
IEnumerable parameters)
I have tried as answered here..but, not working.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…