gpt4 book ai didi

c# - System.Core.dll 中发生类型为 'System.StackOverflowException' 的未处理异常

转载 作者:太空狗 更新时间:2023-10-29 20:46:44 29 4
gpt4 key购买 nike

在我的 Asp.net MVC 项目中

我有一个初始化 unity-container 的 bootsrapper。

我不知道为什么,但是我明白了

An unhandled exception of type 'System.StackOverflowException' occurred in System.Core.dll

我已经仔细检查过,注册只在我的初始化程序中完成。

所有依赖项仅注入(inject)到 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;
}
}
}
}

最佳答案

没有提供代码,我猜这是由于循环依赖。另一个可能的原因是您的一个构造函数中有一个不正确的循环。

举个例子,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)
{
}
}

关于c# - System.Core.dll 中发生类型为 'System.StackOverflowException' 的未处理异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14561242/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com