Open RamiAhmed opened 2 months ago
I think I may have found the issue. It seems to be related to RegisterComponentInHierarchy using Lifetime.Scoped by default. This could be causing the multiple calls each time a new child scope is loaded. I’m not entirely certain if this is the root cause, but it appears likely.
Would using Lifetime.Singleton instead resolve the issue, or is there another recommended approach to prevent these repeated injections in child scopes?
Thanks for looking into this. Your conclusion makes sense, however unfortunately there is no option to change the lifetime to singleton when calling RegisterComponentInHierarchy.
I believe that entry point interfaces might not be ideal for use with MonoBehaviour components, as the pure C# entry points are actually intended as a replacement for MonoBehaviour. Using entry points on MonoBehaviour can lead to issues with lifecycle management, especially when dealing with scope hierarchies, as seen here.
1) As for entry points, we have been using IPostStartable to be sure injection has taken place, however, it seems simply moving that logic to Start() works just fine.
One discovery from that refactor, is that calling AsImplementedInterfaces()
on a registration of a monobehaviour with zero interface implementations, results in an index out of range exception, e.g.
builder.RegisterComponentInHierarchy<ComponentWithNoInterfaces>().AsImplementedInterfaces()
this is due to registrationBuilder.InterfaceTypes
being empty but not null in the following snippet from RegisterComponentInHierarchy
container.Resolve(
registrationBuilder.InterfaceTypes != null
? registrationBuilder.InterfaceTypes[0]
: registrationBuilder.ImplementationType
);
2) The issue also applies to injection, not just entry points. And we can't get around that issue. We have monobehaviours that are intended to be singletons in a parent scope, but they are reinjected in child scopes, due to, as you found, the default (and only possible) registration being scoped. So it makes sense to allow monobehaviours to be registered as singletons.
It would be cool if RegisterComponentInHieararchy
allowed us to specify the lifetime, I think.
I had same issue, and researched a lot of code, finally get to multi-injection into same object, that was so strange. The problem was really in RegisterComponentInHieararchy
, Changed to RegisterComponent
and provided instance with SerializedField
solved the issue. One more thing: in Editor was no issues, but was in Android build
Hi,
Thanks for building and maintaining VContainer - so far we're really happy to use it, finding it a good improvement over Zenject.
However, we have run into a problem that we suspect is a bug in the framework. It seems that
MonoBehaviour
s that implement "entry point interfaces" (e.g.IInitializable
,IStartable
,IPostStartable
, etc.) get called multiple times - once per child scope loaded.[Inject]
decorated methods also appear to suffer from the issue of being called once per child scope loaded.So binding a
MonoBehaviour
that implements an entry point interface will have its method called multiple times, despite its scope never being unloaded or reloaded. You don't even need separate scenes to replicate it, as its only tied to loading more VContainer child scopes it seems. ThusMonoBehaviour
s bound in a "root" / "grand parent" scope will have their entry points called 3 times (one for itself, one for the parent and one for the child).At a glance it seems that this issue is a duplicate of this issue, but since that has been closed and we are still replicating the bug in v1.6.3 it seems that either the issue was not fully resolved, or that it has reappeared. Also that issue appears to have been caused by bindings in the child scope, whereas here the issue seems to stem from bindings in 'parent' scopes.
Here's a minimal test project where I've set up a case to replicate the behaviour: VContainerTestProject.zip
Steps to reproduce (already set up in the above project which can be downloaded and played out of the box):
Install latest version of VContainer from UPM in a new, empty Unity project
Set up three game objects which will have each their own
LifetimeScope
implementation in a scene, and a fourth game object for the behaviours to bind.Implement abstract base behaviour and child implementations, one for each scope, then attach all three behaviours to the
BehaviourTest
game object:public abstract class BaseBehaviourTest : MonoBehaviour, IInitializable, IPostInitializable, IStartable, IPostStartable, IDisposable { private int _injectedCounter; private int _initializeCounter; private int _postInitializeCounter; private int _startCounter; private int _postStartCounter; private int _disposeCounter;
}
public class RootBehaviourTest : BaseBehaviourTest { }
public class ParentBehaviourTest : BaseBehaviourTest { }
public class ChildBehaviourTest : BaseBehaviourTest { }
using VContainer; using VContainer.Unity;
public class RootLifetimeScope : LifetimeScope { protected override void Configure(IContainerBuilder builder) { builder.RegisterComponentInHierarchy().AsImplementedInterfaces();
}
}
public class ParentLifetimeScope : LifetimeScope { protected override void Configure(IContainerBuilder builder) { builder.RegisterComponentInHierarchy().AsImplementedInterfaces();
}
}
public class ChildLifetimeScope : LifetimeScope { protected override void Configure(IContainerBuilder builder) { builder.RegisterComponentInHierarchy().AsImplementedInterfaces();
}
}