我使用autofac作为DI容器。
我想将参数store
注入到构造函数中。
这是我的构造函数的样子。
public SomeClass (IMyCouchStore store)
{
this.store = store;
}
< p >“store”参数需要两个字符串参数才能被实例化:
// sample instantiation
var store = new MyCouchStore("http://someUri","someDbName");
在引导过程中,我尝试注册这两个参数:
builder
.RegisterType<MyCouchStore>()
.As<IMyCouchStore>()
.WithParameters(new [] {
new NamedParameter("dbUri","http://someUri"),
new NamedParameter("dbName","someDbName")
}
然而,我收到了以下错误信息:
Autofac.Core.DependencyResolutionException
在 'MyCouch.MyCouchStore' 类型上有多个长度相等的构造函数,无法选择。当组件被注册时,请使用 UsingConstructor() 配置方法显式地选择构造函数。
我该如何注入多个相同类型的参数?
UsingConstructor()
明确选择构造函数。 - Aydin