c# - 类型 X 有多个长度为 1 的构造函数。无法消除歧义

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

我在 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 时才会出现此错误,所有其他控件都可以正常工作。

请参阅更详细地描述错误消息的附加图像。

enter image description here

最佳答案

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

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

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

关于c# - 类型 X 有多个长度为 1 的构造函数。无法消除歧义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28152836/

相关文章:

android - 如何使用 Hilt 将 SharedPreferences 注入(inject)后台服务

c# - Mongodb C# 查找异步。使用linq过滤文档内的列表

c# - C#中的唯一编号生成

c# - OnPost 修改后 Razor Pages 模型值差异

c# - 如何让代码在返回某些东西之前运行一定次数?

asp.net-mvc - Entity Framework "Migrations Configuration"和 autofac

c# - 由于缺少 Azure 存储连接字符串和 SAS 连接 uri,Blob 存储的 secret 初始化失败

c# - 需要 CS1001 网站标识符

c# - 将复选框添加到 gridview 的标题

c# - 使用简单注入(inject)器注册集合