Re: Possible concurrency issue with ObjectFactory.GetInstance?

388 views
Skip to first unread message

Filippo Pensalfini

unread,
Oct 17, 2012, 3:27:15 AM10/17/12
to structure...@googlegroups.com
Small correction: in the exception message INodeConfigurationService should read IConfigurationService.

Filippo Pensalfini

unread,
Oct 18, 2012, 3:53:12 AM10/18/12
to structure...@googlegroups.com
I managed to debug to the point where the error generates in StructureMap: it seems that in the StructureMap.Pipeline.Profile.GetDefault method the Dictionary of _instances has not yet been filled completely when the check on ContainsKey takes place. Locking on modifications of _instances makes this behavior more evident.

Does anybody have a clue why this might be happening? It looks like a race condition but I lack deeper knowledge of StructureMap to make a call, maybe there is something I misunderstand and/or I misconfigured. I will keep digging deeper to find the root cause in the meanwhile.

Filippo Pensalfini

unread,
Oct 18, 2012, 5:27:44 AM10/18/12
to structure...@googlegroups.com
I think I got it: there was a call to ObjectFactory.Inject for every request, which as far as I understand completely reconfigures the _instances dictionary in the current profile. The comment of Inject states the following:

        /// Injects the given object into a Container as the default for the designated
        /// PLUGINTYPE.  Mostly used for temporarily setting up return values of the Container
        /// to introduce mocks or stubs during automated testing scenarios

I suppose it was mainly used for testing and not in productive multithreaded environments. I will attempt to work around the Inject method for now, though I am asking myself if this is by design or if it should be reported as a bug.

ssu...@gmail.com

unread,
Nov 26, 2013, 8:41:29 AM11/26/13
to structure...@googlegroups.com
Did you find the work arounf for this? I am also facing similar issue in a windows service. But not able to simulate the error locally at all.

Filippo Pensalfini

unread,
Dec 1, 2013, 2:58:38 PM12/1/13
to structure...@googlegroups.com
I don't have the code at hand at the moment, but I think I solved it by introducing a level of indirection (say, a factory) between the instance that I was trying to inject and the "client code" that used that instance. For example, if I wanted to inject an instance of IApple, I have an IAppleFactory with a method that returns the appropriate instance of IApple.

HTH
Reply all
Reply to author
Forward
0 new messages