4

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

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

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

这是堆栈跟踪:

在 Autofac.Core.Activators.Reflection.MostParametersConstructorSelector.SelectConstructorBinding(ConstructorParameterBinding[] constructorBindings) 在 Autofac.Core.Activators.Reflection.ReflectionActivator.ActivateInstance(IComponentContext 上下文,IEnumerable 1 parameters) at Autofac.Core.Resolving.InstanceLookup.Activate(IEnumerable1 参数) 在 Autofac.Core.Resolving.InstanceLookup.Execute()在 Autofac.Core.Resolving.ResolveOperation.GetOrCreateInstance(ISharingLifetimeScope currentOperationScope,IComponentRegistration 注册,IEnumerable 1 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 context, IEnumerable 1 parameters) at Autofac.Core.Resolving.InstanceLookup.Activate(IEnumerable1 参数) 在 Autofac.Core.Resolving.InstanceLookup.Execute() 在Autofac.Core.Resolving.ResolveOperation.GetOrCreateInstance(ISharingLifetimeScope currentOperationScope, IComponentRegistration registration, IEnumerable 1 parameters) at Autofac.Core.Resolving.InstanceLookup.ResolveComponent(IComponentRegistration registration, IEnumerable1 parameters) at Autofac.Core.Activators.Reflection.AutowiringParameter.<>c_ DisplayClass2.b _0() at 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 注册, IEnumerable 1 parameters) at Autofac.Core.Resolving.ResolveOperation.ResolveComponent(IComponentRegistration registration, IEnumerable1 个参数) 在 Autofac.Core.Resolving.ResolveOperation.Execute(IComponentRegistration 注册, IEnumerable 1 parameters) at Autofac.Core.Lifetime.LifetimeScope.ResolveComponent(IComponentRegistration registration, IEnumerable1 参数)在 Autofac.ResolutionExtensions.TryResolveService(IComponentContext 上下文,服务服务,IEnumerable 1 parameters, Object& instance) at Autofac.ResolutionExtensions.ResolveService(IComponentContext context, Service service, IEnumerable1 参数)在 Autofac.ResolutionExtensions.Resolve(IComponentContext 上下文,类型 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](Func1 func,CancellationToken 取消令牌)

4

2 回答 2

4

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

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

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

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

祝你好运!

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

于 2014-10-31T11:05:18.307 回答
0

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

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

改为:

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

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

于 2015-10-28T20:00:14.760 回答