【问题标题】:The type X has multiple constructors of length 1. Unable to disambiguateX 类型有多个长度为 1 的构造函数。无法消除歧义
【发布时间】:2019-10-28 20:13:52
【问题描述】:

我在一个 ASP.NET Webforms 项目中工作,并且正在使用 Unity 进行 DI。 该项目还使用了 DevExpress ASP.NET Ajax 控件。

现在我们遇到了问题,似乎是 DevExpress 和 Unity 之间的冲突。

这是 Unity 配置

    [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());
        }
    }
}

任何 Unity 配置都与 DevExpress 控件无关,但我认为它与 HttpContext 对象挂钩。

只有当我使用 DevExpress 的 TabControl 时才会出现此错误,所有其他控件都可以正常工作。

查看附件图片,更详细地描述错误消息。

【问题讨论】:

    标签: c# asp.net dependency-injection unity-container


    【解决方案1】:

    按照惯例,如果没有提供其他配置,Unity 更喜欢具有最长参数列表的构造函数。有两个具有相同长度的参数列表的构造函数会产生歧义,因此 Unity 会引发异常。这就是它无法解析您正在使用的控件的原因。

    您可以明确告诉 Unity 首选哪个构造函数:

    container.RegisterType<IService, Service>(new InjectionConstructor(typeof(IServiceDependency)));
    

    【讨论】:

    • +1。虽然我了解为什么 Unity 会寻找具有最大参数数量的构造函数,但这是我之前没有想到的,而且似乎不是很优雅。但话又说回来,我们依靠 DI 来实现 IoC,所以不能抱怨。
    【解决方案2】:

    您可以在想要的构造器上使用 [InjectionConstructor] 属性

    【讨论】:

      【解决方案3】:

      我在将它添加到我的 unityconifg 中时遇到了同样的问题

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

      使用尼伯兰特

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-02-28
        • 2020-10-22
        • 1970-01-01
        • 2022-07-26
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多