无法在“System.String”类型上选择长度相等的多个构造函数

时间:2013-07-31 18:05:59

标签: string autofac

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

  

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

该类型有1个构造函数,它带有IRepositoryILog所以我真的不知道System.String在图片中的位置。我很困惑。有谁知道问题是什么?

这是堆栈跟踪:

  

at Autofac.Core.Activators.Reflection.MostParametersConstructorSelector.SelectConstructorBinding(ConstructorParameterBinding [] constructorBindings)      at Autofac.Core.Activators.Reflection.ReflectionActivator.ActivateInstance(IComponentContext context,IEnumerable 1 parameters) at Autofac.Core.Resolving.InstanceLookup.Activate(IEnumerable 1个参数)      在Autofac.Core.Resolving.InstanceLookup.Execute()      at Autofac.Core.Resolving.ResolveOperation.GetOrCreateInstance(ISharingLifetimeScope currentOperationScope,IComponentRegistration registration,IEnumerable 1 parameters) at Autofac.Core.Resolving.InstanceLookup.ResolveComponent(IComponentRegistration registration, IEnumerable 1个参数)      at Autofac.Core.Activators.Reflection.AutowiringParameter。&lt;&gt; c_ DisplayClass2.b _0()      at Autofac.Core.Activators.Reflection.ConstructorParameterBinding.Instantiate()      at Autofac.Core.Activators.Reflection.ReflectionActivator.ActivateInstance(IComponentContext context,IEnumerable 1 parameters) at Autofac.Core.Resolving.InstanceLookup.Activate(IEnumerable 1个参数)      在Autofac.Core.Resolving.InstanceLookup.Execute()      at Autofac.Core.Resolving.ResolveOperation.GetOrCreateInstance(ISharingLifetimeScope currentOperationScope,IComponentRegistration registration,IEnumerable 1 parameters) at Autofac.Core.Resolving.InstanceLookup.ResolveComponent(IComponentRegistration registration, IEnumerable 1个参数)      at Autofac.Core.Activators.Reflection.AutowiringParameter。&lt;&gt; c_ DisplayClass2.b _0()      at Autofac.Core.Activators.Reflection.ConstructorParameterBinding.Instantiate()      at Autofac.Core.Activators.Reflection.ReflectionActivator.ActivateInstance(IComponentContext context,IEnumerable 1 parameters) at Autofac.Core.Resolving.InstanceLookup.Activate(IEnumerable 1个参数)      在Autofac.Core.Resolving.InstanceLookup.Execute()      at Autofac.Core.Resolving.ResolveOperation.GetOrCreateInstance(ISharingLifetimeScope currentOperationScope,IComponentRegistration registration,IEnumerable 1 parameters) at Autofac.Core.Resolving.ResolveOperation.ResolveComponent(IComponentRegistration registration, IEnumerable 1个参数)      at Autofac.Core.Resolving.ResolveOperation.Execute(IComponentRegistration registration,IEnumerable 1 parameters) at Autofac.Core.Lifetime.LifetimeScope.ResolveComponent(IComponentRegistration registration, IEnumerable 1个参数)      at Autofac.ResolutionExtensions.TryResolveService(IComponentContext context,Service service,IEnumerable 1 parameters, Object& instance) at Autofac.ResolutionExtensions.ResolveService(IComponentContext context, Service service, IEnumerable 1个参数)      at Autofac.ResolutionExtensions.Resolve(IComponentContext context,Type serviceType,IEnumerable 1 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](Func 1 func,CancellationToken cancellationToken)

2 个答案:

答案 0 :(得分:4)

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

当您未自行设置主题时,Autofac会自动为主题的构造函数创建对象。

但是,当构造函数具有String参数时,它无法创建字符串,因为String没有无参数构造函数! [1]

您需要明确地在主题的构造函数上设置所有字符串。您还可以使用NamedParameters并为Strings提供显式值。

祝你好运!

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

答案 1 :(得分:0)

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

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

改为:

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

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