我正在使用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) –