Structuremap在辛格尔顿返回多个实例

Structuremap在辛格尔顿返回多个实例

本文介绍了Structuremap在辛格尔顿返回多个实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经注册了5派生类使用命名实例相同的接口。所有这些类被注册为单身

 对于< IBaseInterface>()单身()使用< DerivedClass1>()命名(Derived1)。
对于< IBaseInterface>()单身()使用< DerivedClass2>()命名(Derived2的)。
对于< IBaseInterface>()单身()使用< DerivedClass3>()命名(Derived3)。
 

有它解决了基于输入的实例的静态类。不过,我观察到,每次调用ObjectFactory.GetInstance返回每个请求,而不是一个Singleton实例的。还有在应用程序没有线程为好。

为什么这个任何想法是怎么回事?

编辑:

做了静态分辨率帮手造成任何问题?这是我解决了实例的方式。辛格尔顿正常工作的一个示例应用程序,但它不工作,我的机器上。

要添加更多的细节 - 该项目是MVC的Web API和我测试的本地IIS。我肯定没有任何用户创建的线程的应用程序。

 公共静态类解析器
{
    公共静态IBaseInterface调用getHelper(字符串inputParam)
    {
        如果inputParam是案例1
            返回ObjectFactory.GetInstance&其中; IBaseInterface&GT(Derived1)
        //同样,对于其他情况
    }
}
 

解决方案

我想,你是正确使用依赖注入容器要小心。例如,您在您的文章显示类,这是作为一个简单的类型工厂或供应商的?

在使用依赖注入,你要确定,并按照存款准备金率的模式:注册,解析和发布。登记应发生在你的应用程序的构成根。对于ASP.Net MVC,这是通常游戏的地方在的Global.asax ,比如在code-后面的的Application_Start 方法。这应该只出现一次,每个应用程序池启动(对于IIS)。

如果碰巧你是路过的容器周围(或一个对象,它实例化一个容器,并进行登记,后来分辨率)—你不应该做的和mdash;这是可能的,这些不同的情况下,你看到的是来两个不同的容器。即使你没有绕过容器本身,如果你实例化你的容器的地方,这样,每个请求后,容器进行垃圾收集并重新创建后续请求,您可能会看到单一个不同的实例对象被解决,实例化;再次,每个从容器的不同实例来了。可以验证的一种方法是,以验证从容器解析的对象也从相同的容器实例的到来。

HTH。

I have registered 5 derived classes for the same interface using named instances. All these classes are registered as 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. 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.

To add some more details - the project is MVC Web API and I am testing on local 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. For instance, the Resolver class that you show in your post, is this acting as simply a type of Factory or Provider?

When using Dependency Injection, you want to be sure and follow the RRR pattern: Register, Resolve, and Release. 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. This should only occur once per Application Pool startup (for 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.

这篇关于Structuremap在辛格尔顿返回多个实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 20:11