C# 手动注册UnityServiceLocator时Unity不遵守CallifeTimeManager

C# 手动注册UnityServiceLocator时Unity不遵守CallifeTimeManager,c#,unity-container,C#,Unity Container,在使用层次结构CallifeTimeManager注册和解析IServiceLocator时,我看到Unity中出现了一些意外行为 我看到的情况表明,Unity在注册时将具体类型UnityServiceLocator视为特例。它将该类型视为静态单例,即使它不应该这样做。这种行为似乎是UnityServiceLocator所特有的 背景:我正在代码中的一些地方逐步淘汰“静态”ServiceLocator。第一步是在Unity中注册IServiceLocator,然后将其作为依赖项注入到使用它的类中

在使用
层次结构CallifeTimeManager
注册和解析
IServiceLocator
时,我看到Unity中出现了一些意外行为

我看到的情况表明,Unity在注册时将具体类型
UnityServiceLocator
视为特例。它将该类型视为静态单例,即使它不应该这样做。这种行为似乎是UnityServiceLocator所特有的

背景:我正在代码中的一些地方逐步淘汰“静态”ServiceLocator。第一步是在Unity中注册IServiceLocator,然后将其作为依赖项注入到使用它的类中

在下面的代码中,我注册了一个注入工厂来创建
UnityServiceLocator
的新实例。我还使用
HierarchyCallifeTimeManager
对其进行范围分析,为每个子容器提供一个实例:

private static void Main(string[] args)
{
    var container = new UnityContainer();
    container.RegisterType<IServiceLocator>(
        new HierarchicalLifetimeManager(),
        new InjectionFactory(
            c =>
                {
                    Console.WriteLine("InjectionFactory invoked with container: " + c.GetHashCode());
                    return new UnityServiceLocator(c);
                }));

    container.Resolve<IServiceLocator>(); // expect to see console output here
    container.Resolve<IServiceLocator>(); // don't expect to see console output here

    var child = container.CreateChildContainer();
    child.Resolve<IServiceLocator>(); // expect to see console output here
    container.Resolve<IServiceLocator>(); // don't expect to see console output here

    var anotherChildContainer = container.CreateChildContainer();
    anotherChildContainer.Resolve<IServiceLocator>(); // expect to see console output here
    anotherChildContainer.Resolve<IServiceLocator>(); // don't expect to see console output here
}

这开始以我期望的方式运行:

InjectionFactory invoked with container: 20903718 
InjectionFactory invoked with container: 51746094 
InjectionFactory invoked with container: 41215084
除非Unity将
UnityServiceLocator
视为特例,否则我无法理解这种行为。
有人对这种行为有其他解释吗?我是不是遗漏了什么明显的东西,或者Unity是否在内部将UnityServiceLocator视为一个特例,而忽略我指定的生存期策略?

事实证明,
UnityServiceLocator
是一个特例-它在第一次创建时在自己的构造函数中向
ExternallyControlled LifetimeManager
注册


有关更多信息,请参见Randy Levy的评论:

事实证明,
UnityServiceLocator
是一个特例-它在第一次创建时在自己的构造函数中向
外部控制的LifetimeManager
注册


更多信息,请参见Randy Levy的评论:

这不是对您问题的回答,而是一个建议:不要注入ServiceLocator,注入实际的依赖关系。谢谢Alex-事实上,这就是现在正在做的事情,但我想通过一系列安全的小步骤,从静态服务定位器到正确的构造函数注入。第一种方法是注入服务定位器,而不是使用静态方法获取它,因此一次只做一个相对较小的更改。不过,一旦我注意到这种奇怪的行为,我就觉得有必要再深入一点,因为它看起来太奇怪了!UnityServiceLocator未以任何特殊方式处理;您可以检查Unity源以确认。这是一个奇怪的问题。事实证明UnityServiceLocator是以一种特殊的方式处理的——它在自己的构造函数中使用ExternallyControlledLifetimeManager向容器注册自己。请看Randy Levy的评论:这不是对你问题的回答,而是一个建议:不要注入ServiceLocator,而是注入实际的依赖关系。谢谢Alex——事实上,这就是现在正在做的事情,但我想通过一系列小的安全步骤,从静态服务定位器到正确的构造函数注入。第一种方法是注入服务定位器,而不是使用静态方法获取它,因此一次只做一个相对较小的更改。不过,一旦我注意到这种奇怪的行为,我就觉得有必要再深入一点,因为它看起来太奇怪了!UnityServiceLocator未以任何特殊方式处理;您可以检查Unity源以确认。这是一个奇怪的问题。事实证明UnityServiceLocator是以一种特殊的方式处理的——它在自己的构造函数中使用ExternallyControlledLifetimeManager向容器注册自己。请参见Randy Levy的评论:
     return new MyUnityServiceLocator(c);
InjectionFactory invoked with container: 20903718 
InjectionFactory invoked with container: 51746094 
InjectionFactory invoked with container: 41215084