Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 640
Nils Anders Avatar asked Jan 26 '15 15:01

Nils Anders


2 Answers

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)));
like image 75
Sergey Kolodiy Avatar answered Nov 07 '22 05:11

Sergey Kolodiy


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

like image 35
k4st0r42 Avatar answered Nov 07 '22 03:11

k4st0r42