C# System.Core.dll 中发生类型为“System.StackOverflowException”的未处理异常

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14561242/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 12:15:44  来源:igfitidea点击:

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

c#asp.net-mvcunity-container

提问by Elad Benda

In my Asp.net MVC project

在我的 Asp.net MVC 项目中

I have a bootsrapper that initialize a unity-container.

我有一个初始化统一容器的引导程序。

I don't know why, but I get

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

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

System.Core.dll 中发生类型为“System.StackOverflowException”的未处理异常

I have doubled checked and registration is done only in my initializer.

我已经加倍检查并且注册仅在我的初始值设定项中完成。

All dependencies are injected in the ctors only.

所有依赖项仅注入到构造函数中。

What could have caused this?

这可能是什么原因造成的?

    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();
        WebApiConfig.Register(GlobalConfiguration.Configuration);
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);

        Initializer.Initialize();
        BundleConfig.RegisterBundles(BundleTable.Bundles);
    }

It fails after 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;
            }
        }
    }
}

采纳答案by daryal

Without providing code, I guess this is due to a circular dependency. Another possible reason is that you have an improper loop in one of your constructors.

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

As an example, A class requires an instance of B to be resolved; B class requires an instance of C class to be resolved and C class needs an instance of A to be resolved. This results in an infinite loop:

例如,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)
    {
    }
}

回答by Naravia

As Rafael mentioned, this is usually caused by circular dependencies, however if you need these dependencies you can fix it by manually resolving some of them.

正如 Rafael 提到的,这通常是由循环依赖引起的,但是如果你需要这些依赖,你可以通过手动解析其中的一些来修复它。

For example:

例如:

// 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;
    }
}

This means that C can be constructed without needing to know about A until it's time to use it :)

这意味着可以构造 C 而不需要知道 A,直到需要使用它:)