在依赖实现的情况下,如何将配置值传递给构造函数

时间:2019-07-02 04:25:34

标签: c# dependency-injection autofac

我正在使用Autofac进行IoC

这是我的类的构造函数,它以IProcessorsize作为参数

public GatewayClient(IProcessor processor,
      int size)
{
   _size = size;
   _processor = processor;
}

这是我的容器启动器类,其职责是注册依赖项。

var size = Convert.ToInt32(configuration.GetValue<string>("Settings:SizeInKb"));

builder.RegisterType<Processor>().As<IProcessor>().SingleInstance();
builder.Register(c => new GatewayClient(size)).As<IGatewayClient>().SingleInstance();

错误说明如下

  

没有给出与“ GatewayClient(IProcessor,int)” MyProject.GatewayService C:\ work \ GatewayService \ ContainerInitiator.cs所需的形式参数“ processor”相对应的参数。

问题是,如何将size传递给构造函数? autofac会照顾好吗?

0 个答案:

没有答案
相关问题