【问题标题】:Cannot choose between multiple constructors with equal length 1 on type 'System.String'无法在类型“System.String”的多个等长 1 的构造函数之间进行选择
【发布时间】:2013-07-31 18:05:59
【问题描述】:

尝试解析类型时出现以下错误:

无法在类型“System.String”的多个长度为 1 的构造函数之间进行选择。注册组件时,使用 UsingConstructor() 配置方法显式选择构造函数。

该类型有 1 个构造函数,该构造函数采用 IRepositoryILog,所以我真的不知道 System.String 在图片中的位置。我很困惑。有谁知道问题出在哪里?

这是堆栈跟踪:

在 Autofac.Core.Activators.Reflection.MostParametersConstructorSelector.SelectConstructorBinding(ConstructorParameterBinding[] constructorBindings) 在 Autofac.Core.Activators.Reflection.ReflectionActivator.ActivateInstance(IComponentContext 上下文,IEnumerable1 parameters) at Autofac.Core.Resolving.InstanceLookup.Activate(IEnumerable1 参数) 在 Autofac.Core.Resolving.InstanceLookup.Execute() 在 Autofac.Core.Resolving.ResolveOperation.GetOrCreateInstance(ISharingLifetimeScope currentOperationScope,IComponentRegistration 注册,IEnumerable1 parameters) at Autofac.Core.Resolving.InstanceLookup.ResolveComponent(IComponentRegistration registration, IEnumerable1 参数) 在 Autofac.Core.Activators.Reflection.AutowiringParameter.c_DisplayClass2.b_0() 在 Autofac.Core.Activators.Reflection.ConstructorParameterBinding.Instantiate() 在 Autofac.Core.Activators.Reflection.ReflectionActivator.ActivateInstance(IComponentContext 上下文,IEnumerable1 parameters) at Autofac.Core.Resolving.InstanceLookup.Activate(IEnumerable1 参数) 在 Autofac.Core.Resolving.InstanceLookup.Execute() 在 Autofac.Core.Resolving.ResolveOperation.GetOrCreateInstance(ISharingLifetimeScope currentOperationScope,IComponentRegistration 注册,IEnumerable1 parameters) at Autofac.Core.Resolving.InstanceLookup.ResolveComponent(IComponentRegistration registration, IEnumerable1 参数) 在 Autofac.Core.Activators.Reflection.AutowiringParameter.c_DisplayClass2.b_0() 在 Autofac.Core.Activators.Reflection.ConstructorParameterBinding.Instantiate() 在 Autofac.Core.Activators.Reflection.ReflectionActivator.ActivateInstance(IComponentContext 上下文,IEnumerable1 parameters) at Autofac.Core.Resolving.InstanceLookup.Activate(IEnumerable1 参数) 在 Autofac.Core.Resolving.InstanceLookup.Execute() 在 Autofac.Core.Resolving.ResolveOperation.GetOrCreateInstance(ISharingLifetimeScope currentOperationScope,IComponentRegistration 注册,IEnumerable1 parameters) at Autofac.Core.Resolving.ResolveOperation.ResolveComponent(IComponentRegistration registration, IEnumerable1 参数) 在 Autofac.Core.Resolving.ResolveOperation.Execute(IComponentRegistration 注册,IEnumerable1 parameters) at Autofac.Core.Lifetime.LifetimeScope.ResolveComponent(IComponentRegistration registration, IEnumerable1 参数) 在 Autofac.ResolutionExtensions.TryResolveService(IComponentContext 上下文,服务服务,IEnumerable1 parameters, Object& instance) at Autofac.ResolutionExtensions.ResolveService(IComponentContext context, Service service, IEnumerable1 参数) 在 Autofac.ResolutionExtensions.Resolve(IComponentContext context, Type serviceType, IEnumerable1 parameters) at Autofac.ResolutionExtensions.Resolve(IComponentContext context, Type serviceType) at SomeCompany.ComponentModel.Composition.AutofacIocContainer.Resolve(Type type) in c:\SomeCompany.Core\ComponentModel\Composition\AutofacIocContainer.cs:line 17 at SomeCompany.Commands.CommandFactory.Create(String name) in c:\SomeCompany.Core\Commands\CommandFactory.cs:line 28 at SomeCompany.Web.Controllers.CommandsController.Post(String id, String request) in c:\SomeCompany.Web\Controllers\CommandsController.cs:line 49 at lambda_method(Closure , Object , Object[] ) at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass13.<GetExecutor>b__c(Object instance, Object[] methodParameters) at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.Execute(Object instance, Object[] arguments) at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.<>c__DisplayClass5.<ExecuteAsync>b__4() at System.Threading.Tasks.TaskHelpers.RunSynchronously[TResult](Func1 func, CancellationToken cancelToken)

【问题讨论】:

  • IRepository 和 ILog 解析到什么?实现这些接口的类的构造函数可能采用字符串参数,或者它们可以采用具有带有字符串参数的构造函数的依赖项,等等。
  • 请显示您的配置和正在解析的类型的定义。会不会是它的依赖项之一(IRepositoryILog 或它们的递归依赖项)包含多个构造函数? Autofac 不会抛出类型名称(可能在内部异常中)?
  • 你有没有想过这个问题?
  • 对不起,伙计们,我当时工作的公司将他们的大部分开发产品运送到海外,我和其他许多人都伤亡了。我希望他们能得到他们所支付的:) 谢谢你的回答!我很确定我已经解决了这个问题,但我没有履行更新这个问题的职责,为此,我深表歉意。

标签: string autofac


【解决方案1】:

这与您自己的代码上的多个构造函数无关!

当您没有自己明确设置对象时,Autofac 会自动为您的主题的构造函数创建对象。

但是,当你的构造函数有一个String参数时,它不能创建一个字符串,因为String没有一个无参数的构造函数! [1]

您需要在主题的构造函数上明确设置所有字符串。您还可以使用 NamedParameters 并给字符串一个明确的值。

祝你好运!

[1] http://msdn.microsoft.com/en-us/library/system.string%28v=vs.110%29.aspx

【讨论】:

  • 这是正确的答案,但是如果您有一个相当复杂的容器连接起来遍历所有依赖项以找到带有字符串的容器,那么 Autofac 提供的一些更有用的调试信息会更好有点痛。
【解决方案2】:

如果您有一个数据库定义文件 (.dbml),请检查您未对代码进行的修改。就我而言,以下方法

public JudicialDataContext(string connectionString) : base(connectionString, mappingSource)
{
    OnCreated();
}

改成这样了:

public JudicialDataContext(string connection) : base(connection, mappingSource)
{
    OnCreated();
}

我不知道它为什么这样做,或者它试图通过这样做来完成什么,或者为什么这种改变甚至是必要的。但是丢弃更改并重新编译克服了这个错误。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-22
    • 1970-01-01
    • 2014-10-19
    • 1970-01-01
    • 2014-10-30
    相关资源
    最近更新 更多