Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Autofac C#Resolve中,如何将参数传递给构造函数_C#_Dependency Injection_Autofac_.net 4.8 - Fatal编程技术网

在Autofac C#Resolve中,如何将参数传递给构造函数

在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

我有一个与类的接口,该类的构造函数将Autofac IContainer作为参数。如何一次传递这个参数来解析这个类。我尝试使用新的NamedParameter,但出现错误

等级 在控制台应用程序中
出现错误是因为命名参数是
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));