Troubleshooting
This page lists common errors you might encounter when using VContainer and how to resolve them.
Common Exceptions
VContainerException: No such registration of type: X
Cause:
You are trying to Resolve<X>() (or inject X into a constructor), but X has not been registered in the current LifetimeScope or any of its parents.
Solution:
- Check your
Configuremethod inLifetimeScope. Ensurebuilder.Register<X>(...)exists. - If
Xis an interface, ensure you registered it as that interface:builder.Register<XImpl>(...).As<X>(). - If you expect it to be inherited from a parent scope, verify the parent-child connection in the Inspector or code.
VContainerException: Circular dependency detected
Cause: Class A depends on Class B, and Class B depends on Class A. VContainer cannot create either because they require each other to exist first.
class A { public A(B b) {} }
class B { public B(A a) {} }
Solution:
- Refactor (Recommended): This usually indicates a design flaw. Extract the shared logic into a third class
C, and have bothAandBdepend onC. - Lazy Injection: If refactoring is impossible, use
Lazy<T>orFunc<T>to delay resolution.class A { public A(Lazy<B> b) {} }
VContainerException: Implementation type must be a subclass of ...
Cause: You tried to register a type as an interface it doesn't implement.
// Error if ServiceA does not implement IService
builder.Register<ServiceA>(Lifetime.Singleton).As<IService>();
Solution:
Ensure ServiceA implements IService.
Unity Specific Issues
MonoBehaviour fields are null after Injection
Cause:
Awake() is called before VContainer injects dependencies into MonoBehaviours in the scene.
Solution:
- Move your initialization logic to
Start(). VContainer injects afterAwakebut beforeStart. - Or, implement
IInitializableif you are usingRegisterEntryPoint.
"Multiple constructors found"
Cause: The class you are trying to register has multiple constructors, and VContainer doesn't know which one to use.
Solution:
Add the [Inject] attribute to the constructor you want VContainer to use.
class MyService
{
public MyService() {} // Don't use this
[Inject]
public MyService(Dependency d) {} // Use this!
}
Performance Issues
"GC Allocation is high during Resolve"
Cause:
You might be using Lifetime.Transient for heavy objects or frequently resolving objects inside a tight loop (like Update).
Solution:
- Use
Lifetime.SingletonorLifetime.Scopedwhere possible. - Avoid calling
Resolve()insideUpdate(). Resolve once in the constructor/Start and cache the reference.