C# 类型X有多个长度为1的构造函数。无法消除歧义
我在一个ASP.NETWebForms项目中工作,正在使用Unity进行DI。 此项目还使用DevExpress ASP.NET Ajax控件 现在我们遇到了问题,这似乎是发展和团结之间的冲突 这是统一配置C# 类型X有多个长度为1的构造函数。无法消除歧义,c#,asp.net,dependency-injection,unity-container,C#,Asp.net,Dependency Injection,Unity Container,我在一个ASP.NETWebForms项目中工作,正在使用Unity进行DI。 此项目还使用DevExpress ASP.NET Ajax控件 现在我们遇到了问题,这似乎是发展和团结之间的冲突 这是统一配置 [assembly: WebActivator.PostApplicationStartMethod(typeof(Sales.Web.App_Start.UnityWebFormsStart), "PostStart")] namespace Sales.Web.App_Start
[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());
}
}
}
[程序集:WebActivator.PostApplicationStartMethod(typeof(Sales.Web.App\u Start.UnityWebFormsStart),“PostStart”)]
名称空间Sales.Web.App\u开始
{
///
///Unity.WebForms NuGet包的启动类。
///
内部静态类UnityWebFormsStart
{
///
///在应用程序启动时初始化unity容器。
///
///
///不要编辑此方法。请在中执行任何修改
///方法。
///
内部静态void PostStart()
{
IUnityContainer容器=新的UnityContainer();
HttpContext.Current.Application.SetContainer(容器);
注册依赖性(容器);
}
///
///在提供的容器中注册依赖项。
///
///要填充的容器的实例。
专用静态无效注册表依赖项(IUnityContainer容器)
{
//TODO:在此处添加所需的任何依赖项
容器
.RegisterType(新层次结构CallifeTimeManager())
.RegisterType(typeof(IDbProxy),typeof(DbProxy))
.RegisterType(新层次结构CallifeTimeManager())
.RegisterType(新层次结构CallifeTimeManager())
.RegisterType(新层次结构CallifeTimeManager())
.RegisterType(新层次结构CallifetimeManager());
}
}
}
任何这种Unity配置都与DevExpress控件无关,但我认为它连接了HttpContext对象
只有当我使用DevExpress中的TabControl时,这个错误才会出现,所有其他控件都可以正常工作
请参阅更详细地描述错误消息的附加图像
按照惯例,如果没有提供其他配置,Unity更喜欢参数列表最长的构造函数。如果两个构造函数的参数列表长度相等,则会产生歧义,因此Unity会引发异常。这就是为什么它无法解析您正在使用的控件 您可以明确告诉Unity首选哪个构造函数:
container.RegisterType<IService, Service>(new InjectionConstructor(typeof(IServiceDependency)));
container.RegisterType(新注入构造函数(typeof(IServiceDependency));
您可以在想要的构造函数上使用[InjectionConstructor]属性我在将其添加到unityconifg中时遇到同样的问题
public static void RegisterTypes(IUnityContainer container)
{
container.UseApplicationLayer(false);
container.UseApplicationRepository(false);
container.ConfigureMappings();
}
使用Nhiberante+1。虽然我理解为什么Unity会寻找一个具有最大数量参数的构造函数,但这是我以前没有想到的,而且看起来不是很优雅。但话说回来,我们正在借助DI来实现国际奥委会的目标,所以我们不能抱怨。