Resolving IContainer in Constructor when using Autofac


While using Unity as an Inversion of Control and Dependency Injection container, there is an option to directly inject the container into a class’s constructor by placing the IUnityContainer interface as a constructor parameter. This container can then be used by the class members for further resolution.

This option is not directly available in Autofac. If we put a parameter of IContainer type in a class’s constructor, then we get the below exception.

None of the constructors found with 
on type 'your_class' can be invoked with the 
available services and parameters:

Cannot resolve parameter 'Autofac.IContainer container' 
of constructor 'Void .ctor(Autofac.IContainer)'.

Instead, you can inject the IComponentContext or the ILifetimeScope interfaces in the constructor of your class and use the injected instance of either of these for resolving further dependencies.


public class SampleClass
    public SampleClass(ILifetimeScope lifetimeScope)
        // basically using the lifetimescope parameter 
        // as a container for resolving other dependencies.

        var someOtherDependency = 


Hope this helps!


This entry was posted in General and tagged , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *