Table of Contents

Class ServiceRegistry

Namespace
Stride.Core
Assembly
Stride.Core.dll

Provides a base implementation for managing services within an application. Implements the IServiceRegistry interface.

public class ServiceRegistry : IServiceRegistry
Inheritance
object
ServiceRegistry
Implements
Extension Methods

Remarks

This class uses a dictionary to store services by their types. It is thread-safe.

Fields

ServiceRegistryKey

The key used to identify the ServiceRegistry instance.

public static readonly PropertyKey<IServiceRegistry> ServiceRegistryKey

Field Value

PropertyKey<IServiceRegistry>

Methods

AddService(Type, object)

Adds a service under a type known at run time.

public void AddService(Type serviceType, object service)

Parameters

serviceType Type
service object

Exceptions

ArgumentException

The service is not of serviceType, or that type is already registered.

AddService<T>(T)

Adds a service to this ServiceRegistry.

public void AddService<T>(T service) where T : class

Parameters

service T

The service to add.

Type Parameters

T

The type of service to add.

Remarks

This implementation triggers the ServiceAdded event after a service is successfully added.

Exceptions

ArgumentNullException

Thrown when the provided service is null.

ArgumentException

Thrown when a service of the same type is already registered.

GetOrCreate<T>()

Gets the service object of the specified type, create one if it didn't exist before.

public T GetOrCreate<T>() where T : class, IService

Returns

T

Type Parameters

T

The type of the service to retrieve.

GetService<T>()

Gets the service object of the specified type.

public T? GetService<T>() where T : class

Returns

T

A service of the requested type, or [null] if not found.

Type Parameters

T

The type of the service to retrieve.

Remarks

The generic type provided must match the generic type of your initial call to AddService<T>(T)

RemoveService<T>()

Removes the object providing a specified service.

public void RemoveService<T>() where T : class

Type Parameters

T

The type of the service to remove.

Remarks

The generic type provided must match the generic type of your initial call to AddService<T>(T)

RemoveService<T>(T)

Removes the following object from services if it was registered as one.

public bool RemoveService<T>(T serviceObject) where T : class

Parameters

serviceObject T

Returns

bool

True if the argument was a service, false otherwise

Type Parameters

T

The type of the service to remove.

Remarks

The generic type provided must match the generic type of your initial call to AddService<T>(T)

Events

ServiceAdded

Occurs when a new service is added to the registry.

public event EventHandler<ServiceEventArgs>? ServiceAdded

Event Type

EventHandler<ServiceEventArgs>

ServiceRemoved

Occurs when a service is removed from the registry.

public event EventHandler<ServiceEventArgs>? ServiceRemoved

Event Type

EventHandler<ServiceEventArgs>