Class ServiceCollectionExtensions
Provides extension methods for Microsoft.Extensions.DependencyInjection.IServiceCollection to register services with unique semantics.
Inheritance
Namespace: Umbraco.Extensions
Assembly: Umbraco.Core.dll
Syntax
public static class ServiceCollectionExtensions
Remarks
These methods ensure that only one registration exists for a given service type by removing previous registrations.
Methods
View SourceAddLogger(IServiceCollection, IHostEnvironment, IConfiguration)
Create and configure the logger.
Declaration
public static IServiceCollection AddLogger(this IServiceCollection services, IHostEnvironment hostEnvironment, IConfiguration configuration)
Parameters
| Type | Name | Description |
|---|---|---|
| IServiceCollection | services | |
| IHostEnvironment | hostEnvironment | |
| IConfiguration | configuration |
Returns
| Type | Description |
|---|---|
| IServiceCollection |
Remarks
Additional Serilog services are registered during Microsoft.Extensions.Hosting.HostBuilderExtensions.ConfigureUmbracoDefaults(Microsoft.Extensions.Hosting.IHostBuilder).
AddMultipleUnique<TService1, TService2, TImplementing>(IServiceCollection)
Adds services of types TService1 & TService2 with a shared
implementation type of TImplementing to the specified Microsoft.Extensions.DependencyInjection.IServiceCollection.
Declaration
public static IServiceCollection AddMultipleUnique<TService1, TService2, TImplementing>(this IServiceCollection services) where TService1 : class where TService2 : class where TImplementing : class, TService1, TService2
Parameters
| Type | Name | Description |
|---|---|---|
| IServiceCollection | services |
Returns
| Type | Description |
|---|---|
| IServiceCollection | A reference to this instance after the operation has completed. |
Type Parameters
| Name | Description |
|---|---|
| TService1 | |
| TService2 | |
| TImplementing |
Remarks
Removes all previous registrations for the types TService1 &
TService2.
AddMultipleUnique<TService1, TService2, TImplementing>(IServiceCollection, ServiceLifetime)
Adds services of types TService1 & TService2 with a shared implementation type of TImplementing to the specified Microsoft.Extensions.DependencyInjection.IServiceCollection.
Declaration
public static IServiceCollection AddMultipleUnique<TService1, TService2, TImplementing>(this IServiceCollection services, ServiceLifetime lifetime) where TService1 : class where TService2 : class where TImplementing : class, TService1, TService2
Parameters
| Type | Name | Description |
|---|---|---|
| IServiceCollection | services | |
| ServiceLifetime | lifetime |
Returns
| Type | Description |
|---|---|
| IServiceCollection | A reference to this instance after the operation has completed. |
Type Parameters
| Name | Description |
|---|---|
| TService1 | |
| TService2 | |
| TImplementing |
Remarks
Removes all previous registrations for the types TService1 & TService2.
AddRecurringBackgroundJob<TJob>(IServiceCollection)
Adds a recurring background job with an implementation type of
TJob to the specified Microsoft.Extensions.DependencyInjection.IServiceCollection.
Declaration
public static void AddRecurringBackgroundJob<TJob>(this IServiceCollection services) where TJob : class, IRecurringBackgroundJob
Parameters
| Type | Name | Description |
|---|---|---|
| IServiceCollection | services |
Type Parameters
| Name | Description |
|---|---|
| TJob |
AddRecurringBackgroundJob<TJob>(IServiceCollection, Func<IServiceProvider, TJob>)
Adds a recurring background job with an implementation type of
TJob using the factory implementationFactory
to the specified Microsoft.Extensions.DependencyInjection.IServiceCollection.
Declaration
public static void AddRecurringBackgroundJob<TJob>(this IServiceCollection services, Func<IServiceProvider, TJob> implementationFactory) where TJob : class, IRecurringBackgroundJob
Parameters
| Type | Name | Description |
|---|---|---|
| IServiceCollection | services | |
| Func<IServiceProvider, TJob> | implementationFactory |
Type Parameters
| Name | Description |
|---|---|
| TJob |
AddTypeLoader(IServiceCollection, Assembly?, ILoggerFactory, IConfiguration)
Called to create the TypeLoader to assign to the IUmbracoBuilder
Declaration
public static TypeLoader AddTypeLoader(this IServiceCollection services, Assembly? entryAssembly, ILoggerFactory loggerFactory, IConfiguration configuration)
Parameters
| Type | Name | Description |
|---|---|---|
| IServiceCollection | services | |
| Assembly | entryAssembly | |
| ILoggerFactory | loggerFactory | |
| IConfiguration | configuration |
Returns
| Type | Description |
|---|---|
| TypeLoader |
Remarks
This should never be called in a web project. It is used internally by Umbraco but could be used in unit tests. If called in a web project it will have no affect except to create and return a new TypeLoader but this will not be the instance in DI.
AddTypeLoader(IServiceCollection, Assembly, ILoggerFactory, AppCaches, IConfiguration, IProfiler)
Called to create the TypeLoader to assign to the IUmbracoBuilder
Declaration
[Obsolete("Please use alternative extension method.")]
public static TypeLoader AddTypeLoader(this IServiceCollection services, Assembly entryAssembly, ILoggerFactory loggerFactory, AppCaches appCaches, IConfiguration configuration, IProfiler profiler)
Parameters
| Type | Name | Description |
|---|---|---|
| IServiceCollection | services | |
| Assembly | entryAssembly | |
| ILoggerFactory | loggerFactory | |
| AppCaches | appCaches | |
| IConfiguration | configuration | |
| IProfiler | profiler |
Returns
| Type | Description |
|---|---|
| TypeLoader |
Remarks
This should never be called in a web project. It is used internally by Umbraco but could be used in unit tests. If called in a web project it will have no affect except to create and return a new TypeLoader but this will not be the instance in DI.
AddUnique(IServiceCollection, Type, object)
Adds a singleton service of the type specified by serviceType to the specified
Microsoft.Extensions.DependencyInjection.IServiceCollection.
Declaration
public static IServiceCollection AddUnique(this IServiceCollection services, Type serviceType, object instance)
Parameters
| Type | Name | Description |
|---|---|---|
| IServiceCollection | services | |
| Type | serviceType | |
| object | instance |
Returns
| Type | Description |
|---|---|
| IServiceCollection | A reference to this instance after the operation has completed. |
Remarks
Removes all previous registrations for the type specified by serviceType.
AddUnique<TService>(IServiceCollection, Func<IServiceProvider, TService>)
Adds a service of type TService with an implementation factory method to the specified
Microsoft.Extensions.DependencyInjection.IServiceCollection.
Declaration
public static IServiceCollection AddUnique<TService>(this IServiceCollection services, Func<IServiceProvider, TService> factory) where TService : class
Parameters
| Type | Name | Description |
|---|---|---|
| IServiceCollection | services | |
| Func<IServiceProvider, TService> | factory |
Returns
| Type | Description |
|---|---|
| IServiceCollection | A reference to this instance after the operation has completed. |
Type Parameters
| Name | Description |
|---|---|
| TService |
Remarks
Removes all previous registrations for the type TService.
AddUnique<TService>(IServiceCollection, Func<IServiceProvider, TService>, ServiceLifetime)
Adds a service of type TService with an implementation factory method to the specified Microsoft.Extensions.DependencyInjection.IServiceCollection.
Declaration
public static IServiceCollection AddUnique<TService>(this IServiceCollection services, Func<IServiceProvider, TService> factory, ServiceLifetime lifetime) where TService : class
Parameters
| Type | Name | Description |
|---|---|---|
| IServiceCollection | services | |
| Func<IServiceProvider, TService> | factory | |
| ServiceLifetime | lifetime |
Returns
| Type | Description |
|---|---|
| IServiceCollection | A reference to this instance after the operation has completed. |
Type Parameters
| Name | Description |
|---|---|
| TService |
Remarks
Removes all previous registrations for the type TService.
AddUnique<TService>(IServiceCollection, TService)
Adds a singleton service of type TService to the specified
Microsoft.Extensions.DependencyInjection.IServiceCollection.
Declaration
public static IServiceCollection AddUnique<TService>(this IServiceCollection services, TService instance) where TService : class
Parameters
| Type | Name | Description |
|---|---|---|
| IServiceCollection | services | |
| TService | instance |
Returns
| Type | Description |
|---|---|
| IServiceCollection | A reference to this instance after the operation has completed. |
Type Parameters
| Name | Description |
|---|---|
| TService |
Remarks
Removes all previous registrations for the type type TService.
AddUnique<TService, TImplementing>(IServiceCollection)
Adds a service of type TService with an implementation type of
TImplementing to the specified Microsoft.Extensions.DependencyInjection.IServiceCollection.
Declaration
public static IServiceCollection AddUnique<TService, TImplementing>(this IServiceCollection services) where TService : class where TImplementing : class, TService
Parameters
| Type | Name | Description |
|---|---|---|
| IServiceCollection | services |
Returns
| Type | Description |
|---|---|
| IServiceCollection | A reference to this instance after the operation has completed. |
Type Parameters
| Name | Description |
|---|---|
| TService | |
| TImplementing |
Remarks
Removes all previous registrations for the type TService.
AddUnique<TService, TImplementing>(IServiceCollection, ServiceLifetime)
Adds a service of type TService with an implementation type of
TImplementing to the specified Microsoft.Extensions.DependencyInjection.IServiceCollection.
Declaration
public static IServiceCollection AddUnique<TService, TImplementing>(this IServiceCollection services, ServiceLifetime lifetime) where TService : class where TImplementing : class, TService
Parameters
| Type | Name | Description |
|---|---|---|
| IServiceCollection | services | |
| ServiceLifetime | lifetime |
Returns
| Type | Description |
|---|---|
| IServiceCollection | A reference to this instance after the operation has completed. |
Type Parameters
| Name | Description |
|---|---|
| TService | |
| TImplementing |
Remarks
Removes all previous registrations for the type TService.