I have an Interface with the class whose constructor takes Autofac IContainer as a parameter. How I can pass this parameter at a time to resolve this class. I have tried to use new NamedParameter but getting an error
public class AppAmbientState : IAppAmbientState
{
public IContainer ServiceContainer { get; }
public AppAmbientState(
IContainer container
)
{
ServiceContainer = container;
}
}
var appAmbientState = buildContainer.Resolve<IAppAmbientState>(new NamedParameter("IContainer", "buildContainer"));
public static IContainer Configure()
{
ContainerBuilder builder = new ContainerBuilder();
builder.RegisterType<AppAmbientState>().As<IAppAmbientState>().SingleInstance();
DependencyResolutionException: None of the constructors found with 'Autofac.Core.Activators.Reflection.DefaultConstructorFinder' on type 'App.ConsoleHost.AmbientState.AppAmbientState' can be invoked with the available services and parameters:
Cannot resolve parameter 'Autofac.IContainer container' of constructor 'Void .ctor(Autofac.IContainer)'.
You are getting error because named argument is container
but IContainer
is type of this argument.
You can change your code to:
var appAmbientState = buildContainer.Resolve<IAppAmbientState>(new NamedParameter("container", buildContainer));
and it will work