我正在使用Caliburn.Micro和Autofac的应用程序。Autofac和Func工厂
在我的作文根我现在面临的一个问题Autofac: 我到全球范围内使用IEventAggregator注入我的FirstViewModel,并受本FirstViewModel只使用具有第二IEventAggregator和它的孩子。
我的想法是使第二个注入为Owned<IEA>
,它的工作原理是容器提供了IEA的另一个实例。
public FirstViewModel(
IEventAggregator globalEA,
IEventAggregator localEA,
Func<IEventAggregator, SecondViewModel> secVMFactory) {}
问题是当我必须提供事件聚合到SecondViewModel。
创建SecondViewModel我使用工厂方法Func<IEA, SecondVM>
。 的SecondViewModel的构造函数如下:
public SecondViewModel(IEventAggregator globalEA, IEventAggregator localEA) {}
我想要的容器注入第一作为注册一个,第二个将是Func<IEA, SecVM>
的IEA参数。
这是我在容器中注册的功能:
builder.Register<Func<IEventAggregator, SecondViewModel>>(
c =>
(ea) =>
{
return new SecondViewModel(
c.Resolve<IEventAggregator>(),
ea);
}
);
但是当它得到由FirstViewModel
我碰到下面的错误称为:
An exception of type 'System.ObjectDisposedException' occurred in Autofac.dll but was not handled in user code
Additional information: This resolve operation has already ended. When registering components using lambdas, the IComponentContext 'c' parameter to the lambda cannot be stored. Instead, either resolve IComponentContext again from 'c', or resolve a Func<> based factory to create subsequent components from.
我不明白问题出在哪里是,你能帮我吗,我错过了什么?
谢谢。
哈哈我试图做到这一点,但我把它放在内部的功能,所以我一直得到同样的错误! +1还有一个问题:我应该在每次注册时都这样做,以防止任何vm命令实例重构? – Sergio
这取决于你的需求,当你在容器中注册一个'Func'时,你总是可以'c.Resolve();'只有在你实际需要调用'Resolve'的情况下你的工厂。 –
nemesv
您可以在帖子中看到更多关于这个问题的文章[Autofac中的死锁的好奇案例](http://www.continuousimprover.com/2015/01/the-curious-case-of-deadlock-in-autofac。 html) –