Five New Optimizely Certifications are Here! Validate your expertise and advance your career with our latest certification exams. Click here to find out more

Class ServiceCollectionExtensions

Provides extension methods for Microsoft.Extensions.DependencyInjection.IServiceCollection implemenations.

Inheritance
System.Object
ServiceCollectionExtensions
Namespace: EPiServer.ServiceLocation
Assembly: EPiServer.Framework.dll
Version: 12.0.3
Syntax
public static class ServiceCollectionExtensions : Object

Methods

Add(IServiceCollection, Type, Func<IServiceProvider, Object>, ServiceLifetime)

Registers implementation factory for a service

Declaration
public static IServiceCollection Add(this IServiceCollection services, Type serviceType, Func<IServiceProvider, object> implementationFactory, ServiceLifetime serviceLifetime)
Parameters
Type Name Description
Microsoft.Extensions.DependencyInjection.IServiceCollection services

The service collection that is extended

System.Type serviceType

The service type

System.Func<System.IServiceProvider, System.Object> implementationFactory

The implementation factory

Microsoft.Extensions.DependencyInjection.ServiceLifetime serviceLifetime

The lifetime of the service.

Returns
Type Description
Microsoft.Extensions.DependencyInjection.IServiceCollection

The service collection

Add(IServiceCollection, Type, Type, ServiceLifetime)

Registers implementation type for a service

Declaration
public static IServiceCollection Add(this IServiceCollection services, Type serviceType, Type implementationType, ServiceLifetime serviceLifetime)
Parameters
Type Name Description
Microsoft.Extensions.DependencyInjection.IServiceCollection services

The service collection that is extended

System.Type serviceType

The service type

System.Type implementationType

The implementation type.

Microsoft.Extensions.DependencyInjection.ServiceLifetime serviceLifetime

The lifetime of the service.

Returns
Type Description
Microsoft.Extensions.DependencyInjection.IServiceCollection

The service collection

Add<TService>(IServiceCollection, Func<IServiceProvider, TService>, ServiceLifetime)

Registers lifecycled implementation factory for a service

Declaration
public static IServiceCollection Add<TService>(this IServiceCollection services, Func<IServiceProvider, TService> implementationFactory, ServiceLifetime serviceLifetime)
    where TService : class
Parameters
Type Name Description
Microsoft.Extensions.DependencyInjection.IServiceCollection services

The service provider that is extended

System.Func<System.IServiceProvider, TService> implementationFactory

The implementation factory

Microsoft.Extensions.DependencyInjection.ServiceLifetime serviceLifetime

The lifetime of the service.

Returns
Type Description
Microsoft.Extensions.DependencyInjection.IServiceCollection

The service collection

Type Parameters
Name Description
TService

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

Registers a transient (httpcontext or thread local) lifecycled factory for a service.

Declaration
public static IServiceCollection AddHttpContextOrThreadScoped<TService>(this IServiceCollection services, Func<IServiceProvider, TService> implementationFactory)
Parameters
Type Name Description
Microsoft.Extensions.DependencyInjection.IServiceCollection services

The service collection.

System.Func<System.IServiceProvider, TService> implementationFactory

The factory

Returns
Type Description
Microsoft.Extensions.DependencyInjection.IServiceCollection

The service configuration provider

Type Parameters
Name Description
TService

The service type

AddHttpContextOrThreadScoped<TService, TImpl>(IServiceCollection)

Registers a hybrid (httpcontext or thread local) lifecycled implementation type for a service

Declaration
public static IServiceCollection AddHttpContextOrThreadScoped<TService, TImpl>(this IServiceCollection services)
    where TImpl : TService
Parameters
Type Name Description
Microsoft.Extensions.DependencyInjection.IServiceCollection services

The service provider that is extended

Returns
Type Description
Microsoft.Extensions.DependencyInjection.IServiceCollection

The service configuration provider

Type Parameters
Name Description
TService

The service type

TImpl

The implementation type

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

Registers a hybrid (httpcontext or thread local) lifecycled instance factory as a service

Declaration
public static IServiceCollection AddScoped(this IServiceCollection services, Type serviceType, Func<IServiceProvider, object> implementationFactory)
Parameters
Type Name Description
Microsoft.Extensions.DependencyInjection.IServiceCollection services

The service collection that is extended

System.Type serviceType

The service type

System.Func<System.IServiceProvider, System.Object> implementationFactory

The implementation factory

Returns
Type Description
Microsoft.Extensions.DependencyInjection.IServiceCollection

The service collection

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

Registers a scoped lifecycled factory as a service

Declaration
public static IServiceCollection AddScoped<TService>(this IServiceCollection services, Func<IServiceProvider, TService> implementationFactory)
    where TService : class
Parameters
Type Name Description
Microsoft.Extensions.DependencyInjection.IServiceCollection services

The service collection that is extended

System.Func<System.IServiceProvider, TService> implementationFactory

The instance factory

Returns
Type Description
Microsoft.Extensions.DependencyInjection.IServiceCollection

The service collection

Type Parameters
Name Description
TService

AddScoped<TService, TImplementation>(IServiceCollection, Func<IServiceProvider, TImplementation>)

Registers a hybrid (httpcontext or thread local) lifecycled factory as a service

Declaration
public static IServiceCollection AddScoped<TService, TImplementation>(this IServiceCollection services, Func<IServiceProvider, TImplementation> implementationFactory)
    where TService : class where TImplementation : class, TService
Parameters
Type Name Description
Microsoft.Extensions.DependencyInjection.IServiceCollection services

The service collection that is extended

System.Func<System.IServiceProvider, TImplementation> implementationFactory

The instance factory

Returns
Type Description
Microsoft.Extensions.DependencyInjection.IServiceCollection

The service collection

Type Parameters
Name Description
TService
TImplementation

AddServiceAccessor(IServiceCollection)

Add service accessor for latest registered service.

Declaration
public static IServiceCollection AddServiceAccessor(this IServiceCollection services)
Parameters
Type Name Description
Microsoft.Extensions.DependencyInjection.IServiceCollection services

The service collection that is extended

Returns
Type Description
Microsoft.Extensions.DependencyInjection.IServiceCollection

The service collection

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

Registers a singleton lifecycled factory for a service

Declaration
public static IServiceCollection AddSingleton(this IServiceCollection services, Type serviceType, Func<IServiceProvider, object> implementationFactory)
Parameters
Type Name Description
Microsoft.Extensions.DependencyInjection.IServiceCollection services

The service collection that is extended

System.Type serviceType

The service type

System.Func<System.IServiceProvider, System.Object> implementationFactory

The implementation factory

Returns
Type Description
Microsoft.Extensions.DependencyInjection.IServiceCollection

The service collection

AddSingleton<TService, TImplementation>(IServiceCollection, Func<IServiceProvider, TImplementation>)

Registers a singleton lifecycled factory as a service

Declaration
public static IServiceCollection AddSingleton<TService, TImplementation>(this IServiceCollection services, Func<IServiceProvider, TImplementation> implementationFactory)
    where TService : class where TImplementation : class, TService
Parameters
Type Name Description
Microsoft.Extensions.DependencyInjection.IServiceCollection services

The service collection that is extended

System.Func<System.IServiceProvider, TImplementation> implementationFactory

The factory used to create the singleton instance

Returns
Type Description
Microsoft.Extensions.DependencyInjection.IServiceCollection

The service collection

Type Parameters
Name Description
TService
TImplementation

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

Registers a transient lifecycled implementation factory for a service

Declaration
public static IServiceCollection AddTransient(this IServiceCollection services, Type serviceType, Func<IServiceProvider, object> implementationFactory)
Parameters
Type Name Description
Microsoft.Extensions.DependencyInjection.IServiceCollection services

The service collection that is extended

System.Type serviceType

The service type

System.Func<System.IServiceProvider, System.Object> implementationFactory

The implementation factory

Returns
Type Description
Microsoft.Extensions.DependencyInjection.IServiceCollection

The service collection

AddTransient<TService, TImplementation>(IServiceCollection, Func<IServiceProvider, TImplementation>)

Registers a transient lifecycled factory for a service

Declaration
public static IServiceCollection AddTransient<TService, TImplementation>(this IServiceCollection services, Func<IServiceProvider, TImplementation> implementationFactory)
    where TService : class where TImplementation : class, TService
Parameters
Type Name Description
Microsoft.Extensions.DependencyInjection.IServiceCollection services

The service provider that is extended

System.Func<System.IServiceProvider, TImplementation> implementationFactory

The factory method to create instances

Returns
Type Description
Microsoft.Extensions.DependencyInjection.IServiceCollection

The service collection

Type Parameters
Name Description
TService
TImplementation

TryAdd(IServiceCollection, Type, Func<IServiceProvider, Object>, ServiceLifetime)

Registers implementation factory for a service if it does not exist

Declaration
public static IServiceCollection TryAdd(this IServiceCollection services, Type serviceType, Func<IServiceProvider, object> implementationFactory, ServiceLifetime serviceLifetime)
Parameters
Type Name Description
Microsoft.Extensions.DependencyInjection.IServiceCollection services

The service collection that is extended

System.Type serviceType

The service type

System.Func<System.IServiceProvider, System.Object> implementationFactory

The implementation factory

Microsoft.Extensions.DependencyInjection.ServiceLifetime serviceLifetime

The lifetime of the service.

Returns
Type Description
Microsoft.Extensions.DependencyInjection.IServiceCollection

The service collection

TryAdd(IServiceCollection, Type, Type, ServiceLifetime)

Registers implementation type for a service if it does not exist

Declaration
public static IServiceCollection TryAdd(this IServiceCollection services, Type serviceType, Type implementationType, ServiceLifetime serviceLifetime)
Parameters
Type Name Description
Microsoft.Extensions.DependencyInjection.IServiceCollection services

The service collection that is extended

System.Type serviceType

The service type

System.Type implementationType

The implementation type

Microsoft.Extensions.DependencyInjection.ServiceLifetime serviceLifetime

The lifetime of the service.

Returns
Type Description
Microsoft.Extensions.DependencyInjection.IServiceCollection

The service collection

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

Registers the service if not already registered.

Declaration
public static IServiceCollection TryAdd<TService>(this IServiceCollection services, Func<IServiceProvider, TService> implementationFactory)
    where TService : class
Parameters
Type Name Description
Microsoft.Extensions.DependencyInjection.IServiceCollection services

The service collection that is extended

System.Func<System.IServiceProvider, TService> implementationFactory

The implementation factory

Returns
Type Description
Microsoft.Extensions.DependencyInjection.IServiceCollection

The service collection

Type Parameters
Name Description
TService

The service type

TryAdd<TService>(IServiceCollection, Func<IServiceProvider, TService>, ServiceLifetime)

Registers the service if not already registered.

Declaration
public static IServiceCollection TryAdd<TService>(this IServiceCollection services, Func<IServiceProvider, TService> implementationFactory, ServiceLifetime serviceLifetime)
    where TService : class
Parameters
Type Name Description
Microsoft.Extensions.DependencyInjection.IServiceCollection services

The service collection that is extended

System.Func<System.IServiceProvider, TService> implementationFactory

The implementation factory

Microsoft.Extensions.DependencyInjection.ServiceLifetime serviceLifetime

The lifetime of the service.

Returns
Type Description
Microsoft.Extensions.DependencyInjection.IServiceCollection

The service collection

Type Parameters
Name Description
TService

The service type

TryAdd<TService, TImplementationType>(IServiceCollection, ServiceLifetime)

Registers the service if not already registered.

Declaration
public static IServiceCollection TryAdd<TService, TImplementationType>(this IServiceCollection services, ServiceLifetime serviceLifetime)
    where TService : class
Parameters
Type Name Description
Microsoft.Extensions.DependencyInjection.IServiceCollection services

The service collection that is extended

Microsoft.Extensions.DependencyInjection.ServiceLifetime serviceLifetime

The lifetime of the service.

Returns
Type Description
Microsoft.Extensions.DependencyInjection.IServiceCollection

The service collection

Type Parameters
Name Description
TService

The service type.

TImplementationType

The implementation type.