#HowTo fix the error «This resolve operation has already ended. When registering components using lambdas, the IComponentContext ‘c'» con Autofac.

In this post I’ll explain to you how to fix the error: #HowTo resolver el error «This resolve operation has already ended. When registering components using lambdas, the IComponentContext ‘c'».

Getting started.

  • This example uses Autofac 6.3.0, it might not work in other versions.
  • The dependency injections occur in the constructor.

Example:

I need to inject a list of rules in the constructor of a class. I got this error at runtime.

System.ObjectDisposedException: '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.'

What causes the error?

The error is thrown because the component context was already disposed of when the new instance is created.

How to fix it?

You can resolve this issue by solving manually a IComponentContex class, instead of using the given argument.

Gustavo Sánchez