ServiceCollectionDescriptorExtensions.TryAddSingleton 方法

定義

多載

TryAddSingleton(IServiceCollection, Type)

如果尚未註冊服務類型,則將指定的 service 作為 Singleton 服務新增至 collectionAdds the specified service as a Singleton service to the collection if the service type hasn't already been registered.

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

如果尚未註冊服務類型,則使用 service 中指定的 Factory,將指定的 Singleton 作為 implementationFactory 服務新增至 collectionAdds 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, Type)

如果尚未註冊服務類型,則將所指定 service 作為具有 SingletonimplementationType 服務新增至 collectionAdds the specified service as a Singleton service with the implementationType implementation to the collection if the service type hasn't already been registered.

TryAddSingleton<TService,TImplementation>(IServiceCollection)

如果尚未註冊服務類型,則將指定 TService 作為 Singleton 中指定的 TImplementation 服務實作類型新增至 collectionAdds 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)

如果尚未註冊服務類型,則將指定的 TService 作為 Singleton 服務新增至 collectionAdds 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>)

如果尚未註冊服務類型,則使用 TService 中指定的 Factory,將指定的 Singleton 作為 implementationFactory 服務新增至 servicesAdds 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)

如果尚未註冊服務類型,則使用 TService 中指定的執行個體,將指定的 Singleton 作為 instance 服務新增至 collectionAdds 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)

如果尚未註冊服務類型,則將指定的 service 作為 Singleton 服務新增至 collectionAdds the specified service as a Singleton service to the collection if the service type hasn't already been registered.

public:
[System::Runtime::CompilerServices::Extension]
 static void TryAddSingleton(Microsoft::Extensions::DependencyInjection::IServiceCollection ^ collection, Type ^ service);
public static void TryAddSingleton (this Microsoft.Extensions.DependencyInjection.IServiceCollection collection, Type service);
static member TryAddSingleton : Microsoft.Extensions.DependencyInjection.IServiceCollection * Type -> unit
<Extension()>
Public Sub TryAddSingleton (collection As IServiceCollection, service As Type)

參數

service
Type

要註冊的服務類型。The type of the service to register.

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

如果尚未註冊服務類型,則使用 service 中指定的 Factory,將指定的 Singleton 作為 implementationFactory 服務新增至 collectionAdds 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.

public:
[System::Runtime::CompilerServices::Extension]
 static void TryAddSingleton(Microsoft::Extensions::DependencyInjection::IServiceCollection ^ collection, Type ^ service, Func<IServiceProvider ^, System::Object ^> ^ implementationFactory);
public static void TryAddSingleton (this Microsoft.Extensions.DependencyInjection.IServiceCollection collection, Type service, Func<IServiceProvider,object> implementationFactory);
static member TryAddSingleton : Microsoft.Extensions.DependencyInjection.IServiceCollection * Type * Func<IServiceProvider, obj> -> unit
<Extension()>
Public Sub TryAddSingleton (collection As IServiceCollection, service As Type, implementationFactory As Func(Of IServiceProvider, Object))

參數

service
Type

要註冊的服務類型。The type of the service to register.

implementationFactory
Func<IServiceProvider,Object>

建立服務的 Factory。The factory that creates the service.

TryAddSingleton(IServiceCollection, Type, Type)

如果尚未註冊服務類型,則將所指定 service 作為具有 SingletonimplementationType 服務新增至 collectionAdds the specified service as a Singleton service with the implementationType implementation to the collection if the service type hasn't already been registered.

public:
[System::Runtime::CompilerServices::Extension]
 static void TryAddSingleton(Microsoft::Extensions::DependencyInjection::IServiceCollection ^ collection, Type ^ service, Type ^ implementationType);
public static void TryAddSingleton (this Microsoft.Extensions.DependencyInjection.IServiceCollection collection, Type service, Type implementationType);
static member TryAddSingleton : Microsoft.Extensions.DependencyInjection.IServiceCollection * Type * Type -> unit
<Extension()>
Public Sub TryAddSingleton (collection As IServiceCollection, service As Type, implementationType As Type)

參數

service
Type

要註冊的服務類型。The type of the service to register.

implementationType
Type

服務的實作類型。The implementation type of the service.

TryAddSingleton<TService,TImplementation>(IServiceCollection)

如果尚未註冊服務類型,則將指定 TService 作為 Singleton 中指定的 TImplementation 服務實作類型新增至 collectionAdds the specified TService as a Singleton service implementation type specified in TImplementation to the collection if the service type hasn't already been registered.

public:
generic <typename TService, typename TImplementation>
 where TService : class where TImplementation : class, TService[System::Runtime::CompilerServices::Extension]
 static void TryAddSingleton(Microsoft::Extensions::DependencyInjection::IServiceCollection ^ collection);
public static void TryAddSingleton<TService,TImplementation> (this Microsoft.Extensions.DependencyInjection.IServiceCollection collection) where TService : class where TImplementation : class, TService;
static member TryAddSingleton : Microsoft.Extensions.DependencyInjection.IServiceCollection -> unit (requires 'Service : null)
<Extension()>
Public Sub TryAddSingleton(Of TService As Class, TImplementation As Class) (collection As IServiceCollection)

類型參數

TService

要新增的服務類型。The type of the service to add.

TImplementation

要使用的實作為型別。The type of the implementation to use.

參數

TryAddSingleton<TService>(IServiceCollection)

如果尚未註冊服務類型,則將指定的 TService 作為 Singleton 服務新增至 collectionAdds the specified TService as a Singleton service to the collection if the service type hasn't already been registered.

public:
generic <typename TService>
 where TService : class[System::Runtime::CompilerServices::Extension]
 static void TryAddSingleton(Microsoft::Extensions::DependencyInjection::IServiceCollection ^ collection);
public static void TryAddSingleton<TService> (this Microsoft.Extensions.DependencyInjection.IServiceCollection collection) where TService : class;
static member TryAddSingleton : Microsoft.Extensions.DependencyInjection.IServiceCollection -> unit (requires 'Service : null)
<Extension()>
Public Sub TryAddSingleton(Of TService As Class) (collection As IServiceCollection)

類型參數

TService

要新增的服務類型。The type of the service to add.

參數

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

如果尚未註冊服務類型,則使用 TService 中指定的 Factory,將指定的 Singleton 作為 implementationFactory 服務新增至 servicesAdds 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.

public:
generic <typename TService>
 where TService : class[System::Runtime::CompilerServices::Extension]
 static void TryAddSingleton(Microsoft::Extensions::DependencyInjection::IServiceCollection ^ services, Func<IServiceProvider ^, TService> ^ implementationFactory);
public static void TryAddSingleton<TService> (this Microsoft.Extensions.DependencyInjection.IServiceCollection services, Func<IServiceProvider,TService> implementationFactory) where TService : class;
static member TryAddSingleton : Microsoft.Extensions.DependencyInjection.IServiceCollection * Func<IServiceProvider, 'Service (requires 'Service : null)> -> unit (requires 'Service : null)
<Extension()>
Public Sub TryAddSingleton(Of TService As Class) (services As IServiceCollection, implementationFactory As Func(Of IServiceProvider, TService))

類型參數

TService

要新增的服務類型。The type of the service to add.

參數

implementationFactory
Func<IServiceProvider,TService>

建立服務的 Factory。The factory that creates the service.

TryAddSingleton<TService>(IServiceCollection, TService)

如果尚未註冊服務類型,則使用 TService 中指定的執行個體,將指定的 Singleton 作為 instance 服務新增至 collectionAdds 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.

public:
generic <typename TService>
 where TService : class[System::Runtime::CompilerServices::Extension]
 static void TryAddSingleton(Microsoft::Extensions::DependencyInjection::IServiceCollection ^ collection, TService instance);
public static void TryAddSingleton<TService> (this Microsoft.Extensions.DependencyInjection.IServiceCollection collection, TService instance) where TService : class;
static member TryAddSingleton : Microsoft.Extensions.DependencyInjection.IServiceCollection * 'Service -> unit (requires 'Service : null)
<Extension()>
Public Sub TryAddSingleton(Of TService As Class) (collection As IServiceCollection, instance As TService)

類型參數

TService

要新增的服務類型。The type of the service to add.

參數

instance
TService

要新增的服務執行個體。The instance of the service to add.

適用於