简体   繁体   English

Singleton中的结构图返回多个实例

[英]Structuremap in Singleton returning multiple instances

I have registered 5 derived classes for the same interface using named instances. 我使用命名实例为同一个接口注册了5个派生类。 All these classes are registered as Singleton 所有这些类都注册为Singleton

For<IBaseInterface>().Singleton().Use<DerivedClass1>().Named("Derived1");
For<IBaseInterface>().Singleton().Use<DerivedClass2>().Named("Derived2");
For<IBaseInterface>().Singleton().Use<DerivedClass3>().Named("Derived3");

There is a static class which resolves the instance based on input. 有一个静态类根据输入解析实例。 However I observed that every call to ObjectFactory.GetInstance returns new instances on every request instead of a Singleton. 但是我观察到对ObjectFactory.GetInstance的每次调用都会在每个请求而不是Singleton上返回新的实例。 There are no threads in the application as well. 应用程序中也没有线程。

Any idea on why this is happening? 对于为什么会发生这种情况的任何想法?

Edit: 编辑:

Does a static resolution helper cause any issues? 静态解决方案助手是否会导致任何问题? This is the way I am resolving the instance. 这是我解析实例的方式。 Singleton works properly in a sample application but it doesnt work on my machine. Singleton在示例应用程序中正常工作,但它在我的机器上不起作用。

To add some more details - the project is MVC Web API and I am testing on local IIS. 要添加更多细节 - 该项目是MVC Web API,我正在本地IIS上进行测试。 I am positive there are no user created threads in the application. 我很肯定在应用程序中没有用户创建的线程。

public static class Resolver
{
    public static IBaseInterface GetHelper(string inputParam)
    {
        if inputParam is "Case1"
            return ObjectFactory.GetInstance<IBaseInterface>("Derived1")
        //Similarly for other instances
    }
}

I would be careful that you are using the Dependency Injection container correctly. 我会小心你正确使用Dependency Injection容器。 For instance, the Resolver class that you show in your post, is this acting as simply a type of Factory or Provider? 例如,您在帖子中显示的Resolver类,它只是一种工厂或提供者?

When using Dependency Injection, you want to be sure and follow the RRR pattern: Register, Resolve, and Release. 使用依赖注入时,您需要确保并遵循RRR模式:注册,解析和发布。 Registration should happen in your application's composition root. 注册应该在您的应用程序的组合根目录中进行。 For ASP.Net MVC, it's ususally somewhere within Global.asax , such as in the code-behind's Application_Start method. 对于ASP.Net MVC,它通常位于Global.asax的某个位置,例如在代码隐藏的Application_Start方法中。 This should only occur once per Application Pool startup (for IIS). 这应该只在每个应用程序池启动时发生一次(对于IIS)。

If by chance you are passing the container around (or an object which instantiates a container and performs registration and later resolution)—which you shouldn't do—it's possible that these "different instances" you are seeing are coming from two different containers. 如果您偶然传递容器(或实例化容器并执行注册并稍后解决的对象) - 您不应该这样做 - 您看到的这些“不同实例”可能来自两个不同的容器。 Even if you aren't passing around the container, per se, if you are instantiating your container somewhere such that, after each request, the container is garbage collected and recreated on subsequent requests, you may see a "different instance" of the singleton objects being resolved and instantiated; 即使您没有在容器周围传递,如果您要在某个地方实例化容器,在每次请求之后,容器被垃圾收集并在后续请求中重新创建,您可能会看到单例的“不同实例”正在解析和实例化的对象; again, each coming from a different instance of the container. 再次,每个来自容器的不同实例。 One way you can verify this is to verify that the objects resolved from your container are also coming from the same container instance. 验证这一点的一种方法是验证从容器中解析的对象是否也来自同一个容器实例。

HTH. HTH。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM