Cannot choose between multiple constructors of equal length 1 by type 'System.String'

When trying to resolve a type, the following error occurs:

It is not possible to choose between multiple constructors with equal length 1 of type "System.String". Select the constructor explicitly using the UsingConstructor () configuration method when the component is registered.

The type has 1 constructor that accepts IRepository and ILog , so I don’t know where System.String is in the image. I am perplexed. Does anyone know what the problem is?

Here is the stack trace:

in Autofac.Core.Activators.Reflection.MostParametersConstructorSelector.SelectConstructorBinding (ConstructorParameterBinding [] constructorBindings) on Autofac.Core.Activators.Reflection.ReflectionActivator.ActivateConententocentivecontent 1 parameters) at Autofac.Core.Resolving.InstanceLookup.Activate(IEnumerable 1 parameter) on Autofac.Core.Resolving.InstanceLookup.Execute () on Autofac.Core.Resolving.ResolveOperation.GetOrCreateInstance (ISharingLifetimeScope currentOperationScope, registration IComponentRegistration, IEnumerable 1 parameters) at Autofac.Core.Resolving.InstanceLookup.ResolveComponent(IComponentRegistration registration, IEnumerable 1 parameter) on Autofac.Core.Activators.Reflection.AutowiringParameter. <> c_DisplayClass2.b_0 () on Autofac.Core.Activators.Reflection.ConstructorParameterBinding.Instantiate () on Autofac.Core.Activators.Reflection.ReflectionActivator.ActivateInstance (Context IComponentContext, IEnumerableRec 1 parameters) at Autofac.Core.Resolving.InstanceLookup.Activate(IEnumerable 1 parameter) on Autofac.Core.Resolving.InstanceLookup.Execute () on Autofac.Core.Resolving.ResolveOperation.GetOrCreateInstance (ISharingLifetimeScope currentOperationScope, registration IComponentRegistration, 1 parameters) at Autofac.Core.Resolving.InstanceLookup.ResolveComponent(IComponentRegistration registration, IEnumerable 1 parameter) on Autofac.Core.Activators.Reflection.AutowiringParameter. <> c_DisplayClass2.b_0 () on Autofac.Core.Activators.Reflection.ConstructorParameterBinding.Instantiate () on Autofac.Core.Activators.Reflection.ReflectionActivator.ActivateInstance (Context IComponentContext, IEnumerableRec 1 parameters) at Autofac.Core.Resolving.InstanceLookup.Activate(IEnumerable 1 parameter) on Autofac.Core.Resolving.InstanceLookup.Execute () on Autofac.Core.Resolving.ResolveOperation.GetOrCreateInstance (ISharingLifetimeScope currentOperationScope, registration IComponentRegistration, 1 parameters) at Autofac.Core.Resolving.ResolveOperation.ResolveComponent(IComponentRegistration registration, IEnumerable 1 parameter) on Autofac.Core.Resolving.ResolveOperation.Execute (registration IComponentRegistration, IEnumerable 1 parameters) at Autofac.Core.Lifetime.LifetimeScope.ResolveComponent(IComponentRegistration registration, IEnumerable solveService (IComponentContext context, service, IEnumerable 1 parameters, Object& instance) at Autofac.ResolutionExtensions.ResolveService(IComponentContext context, Service service, IEnumerable 1 parameter) in Autofac.ResolutionExtensions.Resolve (IComponentContext context, Typenumerable 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 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 cancelationToken)

+4
source share
2 answers

This has nothing to do with multiple conductors in your own code!

Autofac automatically creates objects for the constructor of your object if you do not set them explicitly.

However, when your constructor has a String parameter, it cannot create a string because String does not have a constructor without parameters! [one]

You need to set all the lines in the constructor of your object explicitly. You can also use NamedParameters and give the strings an explicit value.

Good luck

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

+4
source

If you have a database definition file (.dbml), check for changes to the code that you did not. In my case, the following method

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

has been changed to the following:

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

I have no idea why he did it or what he is trying to do by doing this, or why this change is even necessary. But the rejection of change and recompilation overcame this error.

0
source

Source: https://habr.com/ru/post/1494550/


All Articles