Skip to content

Usage.Native CSharp Projects

JuDelCo edited this page May 28, 2021 · 13 revisions

Service Container Lifecycle

In native C# applications, you need to ensure the lifecycle of the Service Container and fire some events (this is all handled by the Unity Service in Unity projects).

Start

At some point in the entry point of the logic of your program, register the services using:

ServiceContainer.RegisterService<ITaskService, TaskService>();
ServiceContainer.RegisterService<ICoroutineService, CoroutineService>();
ServiceContainer.RegisterService<ICacheService, CacheService>();

// Note: You need to implement your custom C# native providers for these services if you need to use them
// ServiceContainer.RegisterService<ILogService, LogConsoleService>();
// ServiceContainer.RegisterService<IInputService, InputUnityService>();
// ServiceContainer.RegisterService<ITimeService, UnityTimeService>();

// Register your custom services

Note: The IEventBusService is core and therefore is registered automatically, you can't unload this service.

Read more on how to use the Service Container here.

Update loop

During the update loop of your application you need to fire events so some Services that depends on them (like the Coroutine and Task services) work as intended:

// Update loop
ServiceContainer.Get<IEventBusService>().Fire(new TimePreUpdateEvent(deltaTime));
ServiceContainer.Get<IEventBusService>().Fire(new TimeUpdateEvent(deltaTime));
ServiceContainer.Get<IEventBusService>().Fire(new TimePostUpdateEvent(deltaTime));

// Fixed update loop
ServiceContainer.Get<IEventBusService>().Fire(new TimePreFixedUpdateEvent(fixedDeltaTime));
ServiceContainer.Get<IEventBusService>().Fire(new TimeFixedUpdateEvent(fixedDeltaTime));
ServiceContainer.Get<IEventBusService>().Fire(new TimePostFixedUpdateEvent(fixedDeltaTime));

// others ... (physics events)

Dispose

Before the program finalizes, you should dispose all the services using:

ServiceContainer.Dispose();
Clone this wiki locally