Search Unity

Question VContainer does not create an instance of my service

Discussion in 'Scripting' started by CxydaIO, Feb 26, 2024.

  1. CxydaIO

    CxydaIO

    Joined:
    May 12, 2014
    Posts:
    61
    Hey there, I have a very specific VContainer question and maybe someone can help or notch me into the right direction. I recently switched from Zenject to VContainer and can't get it to create an instance of my service. I'm pretty sure i'm missing something obvious here since I followed the docs and example and in my case it simply does not work.

    What I have:
    Code (CSharp):
    1.     public class GameLifetimeScope : LifetimeScope
    2.     {
    3.         protected override void Configure(IContainerBuilder builder)
    4.         {
    5.             builder.Register<FooService>(Lifetime.Singleton);
    6.         }
    7.     }
    8.     public class FooService
    9.     {
    10.         public FooService()
    11.         {
    12.             Debug.Log("FooService created");
    13.         }
    14.     }
    I never see the log message that my FooService is being created (Of course the registering is happening). I'm guessing i'm missing the equivalent of Zenjects
    .NonLazy()
    method, but i didn't find anything like that in the docs or the examples. So can someone tell me why my constructor is never executed ?

    Thanks a lot for your help!
    Cxyda
     
    theNecromuncher likes this.
  2. theNecromuncher

    theNecromuncher

    Joined:
    May 5, 2017
    Posts:
    6
    Experiencing the same issue exactly.
    Unity 2022.3.16f1
     
  3. Nad_B

    Nad_B

    Joined:
    Aug 1, 2021
    Posts:
    712
    Generally, in most DI frameworks, services are not constructed until requested (Lazy construction), so it can generate the correct graph of dependencies when the first service is requested. I don't know if VContainer offers an Eager service construction.

    Question: why do you need your service to be constructed right away after registration?
     
  4. Lekret

    Lekret

    Joined:
    Sep 10, 2020
    Posts:
    351
    It works the same as in Zenject. It should be either NonLazy (not sure if it's there, bad practice anyway), either have IStartable/ITickable interface, or should be requested as dependency somewhere.
    DI whole point is creation on demand so dependencies are constructed as they are needed and in a proper order.
     
    Nad_B likes this.
  5. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    21,123
    You should be able to do this to create an instance of it instantly.
    Code (csharp):
    1. var foo = builder.Resolve<FooService>();
     
    CodeRonnie likes this.
  6. CxydaIO

    CxydaIO

    Joined:
    May 12, 2014
    Posts:
    61
    Hey guys, thanks for your answers!

    Interesting ... I'm on Unity 2022.3.14f1

    It does need to perform some tasks right away, but even if it wouldn't I can't get it running :(

    hm, no.
    builder
    or better say
    IContainerBuilder
    interface does not have a Resolve method. I'm on the latest VContainer version 1.15.1. To be able to resolve my dependency I need a
    IObjectResolver
    . The
    LifetimeScope
    has a Container property but in my case it is null for some reason. Either it is not initialized yet in the
    Configure
    method or something is horribly wrong on my end :(