【发布时间】:2016-04-27 14:27:11
【问题描述】:
我得到了那个例外:
抛出异常:Autofac.dll 中的“Autofac.Core.DependencyResolutionException”
附加信息:无法在类型“System.String”的多个长度为 1 的构造函数之间进行选择。注册组件时,使用 UsingConstructor() 配置方法显式选择构造函数。
堆栈跟踪:
at Autofac.Core.Activators.Reflection.MostParametersConstructorSelector.SelectConstructorBinding(ConstructorParameterBinding[] constructorBindings)
当我这样做时:
container.Resolve();
我如何注册我的服务:
builder.RegisterType<CustomerService>()
.As<ICustomerProcessingUnit>()
.WithParameter("MyUrl", AppSettings.Default.MyUrl);
我如何解决我的服务:
var customerService = container.Resolve<CustomerService>();
我的 CustomerService 类:
public class CustomerService: CustomerProcessingUnitBase
{
private string myUrl;
private readonly ISendService sendService;
public CustomerService(ISendService sendService, string myUrl)
{
this.sendService = sendService;
this.myUrl= myUrl;
}
}
在 ICustomerProcessingUnit.cs 文件中:
public interface ICustomerProcessingUnit
{
Task ProcessAsync();
}
public abstract class CustomerProcessingUnitBase : ICustomerProcessingUnit
{
public abstract Task ProcessAsync();
public void Process()
{
this.ProcessAsync().Wait();
}
}
为什么会出现这个异常?
我知道:Cannot choose between multiple constructors with equal length 1 on type 'System.String'
但是Autofac 为什么要创建一个字符串呢?我注入它!请参阅我的 .Register 调用,将字符串作为参数传递。
【问题讨论】:
-
你能分享一下堆栈跟踪吗?
-
你能不能只在第一行发布完整的堆栈跟踪蚂蚁? :-)
标签: c# dependency-injection autofac