Autofac and Func factories
Asked Answered
C

1

32

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.

Chestnut answered 14/12, 2013 at 12:33 Comment(0)
S
69

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); 
     };
});
Sovereign answered 14/12, 2013 at 13:26 Comment(4)
haha I tried to do that but I put it the inner function, so i kept getting the same error! +1 one more question: should I do it in every registration in order to prevent any vm order instantiation refactoring?Chestnut
It depends on your requirements, you can always c.Resolve<IComponentContext>(); when you are registering a Func into the container or just do it only in the cases where you actually need to call Resolve in your factory.Sovereign
You can read more about this problem in the post The Curious Case of a Deadlock in AutofacChantay
Thank you for this great answer, it solved my problem! I just don't understand the solution completely. If c gets out of scope by the time we use the registered Func, how come context does not go out of scope? Thanks!Behistun

© 2022 - 2024 — McMap. All rights reserved.