在Autofac C#Resolve中,如何将参数传递给构造函数
我有一个与类的接口,该类的构造函数将Autofac IContainer作为参数。如何一次传递这个参数来解析这个类。我尝试使用新的NamedParameter,但出现错误 等级 在控制台应用程序中在Autofac C#Resolve中,如何将参数传递给构造函数,c#,dependency-injection,autofac,.net-4.8,C#,Dependency Injection,Autofac,.net 4.8,我有一个与类的接口,该类的构造函数将Autofac IContainer作为参数。如何一次传递这个参数来解析这个类。我尝试使用新的NamedParameter,但出现错误 等级 在控制台应用程序中 出现错误是因为命名参数是container,但IContainer是此参数的类型。 您可以将代码更改为: var appAmbientState = buildContainer.Resolve<IAppAmbientState>(new NamedParameter("cont
出现错误是因为命名参数是
container
,但IContainer
是此参数的类型。
您可以将代码更改为:
var appAmbientState = buildContainer.Resolve<IAppAmbientState>(new NamedParameter("container", buildContainer));
var appAmbientState=buildContainer.Resolve(新名称参数(“container”,buildContainer));
它会起作用的
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)'.
var appAmbientState = buildContainer.Resolve<IAppAmbientState>(new NamedParameter("container", buildContainer));