Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Autofac and Func factories

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.

like image 621
Sergio Avatar asked Dec 14 '13 12:12

Sergio


People also ask

What is Autofac container?

Autofac is an addictive IoC container for . NET. It manages the dependencies between classes so that applications stay easy to change as they grow in size and complexity. This is achieved by treating regular . NET classes as components.

When registering components using lambdas the IComponentContext C parameter to the Lambda Cannot be stored?

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 Answers

You are calling secVMFactory outside of your FirstViewModel constructor so by that time the ResolveOperation is disposed and in your factory method the c.Resolve will throw the exception.

Luckily the exception message is very descriptive and telling you what to do:

When registering components using lambdas, the IComponentContext 'c' parameter to the lambda cannot be stored. Instead, either resolve IComponentContext again from 'c'

So instead of calling c.Resolve you need to resolve the IComponentContext from c and use that in your factory func:

builder.Register<Func<IEventAggregator, SecondViewModel>>(c => {      var context = c.Resolve<IComponentContext>();      return ea => {            return new SecondViewModel(context.Resolve<IEventAggregator>(), ea);       }; }); 
like image 169
nemesv Avatar answered Oct 02 '22 20:10

nemesv