This project is read-only.

ISingletonDependency gets instaniate twice if reference in two different modules.. bug or feature?

Topics: Troubleshooting
Nov 30, 2013 at 1:39 PM
I have a service that is implemented using ISingletonDependency (lets avoid the discussion if it really should be a singleton). The service interface is defined in module A, the implementation reside in module B (necessary to get around circular dependencies in this case). The service is then injected into a FilterProvider/ IAuthorizationFilter in module B and also a controller in module C i.e. it is used in B and C and defined in A.

I can see that it gets instantiated twice: once for each time it is injected. Is this normal behaviour or a bug? I am assuming its a bug since the comment on ISingleton is
/// <summary>
/// Base interface for services that are instantiated per shell/tenant.
/// </summary>.  
If its not a bug, what is the behaviour of a ISingletonDependency ? Is it a singleton instantiated per module?
Nov 30, 2013 at 8:14 PM
And the site is not multi-tenant?
Nov 30, 2013 at 11:49 PM
No its not multi-tenant. It is a pertty bare bones install with a couple extra modules I'm working on.

I also just noticed that everytime I enable a module (just enable import-export) that the constructor for the singleton gets called.

Mmmm but I'm running source for 1.7.1 .. I should try 1.7.2 and see if its the same.
Dec 1, 2013 at 4:35 AM
Edited Dec 1, 2013 at 4:38 AM
Are you sure that you actually get a different instance each time? Having ctor called twice does not necessarily mean the same thing (one of the constructed objects may be discarded in the process, like it happens eg. with Lazy<T>). If you still find those two different, please file a bug.
Dec 1, 2013 at 5:44 AM
Installed 1.7.2 with the default recipe. Same thing: the constructor for the ISingleton get called every time a module is enabled.

To move forward I removed references to the service from other modules and moved the interface into the module it is implemented in. Now the ISingletonDepency is defined, implemented and injected only in one module and I am seeing the above behaviour.
Dec 1, 2013 at 11:17 AM
That it is instantiated once after enabling/disabling a module is by design: when you turn features on or off the shell restarts; thus, singletons (that live with the shell) get re-instantiated.
Dec 1, 2013 at 9:44 PM
Ok thats good to know. Thanks for confirming that.

Unfortunately I removed the other case where the singleton was being initialized every time it was injected. I may take the time to setup that scenario again and see what happens.
Dec 3, 2013 at 6:26 AM
Are you sure that you actually get a different instance each time?
I was sure it was getting instantiated twice but your explanation may explain it. There is a "private static boolean initialized" that get set from false to true when the constructor is called. So as you explained, if it initializes an instance then destroy it and creates another, that would explain what I am seeing.