4
votes

I am getting the following error when trying to resolve a type:

Cannot choose between multiple constructors with equal length 1 on type 'System.String'. Select the constructor explicitly, with the UsingConstructor() configuration method, when the component is registered.

The type has 1 constructor that takes an IRepository and an ILog so I don't really know where the System.String is coming in to the picture. I'm perplexed. Does anyone have any idea what the problem is?

Here is the stack trace:

at Autofac.Core.Activators.Reflection.MostParametersConstructorSelector.SelectConstructorBinding(ConstructorParameterBinding[] constructorBindings) at Autofac.Core.Activators.Reflection.ReflectionActivator.ActivateInstance(IComponentContext context, IEnumerable1 parameters) at Autofac.Core.Resolving.InstanceLookup.Activate(IEnumerable1 parameters) at Autofac.Core.Resolving.InstanceLookup.Execute() at Autofac.Core.Resolving.ResolveOperation.GetOrCreateInstance(ISharingLifetimeScope currentOperationScope, IComponentRegistration registration, IEnumerable1 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() at Autofac.Core.Activators.Reflection.ReflectionActivator.ActivateInstance(IComponentContext context, IEnumerable1 parameters) at Autofac.Core.Resolving.InstanceLookup.Activate(IEnumerable1 parameters) at Autofac.Core.Resolving.InstanceLookup.Execute() at Autofac.Core.Resolving.ResolveOperation.GetOrCreateInstance(ISharingLifetimeScope currentOperationScope, IComponentRegistration registration, IEnumerable1 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() at Autofac.Core.Activators.Reflection.ReflectionActivator.ActivateInstance(IComponentContext context, IEnumerable1 parameters) at Autofac.Core.Resolving.InstanceLookup.Activate(IEnumerable1 parameters) at Autofac.Core.Resolving.InstanceLookup.Execute() at Autofac.Core.Resolving.ResolveOperation.GetOrCreateInstance(ISharingLifetimeScope currentOperationScope, IComponentRegistration registration, IEnumerable1 parameters) at Autofac.Core.Resolving.ResolveOperation.ResolveComponent(IComponentRegistration registration, IEnumerable1 parameters) at Autofac.Core.Resolving.ResolveOperation.Execute(IComponentRegistration registration, IEnumerable1 parameters) at Autofac.Core.Lifetime.LifetimeScope.ResolveComponent(IComponentRegistration registration, IEnumerable1 parameters) at Autofac.ResolutionExtensions.TryResolveService(IComponentContext context, Service service, IEnumerable1 parameters, Object& instance) at Autofac.ResolutionExtensions.ResolveService(IComponentContext context, Service service, IEnumerable1 parameters) at 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 cancellationToken)

2
What do IRepository and ILog resolve to? The constructors for the classes implementing those interfaces might take a string param, or they could take dependencies which have a constructor with a string param, etc.Mike Mertsock
Please show your configuration and the definition of the type being resolved. Could it be that one of its dependencies (IRepository, ILog, or their dependencies recursively) contain multiple constructors? Doensn't Autofac throw in a type name (in the inner exception perhaps)?Steven
Did you ever figure this out?ps2goat
Sorry guys, the company I was working for at this time shipped much of their dev offshore and I, along with many others, were casualties. I hope they get exactly what they pay for :) Thank you for the answers though! I'm pretty sure I resolved this but I did not fulfill my duty to update this question and for that, I apologize.ebonnett

2 Answers

4
votes

This has nothing to do with multiple constuctors on your own code!

Autofac automatically creates objects for the constructor of your subject when you don't set them explicitly yourself.

However, when your constructor has a String parameter, it can not create a string, as String does not have a parameterless constructor! [1]

You need to set all strings on the constructor of your subject explicitly. You can also use NamedParameters and give Strings an explicit value.

Good luck!

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

0
votes

If you have a database-definition file (.dbml), check for modifications to the code that you didn't make. In my case, the following method

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

was changed to this:

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

I have no idea why it did this, or what it is trying to accomplish by doing this, or why this change would even be necessary. But discarding the change and recompiling overcame this error.