在我的Asp.net MVC项目中
我有一个初始化统一容器的启动器。
我不知道为什么,但我得到了
System.Core.dll中出现未处理的“System.StackOverflowException”类型异常
我已经加倍检查,只在我的初始化程序中完成注册。
所有依赖关系仅在ctors中注入。
导致这种情况的原因是什么?
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
Initializer.Initialize();
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
BundleConfig.RegisterBundles(BundleTable.Bundles);
public static class Initializer
{
private static bool isInitialize;
private static readonly object LockObj = new object();
private static IUnityContainer defaultContainer = new UnityContainer();
static Initializer()
{
Initialize();
}
public static void Initialize()
{
if (isInitialize)
return;
lock (LockObj)
{
IUnityContainer container = defaultContainer;
//registering Unity for MVC
DependencyResolver.SetResolver(new UnityDependencyResolver(container));
//registering Unity for web API
// GlobalConfiguration.Configuration.DependencyResolver = new Unity.WebApi.UnityDependencyResolver(container);
#region managers
container.RegisterType<ISettingsManager, SettingsManager>();
container.RegisterType<IMamDataManager, MamDataManager>();
container.RegisterType<IAppsDataManager, AppsDataManager>();
#endregion
#region Dals
container.RegisterType<IDal<ClientService.DAL.EntityFramework.App>, AppsDal>();
#endregion Dals
#region cache
container.RegisterType<ICache<string, ClientService.DAL.EntityFramework.Group>, GroupsCache>(new ContainerControlledLifetimeManager());
container.RegisterType<ICache<string, ClientService.DAL.EntityFramework.App>, AppsCache>(new ContainerControlledLifetimeManager());
container.RegisterType<ICache<string, SettingsServiceData>, SettingsServiceDataCache>(new ContainerControlledLifetimeManager());
#endregion cache
#region Pollers
container.RegisterType<IPoller<ClientService.DAL.EntityFramework.Group>, GroupsPoller>(new ContainerControlledLifetimeManager());
container.RegisterType<IPoller<ClientService.DAL.EntityFramework.App>, AppsPoller>(new ContainerControlledLifetimeManager());
container.RegisterType<IPoller<SettingsServiceData>, SettingsPoller>(new ContainerControlledLifetimeManager());
#endregion Pollers
container.RegisterType<IDefaultConfigurationGroupSingleton, DefaultConfigurationGroupSingleton>(new ContainerControlledLifetimeManager());
container.RegisterType<IApplicationLogger, Log4NetLogger>();
if (!isInitialize)
{
isInitialize = true;
}
}
}
}
答案 0 :(得分:21)
不提供代码,我想这是由于循环依赖。 另一个可能的原因是你的一个构造函数中有一个不正确的循环。
例如,A类需要解析B的实例; B类需要解析C类的实例,C类需要解析A的实例。这导致无限循环:
public class A
{
public A(B b)
{
}
}
public class B
{
public B(C c)
{
}
}
public class C
{
public C(A a)
{
}
}
答案 1 :(得分:0)
正如Rafael所提到的,这通常是由循环依赖引起的,但是如果你需要这些依赖,你可以通过手动解析其中的一些来修复它。
例如:
// Register the UnityContainer with itself
container.RegisterInstance<IUnityContainer>(container);
public class A
{
public A(B b) {}
}
public class B
{
public B(C c) {}
}
public class C
{
private readonly IUnityContainer _container;
private A _a => _container.Resolve<A>();
public C(IUnityContainer container) {
_container = container;
}
}
这意味着可以在不需要了解A的情况下构建C,直到使用它为止:)