10

在我的 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;
            }
        }
    }
}
4

2 回答 2

28

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

例如,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)
    {
    }
}
于 2013-01-28T11:48:27.543 回答
4

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

于 2018-04-20T22:12:02.643 回答