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

非 Y 不嫁゛ 提交于 2019-11-30 14:14:23
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:

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

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

public class C
{
    public C(A a)
    {
    }
}

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!