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

2

10

In my Asp.net MVC project

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

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);

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;
            }
        }
    }
}
Ansilme answered 28/1, 2013 at 11:40 Comment(5)
please provide a piece of code where it happens..Stertorous
Do you have no more information about the exception? No stack trace at all? (I realize stack overflow is a bit "special" but I would have expected at least some information.)Gant
This is often due to some endless loop going on, circular references etc. Can you post the stacktrace of the exception?Centroclinal
Can you post the code of the Initializer.Initialize() method? And the stacktrace of the exception?Centroclinal
@EladBenda What about the stacktrace?Centroclinal
M
28

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:

public class A
{
    public A(B b)
    {
    }
}

public class B
{
    public B(C c)
    {
    }
}

public class C
{
    public C(A a)
    {
    }
}
May answered 28/1, 2013 at 11:48 Comment(3)
how can I fix this? I f I want to inject all in DI?Ansilme
you can not fix without changing the code structure. You need to fix the circular dependencies. For example, removing the A from Constructor of C resolves the circular dependency problem.May
Yes. I by mistake added a dep injection into the same service class and this started happening. Removed the link and issue solved! I wish it gave better error messages - this drove me crazy!Uncinariasis
G
4

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.

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 :)

Gannes answered 20/4, 2018 at 22:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.