我正在使用此处找到的 Owned 类型: Autofac 2 的强参考
我还在使用 Quartz 调度程序、MSMQ 和 EF。
我的配置如下所示。我显然有问题,因为注入存储库的上下文与提供给服务的实例不同。
builder.RegisterType<EmailAllocationJob>();
builder.RegisterGeneric(typeof(JobWrapper<>));
builder.RegisterType<DataContext>().InstancePerOwned<EmailAllocationJob>();
builder.RegisterType<DataContext>().As<IUnitOfWork>();
builder.RegisterType<EmailAccountRepository>().As<IEmailAccountRepository>();
builder.RegisterType<EmailMessageRepository>().As<IEmailMessageRepository>();
builder.RegisterType<EmailMessageQueue>().As<IEmailMessageQueue>();
builder.RegisterType<EmailAllocationService>().As<IEmailAllocationService>();
我一生都无法弄清楚如何修复配置。我认为这是行:
builder.RegisterType<DataContext>().As<IUnitOfWork>();
我想说的是:
builder.RegisterType<DataContext>().As<IUnitOfWork>().InstancePerOwned<EmailAllocationJob>();
如果您能提供帮助,请提前致谢。