ServiceCollectionDescriptorExtensions.TryAddSingleton Method

Definition

Overloads

TryAddSingleton(IServiceCollection, Type, Type)

Adds the specified service as a Singleton service with the implementationType implementation to the collection if the service type hasn't already been registered.

TryAddSingleton(IServiceCollection, Type, Func<IServiceProvider,Object>)

Adds the specified service as a Singleton service using the factory specified in implementationFactory to the collection if the service type hasn't already been registered.

TryAddSingleton(IServiceCollection, Type)

Adds the specified service as a Singleton service to the collection if the service type hasn't already been registered.

TryAddSingleton<TService,TImplementation>(IServiceCollection)

Adds the specified TService as a Singleton service implementation type specified in TImplementation to the collection if the service type hasn't already been registered.

TryAddSingleton<TService>(IServiceCollection)

Adds the specified TService as a Singleton service to the collection if the service type hasn't already been registered.

TryAddSingleton<TService>(IServiceCollection, Func<IServiceProvider,TService>)

Adds the specified TService as a Singleton service using the factory specified in implementationFactory to the services if the service type hasn't already been registered.

TryAddSingleton<TService>(IServiceCollection, TService)

Adds the specified TService as a Singleton service with an instance specified in instance to the collection if the service type hasn't already been registered.

TryAddSingleton(IServiceCollection, Type, Type)

Source:
ServiceCollectionDescriptorExtensions.cs
Source:
ServiceCollectionDescriptorExtensions.cs
Source:
ServiceCollectionDescriptorExtensions.cs

Adds the specified service as a Singleton service with the implementationType implementation to the collection if the service type hasn't already been registered.

C#
public static void TryAddSingleton(this Microsoft.Extensions.DependencyInjection.IServiceCollection collection, Type service, Type implementationType);

Parameters

service
Type

The type of the service to register.

implementationType
Type

The implementation type of the service.

Applies to

.NET 9 (package-provided) and other versions
Product Versions
.NET 8 (package-provided), 9 (package-provided)
.NET Framework 4.7 (package-provided), 4.7.1 (package-provided), 4.7.2 (package-provided), 4.8 (package-provided)
.NET Standard 2.0 (package-provided), 2.1 (package-provided)

TryAddSingleton(IServiceCollection, Type, Func<IServiceProvider,Object>)

Source:
ServiceCollectionDescriptorExtensions.cs
Source:
ServiceCollectionDescriptorExtensions.cs
Source:
ServiceCollectionDescriptorExtensions.cs

Adds the specified service as a Singleton service using the factory specified in implementationFactory to the collection if the service type hasn't already been registered.

C#
public static void TryAddSingleton(this Microsoft.Extensions.DependencyInjection.IServiceCollection collection, Type service, Func<IServiceProvider,object> implementationFactory);

Parameters

service
Type

The type of the service to register.

implementationFactory
Func<IServiceProvider,Object>

The factory that creates the service.

Applies to

.NET 9 (package-provided) and other versions
Product Versions
.NET 8 (package-provided), 9 (package-provided)
.NET Framework 4.7 (package-provided), 4.7.1 (package-provided), 4.7.2 (package-provided), 4.8 (package-provided)
.NET Standard 2.0 (package-provided), 2.1 (package-provided)

TryAddSingleton(IServiceCollection, Type)

Source:
ServiceCollectionDescriptorExtensions.cs
Source:
ServiceCollectionDescriptorExtensions.cs
Source:
ServiceCollectionDescriptorExtensions.cs

Adds the specified service as a Singleton service to the collection if the service type hasn't already been registered.

C#
public static void TryAddSingleton(this Microsoft.Extensions.DependencyInjection.IServiceCollection collection, Type service);

Parameters

service
Type

The type of the service to register.

Applies to

.NET 9 (package-provided) and other versions
Product Versions
.NET 8 (package-provided), 9 (package-provided)
.NET Framework 4.7 (package-provided), 4.7.1 (package-provided), 4.7.2 (package-provided), 4.8 (package-provided)
.NET Standard 2.0 (package-provided), 2.1 (package-provided)

TryAddSingleton<TService,TImplementation>(IServiceCollection)

Source:
ServiceCollectionDescriptorExtensions.cs
Source:
ServiceCollectionDescriptorExtensions.cs
Source:
ServiceCollectionDescriptorExtensions.cs

Adds the specified TService as a Singleton service implementation type specified in TImplementation to the collection if the service type hasn't already been registered.

C#
public static void TryAddSingleton<TService,TImplementation>(this Microsoft.Extensions.DependencyInjection.IServiceCollection collection) where TService : class where TImplementation : class, TService;

Type Parameters

TService

The type of the service to add.

TImplementation

The type of the implementation to use.

Parameters

Applies to

.NET 9 (package-provided) and other versions
Product Versions
.NET 8 (package-provided), 9 (package-provided)
.NET Framework 4.7 (package-provided), 4.7.1 (package-provided), 4.7.2 (package-provided), 4.8 (package-provided)
.NET Standard 2.0 (package-provided), 2.1 (package-provided)

TryAddSingleton<TService>(IServiceCollection)

Source:
ServiceCollectionDescriptorExtensions.cs
Source:
ServiceCollectionDescriptorExtensions.cs
Source:
ServiceCollectionDescriptorExtensions.cs

Adds the specified TService as a Singleton service to the collection if the service type hasn't already been registered.

C#
public static void TryAddSingleton<TService>(this Microsoft.Extensions.DependencyInjection.IServiceCollection collection) where TService : class;

Type Parameters

TService

The type of the service to add.

Parameters

Applies to

.NET 9 (package-provided) and other versions
Product Versions
.NET 8 (package-provided), 9 (package-provided)
.NET Framework 4.7 (package-provided), 4.7.1 (package-provided), 4.7.2 (package-provided), 4.8 (package-provided)
.NET Standard 2.0 (package-provided), 2.1 (package-provided)

TryAddSingleton<TService>(IServiceCollection, Func<IServiceProvider,TService>)

Source:
ServiceCollectionDescriptorExtensions.cs
Source:
ServiceCollectionDescriptorExtensions.cs
Source:
ServiceCollectionDescriptorExtensions.cs

Adds the specified TService as a Singleton service using the factory specified in implementationFactory to the services if the service type hasn't already been registered.

C#
public static void TryAddSingleton<TService>(this Microsoft.Extensions.DependencyInjection.IServiceCollection services, Func<IServiceProvider,TService> implementationFactory) where TService : class;

Type Parameters

TService

The type of the service to add.

Parameters

implementationFactory
Func<IServiceProvider,TService>

The factory that creates the service.

Applies to

.NET 9 (package-provided) and other versions
Product Versions
.NET 8 (package-provided), 9 (package-provided)
.NET Framework 4.7 (package-provided), 4.7.1 (package-provided), 4.7.2 (package-provided), 4.8 (package-provided)
.NET Standard 2.0 (package-provided), 2.1 (package-provided)

TryAddSingleton<TService>(IServiceCollection, TService)

Source:
ServiceCollectionDescriptorExtensions.cs
Source:
ServiceCollectionDescriptorExtensions.cs
Source:
ServiceCollectionDescriptorExtensions.cs

Adds the specified TService as a Singleton service with an instance specified in instance to the collection if the service type hasn't already been registered.

C#
public static void TryAddSingleton<TService>(this Microsoft.Extensions.DependencyInjection.IServiceCollection collection, TService instance) where TService : class;

Type Parameters

TService

The type of the service to add.

Parameters

instance
TService

The instance of the service to add.

Applies to

.NET 9 (package-provided) and other versions
Product Versions
.NET 8 (package-provided), 9 (package-provided)
.NET Framework 4.7 (package-provided), 4.7.1 (package-provided), 4.7.2 (package-provided), 4.8 (package-provided)
.NET Standard 2.0 (package-provided), 2.1 (package-provided)