0

我正在使用此处找到的 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>();

如果您能提供帮助,请提前致谢。

4

1 回答 1

0

好,我知道了。需要这条线:

builder.RegisterType<DataContext>().InstancePerOwned<EmailAllocationJob>()
    .As<IUnitOfWork>().AsSelf();

因此,DataContext 作为 RegisterType ONCE 的通用参数似乎很重要,并且对 As<>() 和 AsSelf() 的方法调用将菊花链式连接在单个语句中。现在似乎很明显,昨天晚上有一个新的头脑。

于 2013-05-23T08:02:06.050 回答