aspnet/DependencyInjection — pull request #101 by KKhurin (2014-08-29 19:23:38+00:00)
Title: Fixing the memory leak in issue #100 by changing lifecycle of ServiceProviderService

ServiceProviderService is a helper responsible for obtaining ServiceProvider instances.
This class makes the way of injecting the correct instance of service provider consistent with the way we inject other instances.
Instead of creating a new instance it returns the instance of the service provider itself.
Since it claims its lifecycle as Transient all the ServiceProviders will skip the lookup and add multiple reference to themselves in their own _disposables collections.
This is what causes a memory leak.

So its Lifecycle property should NOT return LifecycleKind.Transient.
It should also not be LifecycleKind.Singleton because in this case it would mess up the scope.
LifecycleKind.Scoped is the most correct lifecycle for this service as it does not change the lifecycle and the scope of the already created service provider it returns.
However this still leaves us with almost every ServiceProvider._disposables holding a reference to itself. To further clean things up we add a reference check in CaptureDisposables().