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!



About Amogh Natu

Technology enthusiast, Associate Consultant @ Microsoft, music lover, love my guitar, Microsoft.NET, ASP.NET, C#.NET Professional.
This entry was posted in General and tagged , , , , . Bookmark the permalink.

Your comments or opinions.....

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s