我使用autofac作为DI容器。我的目标是将参数store注入构造函数。这就是我的构造函数的样子。
public SomeClass (IMyCouchStore store)
{
this.store = store;
} 为了实例化,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()配置方法显式地选择构造函数。
如何注入多个相同类型的参数?
发布于 2016-02-21 02:46:55
你的答案在你的问题中:)
使用
UsingConstructor()配置方法显式地选择构造函数。
public MyCouchStore(string httpSomeuri, string somedbname)
{
this.SomeUri = httpSomeuri;
this.SomeDbName = somedbname;
}builder.RegisterType<MyCouchStore>()
.As<IMyCouchStore>()
.UsingConstructor(typeof (string), typeof (string))
.WithParameters(new[]
{
new NamedParameter("httpSomeuri", "http://someUri"),
new NamedParameter("somedbname", Guid.NewGuid().ToString())
});https://stackoverflow.com/questions/35531684
复制相似问题