Structuremap in Singleton returning multiple instances

淺唱寂寞╮ 提交于 2019-12-04 06:27:05

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.

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