With Opticon around the corner, we'll be canceling this month's (Sept) Happy Hour.

Class ServiceCollectionExtensions

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

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

Methods

AddServiceAccessor<TService>(IServiceCollection)

Add service accessor for service type.

Declaration
public static IServiceCollection AddServiceAccessor<TService>(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.

Type Parameters
Name Description
TService

The service.

Forward<TExisting, TAdditional>(IServiceCollection)

Forward an existing service of type TExisting to an additional type TAdditional.'

Declaration
public static IServiceCollection Forward<TExisting, TAdditional>(this IServiceCollection services)
    where TExisting : class where TAdditional : class
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
TExisting

The existing service type that should be returned when requesting the additional service.

TAdditional

The service type that should be mapped to the existing service

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

Used to intercept a registered service. Will replace all registered services in container. The interceptor factory will have access to the previous registered service.

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

The service provider that is extended

System.Func<System.IServiceProvider, TService, TService> interceptorFactory

The factory that will be called to create the interceptor. The factory have access to the previous registered service.

Returns
Type Description
Microsoft.Extensions.DependencyInjection.IServiceCollection

The service collection

Type Parameters
Name Description
TService

The existing service that is intercepted

TryForward<TExisting, TAdditional>(IServiceCollection)

Forward an existing service of type TExisting to an additional type TAdditional.'

Declaration
public static IServiceCollection TryForward<TExisting, TAdditional>(this IServiceCollection services)
    where TExisting : class where TAdditional : class
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
TExisting

The existing service type that should be returned when requesting the additional service.

TAdditional

The service type that should be mapped to the existing service

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

Adds an inteceptor to the registered service if not already intercepted. Will replace all registered services in container. The interceptor factory will have access to the previous registered service.

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

The service provider that is extended

System.Func<System.IServiceProvider, TService, TService> interceptorFactory

The factory that will be called to create the interceptor. The factory have access to the previous registered service.

Returns
Type Description
Microsoft.Extensions.DependencyInjection.IServiceCollection

The service collection

Type Parameters
Name Description
TService

The existing service that is intercepted

Remarks

It must be same interceptorFactory that is passed in if called several times to not get duplicate registrations. For example you could have a static Func declared as: private static Func<IServiceProvider, IMyService, IMyService> MyServiceInterceptor = (serviceProvider, defaultMyService) => new InterceptingService(defaultMyService);