Nil*_*ers 5 c# asp.net dependency-injection unity-container
我正在使用ASP.NET Webforms项目并使用Unity for 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());
}
}
}
Run Code Online (Sandbox Code Playgroud)
任何这种Unity配置都与DevExpress控件无关,但我认为它会挂钩HttpContext对象.
只有当我使用DevExpress的TabControl时才会出现此错误,所有其他控件都运行良好.
请参阅附加的图像,更详细地描述错误消息.

Ser*_*diy 13
按照惯例,如果没有提供其他配置,Unity会使用最长参数列表的构造函数.具有两个具有相等长度的参数列表的构造函数会产生歧义,因此Unity会抛出异常.这就是为什么它无法解决您正在使用的控件.
您可以明确告诉Unity更喜欢哪个构造函数:
container.RegisterType<IService, Service>(new InjectionConstructor(typeof(IServiceDependency)));
Run Code Online (Sandbox Code Playgroud)