I'm working on an application using Caliburn.Micro and Autofac.
In my composition root I'm now facing a problem with Autofac:
I have to inject the globally used IEventAggregator into my FirstViewModel, and a second IEventAggregator that has to be used only by this FirstViewModel and it's children.
My idea was to make the second one be injected as Owned<IEA>
, and it works, the container provides a different instance of IEA.
public FirstViewModel(
IEventAggregator globalEA,
IEventAggregator localEA,
Func<IEventAggregator, SecondViewModel> secVMFactory) {}
The problem comes when I have to provide the event aggregators to the SecondViewModel.
To create the SecondViewModel I use a factory method as Func<IEA, SecondVM>
.
The SecondViewModel's constructor is the following:
public SecondViewModel(IEventAggregator globalEA, IEventAggregator localEA) {}
I want the container to inject the first as the registered one, and the second will be the IEA parameter of the Func<IEA, SecVM>
.
this is the function I registered in the container:
builder.Register<Func<IEventAggregator, SecondViewModel>>(
c =>
(ea) =>
{
return new SecondViewModel(
c.Resolve<IEventAggregator>(),
ea);
}
);
but when it gets called by the FirstViewModel
I get the following error:
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.
I can't understand where the problem is, can you help me please, what am I missing?
Thank you.
See Question&Answers more detail:
os