Skip to content

)} ,System.String)}

IServiceLocator.AddSingleton(TContract) method

Defined in

Type: IServiceLocator Namespace: Splat Assembly: Splat.Core.dll

Applies to

net10.0, net10.0-browserwasm1.0, net10.0-desktop1.0, net9.0, net9.0-browserwasm1.0, net9.0-desktop1.0, net8.0, net8.0-ios17.5, net8.0-maccatalyst17.5, net8.0-macos14.2, net8.0-macos14.5, net8.0-tvos17.2, netstandard2.1, net462, net481, net471

Overloads

  • 1. void AddSingleton<TContract>(TContract instance) where TContract : class
  • 2. void AddSingleton<TContract>(TContract instance, string contract) where TContract : class
  • 3. void AddSingleton<TContract>(Func<TContract> instanceFactory)
  • 4. void AddSingleton<TContract>(Func<TContract> instanceFactory, string contract)

1. Overload

void AddSingleton<TContract>(TContract instance) where TContract : class

Summary: Registers the specified instance as a singleton service for the given contract type.

Type parameters

NameDescription
TContractThe contract type of the service to register. Must be a reference type.

Parameters

NameTypeDescription
instanceTContractThe instance to use for the singleton service. This instance will be returned for all requests of the specified contract type. Cannot be null.

Remarks

Use this method to provide a specific instance that will be shared across all consumers of the contract type. The same instance is returned each time the service is requested.

2. Overload

void AddSingleton<TContract>(TContract instance, string contract) where TContract : class

Summary: Registers the specified instance as a singleton for the given contract type and contract name.

Type parameters

NameDescription
TContractThe contract type to associate with the singleton instance. Must be a reference type.

Parameters

NameTypeDescription
instanceTContractThe instance to register as a singleton. This instance will be returned for all requests matching the specified contract type and contract name.
contractstringThe contract name used to distinguish this registration. Cannot be null or empty.

Remarks

Subsequent requests for the specified contract type and contract name will return the same instance. This method is typically used to provide a pre-constructed or externally managed singleton to the dependency injection container.

3. Overload

void AddSingleton<TContract>(Func<TContract> instanceFactory)

Summary: Registers a singleton service of the specified contract type using the provided factory function.

Type parameters

NameDescription
TContractThe type of the service to register. The type must have a public parameterless constructor.

Parameters

NameTypeDescription
instanceFactoryFuncA function that returns an instance of the service to be registered as a singleton. This factory is called once to create the singleton instance.

Remarks

Subsequent requests for the service will return the same instance created by the factory. This method is typically used to register services that should have a single shared instance for the application's lifetime.

4. Overload

void AddSingleton<TContract>(Func<TContract> instanceFactory, string contract)

Summary: Registers a singleton service of the specified contract type using the provided factory function and associates it with the given contract name.

Type parameters

NameDescription
TContractThe type of the service to register. The type must have a public parameterless constructor.

Parameters

NameTypeDescription
instanceFactoryFuncA function that creates an instance of the service to be registered as a singleton. This factory is called once to create the singleton instance.
contractstringThe name of the contract to associate with the registered singleton service. Cannot be null or empty.

Remarks

Subsequent requests for the specified contract will return the same singleton instance created by the factory. This method is typically used to register services that should have a single shared instance for the application's lifetime.