使用参数异常的构造函数注入 AppDbContext
Injecting AppDbContext with constructor with params exception
我正在尝试在我的 类 之一中使用 AppDbContext
(属于 entity framework 核心)并使用 autofac 对其进行实例化,如下所示:
public class AppDbContext : IdentityDbContext<ApplicationUser>
{
public AppDbContext(DbContextOptions<AppDbContext> options) : base(options)
{
}
protected override void OnModelCreating(ModelBuilder builder)
{
builder.Entity<Message>().Property(m => m.Service).HasConversion<int>();
builder.Entity<ApplicationUser>().HasMany<Message>(m => m.Messages).WithOne(u => u.User).IsRequired();
base.OnModelCreating(builder);
}
public DbSet<Message> Messages { get; set; }
public DbSet<UsersCredentialsModel> UsersCredentialsModels { get; set; }
public DbSet<CookieModel> CookieModel { get; set; }
}
我很难完全理解如何实现这个correctly.could有人扔骨头吗?
public static IContainer Startup()
{
var builder = new ContainerBuilder();
// builder.RegisterType<AppDbContext>().As<IdentityDbContext<ApplicationUser>>().InstancePerRequest();
builder.RegisterType<Application>().As<IApplication>();
builder.RegisterType<RabbitMQImpl>().As<IListenerQueue>().SingleInstance();
builder.RegisterType<BotFactory>().As<IBotFactory>();
var instance = QuartzInstance.Instance;
builder.RegisterType<QueueImpl>().AsImplementedInterfaces();
builder.RegisterType<ConsumerSchechuler>().AsImplementedInterfaces();
builder.RegisterType<SchedulerImpl>().AsImplementedInterfaces();
builder.RegisterModule(new QuartzAutofacJobsModule(typeof(ConsumerSchedulerJob).Assembly)).RegisterAssemblyModules();
builder.RegisterModule(new QuartzAutofacJobsModule(typeof(DailyCleanUpJob).Assembly)).RegisterAssemblyModules();
builder.RegisterInstance(QuartzInstance.Instance).AsImplementedInterfaces();
return builder.Build();
这是我遇到的错误。
DependencyResolutionException: None of the constructors found with 'Autofac.Core.Activators.Reflection.DefaultConstructorFinder' on type 'JobsImpl.DailyCleanUpJob' can be invoked with the available services and parameters:
Cannot resolve parameter 'Utils.AppDbContext context' of constructor 'Void .ctor(Utils.AppDbContext)'.
一位朋友帮我解决了这个问题:
如果您遇到同样的问题,请使用代码:
var contextOptions = new DbContextOptionsBuilder<AppDbContext>().Options;
//... configure options as necessary
builder.RegisterInstance(contextOptions).As<DbContextOptions<AppDbContext>>();
builder.RegisterType<IdentityDbContext>();
builder.RegisterType<AppDbContext>();
因为 AppDbContext
的构造函数正在接收 DbContextOptions<AppDbContext>
的 实例 你需要首先实例化它,然后将实例注册为 DbContextOptions<AppDbContext>
.
我正在尝试在我的 类 之一中使用 AppDbContext
(属于 entity framework 核心)并使用 autofac 对其进行实例化,如下所示:
public class AppDbContext : IdentityDbContext<ApplicationUser>
{
public AppDbContext(DbContextOptions<AppDbContext> options) : base(options)
{
}
protected override void OnModelCreating(ModelBuilder builder)
{
builder.Entity<Message>().Property(m => m.Service).HasConversion<int>();
builder.Entity<ApplicationUser>().HasMany<Message>(m => m.Messages).WithOne(u => u.User).IsRequired();
base.OnModelCreating(builder);
}
public DbSet<Message> Messages { get; set; }
public DbSet<UsersCredentialsModel> UsersCredentialsModels { get; set; }
public DbSet<CookieModel> CookieModel { get; set; }
}
我很难完全理解如何实现这个correctly.could有人扔骨头吗?
public static IContainer Startup()
{
var builder = new ContainerBuilder();
// builder.RegisterType<AppDbContext>().As<IdentityDbContext<ApplicationUser>>().InstancePerRequest();
builder.RegisterType<Application>().As<IApplication>();
builder.RegisterType<RabbitMQImpl>().As<IListenerQueue>().SingleInstance();
builder.RegisterType<BotFactory>().As<IBotFactory>();
var instance = QuartzInstance.Instance;
builder.RegisterType<QueueImpl>().AsImplementedInterfaces();
builder.RegisterType<ConsumerSchechuler>().AsImplementedInterfaces();
builder.RegisterType<SchedulerImpl>().AsImplementedInterfaces();
builder.RegisterModule(new QuartzAutofacJobsModule(typeof(ConsumerSchedulerJob).Assembly)).RegisterAssemblyModules();
builder.RegisterModule(new QuartzAutofacJobsModule(typeof(DailyCleanUpJob).Assembly)).RegisterAssemblyModules();
builder.RegisterInstance(QuartzInstance.Instance).AsImplementedInterfaces();
return builder.Build();
这是我遇到的错误。
DependencyResolutionException: None of the constructors found with 'Autofac.Core.Activators.Reflection.DefaultConstructorFinder' on type 'JobsImpl.DailyCleanUpJob' can be invoked with the available services and parameters: Cannot resolve parameter 'Utils.AppDbContext context' of constructor 'Void .ctor(Utils.AppDbContext)'.
一位朋友帮我解决了这个问题:
如果您遇到同样的问题,请使用代码:
var contextOptions = new DbContextOptionsBuilder<AppDbContext>().Options;
//... configure options as necessary
builder.RegisterInstance(contextOptions).As<DbContextOptions<AppDbContext>>();
builder.RegisterType<IdentityDbContext>();
builder.RegisterType<AppDbContext>();
因为 AppDbContext
的构造函数正在接收 DbContextOptions<AppDbContext>
的 实例 你需要首先实例化它,然后将实例注册为 DbContextOptions<AppDbContext>
.