The type X has multiple constructors of length 1. Unable to disambiguate
Asked Answered
C

3

6

I'm working at a ASP.NET Webforms project and is using Unity for DI. This project also use DevExpress ASP.NET Ajax Controls.

Now we have run into problem and it seems to be a conflict between DevExpress and Unity.

This is the Unity configuration

    [assembly: WebActivator.PostApplicationStartMethod(typeof(Sales.Web.App_Start.UnityWebFormsStart), "PostStart")]
namespace Sales.Web.App_Start
{
    /// <summary>
    ///     Startup class for the Unity.WebForms NuGet package.
    /// </summary>
    internal static class UnityWebFormsStart
    {
        /// <summary>
        ///     Initializes the unity container when the application starts up.
        /// </summary>
        /// <remarks>
        ///     Do not edit this method. Perform any modifications in the
        ///     <see cref="RegisterDependencies" /> method.
        /// </remarks>
        internal static void PostStart()
        {
            IUnityContainer container = new UnityContainer();
            HttpContext.Current.Application.SetContainer(container);

            RegisterDependencies(container);
        }

        /// <summary>
        ///     Registers dependencies in the supplied container.
        /// </summary>
        /// <param name="container">Instance of the container to populate.</param>
        private static void RegisterDependencies(IUnityContainer container)
        {
            // TODO: Add any dependencies needed here
            container
                .RegisterType<IDbFactory, DbFactory>(new HierarchicalLifetimeManager())
                .RegisterType(typeof(IDbProxy<>), typeof(DbProxy<>))
                .RegisterType<IErpData, ErpData>(new HierarchicalLifetimeManager())
                .RegisterType<ICaseData, CaseData>(new HierarchicalLifetimeManager())
                .RegisterType<ICaseCauseData, CaseCauseData>(new HierarchicalLifetimeManager())
                .RegisterType<ICaseHandler, CaseHandler>(new HierarchicalLifetimeManager());
        }
    }
}

Any of this Unity configuration has nothing to do with the DevExpress controls, but i think it hooks up the HttpContext object.

And this error appears only when i use the TabControl from DevExpress, all other controls works well.

See the attached image that describe the error message more in detail.

enter image description here

Cockadoodledoo answered 26/1, 2015 at 15:9 Comment(0)
N
14

By convention, Unity prefers the constructor with the longest parameter list if no other configuration was supplied. Having two constructors with parameter list of equal length creates an ambiguity, so Unity throws an exception. That's why it cannot resolve the control you are using.

You can explicitly tell Unity which constructor to prefer:

container.RegisterType<IService, Service>(new InjectionConstructor(typeof(IServiceDependency)));
Nonmoral answered 26/1, 2015 at 15:37 Comment(1)
+1. While I understand WHY Unity would look for a constructor with the maximum number of parameters, this is something I had not thought of before and appears to be not very elegant. But then again, we're resorting to DI to achieve IoC so can't really complain.Hypognathous
H
7

You can use the [InjectionConstructor] attribute on the contructor wanted

Hung answered 5/11, 2015 at 15:41 Comment(0)
O
0

I have had the same problem adding this into my unityconifg resloved it

    public static void RegisterTypes(IUnityContainer container)
    {
        container.UseApplicationLayer(false);
        container.UseApplicationRepository(false);
        container.ConfigureMappings();
    }

using Nhiberante

Orlene answered 28/10, 2019 at 12:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.