Table of Contents

Class ServiceCollectionServiceExtensions

Namespace
Microsoft.Extensions.DependencyInjection
Assembly
nanoFramework.DependencyInjection.dll

Extensions for IServiceCollection.

public static class ServiceCollectionServiceExtensions
Inheritance
ServiceCollectionServiceExtensions
Inherited Members

Methods

AddScoped(IServiceCollection, Type)

Adds a scoped service of the type specified in serviceType to the specified IServiceCollection.

public static IServiceCollection AddScoped(this IServiceCollection services, Type serviceType)

Parameters

services IServiceCollection

The IServiceCollection to add the service to.

serviceType Type

The type of the service to register and the implementation to use.

Returns

IServiceCollection

A reference to this instance after the operation has completed.

Exceptions

ArgumentNullException

services can't be null.

See Also

AddScoped(IServiceCollection, Type, ImplementationFactoryDelegate)

Adds a scoped service of the type specified in serviceType with a factory specified in implementationFactory to the specified IServiceCollection.

public static IServiceCollection AddScoped(this IServiceCollection services, Type serviceType, ImplementationFactoryDelegate implementationFactory)

Parameters

services IServiceCollection

The IServiceCollection to add the service to.

serviceType Type

The type of the service to register.

implementationFactory ImplementationFactoryDelegate

The factory that creates the service.

Returns

IServiceCollection

A reference to this instance after the operation has completed.

Exceptions

ArgumentNullException

services can't be null.

ArgumentNullException

serviceType can't be null.

ArgumentNullException

implementationFactory can't be null.

See Also

AddScoped(IServiceCollection, Type, Type)

Adds a scoped service of the type specified in serviceType with an implementation of the type specified in implementationType to the specified IServiceCollection.

public static IServiceCollection AddScoped(this IServiceCollection services, Type serviceType, Type implementationType)

Parameters

services IServiceCollection

The IServiceCollection to add the service to.

serviceType Type

The type of the service to register.

implementationType Type

The implementation type of the service.

Returns

IServiceCollection

A reference to this instance after the operation has completed.

Exceptions

ArgumentNullException

services can't be null.

See Also

AddSingleton(IServiceCollection, Type)

Adds a singleton service of the type specified in serviceType to the specified IServiceCollection.

public static IServiceCollection AddSingleton(this IServiceCollection services, Type serviceType)

Parameters

services IServiceCollection

The IServiceCollection to add the service to.

serviceType Type

The type of the service to register and the implementation to use.

Returns

IServiceCollection

A reference to this instance after the operation has completed.

Exceptions

ArgumentNullException

services can't be null.

See Also

AddSingleton(IServiceCollection, Type, ImplementationFactoryDelegate)

Adds a singleton service of the type specified in serviceType with a factory specified in implementationFactory to the specified IServiceCollection.

public static IServiceCollection AddSingleton(this IServiceCollection services, Type serviceType, ImplementationFactoryDelegate implementationFactory)

Parameters

services IServiceCollection

The IServiceCollection to add the service to.

serviceType Type

The type of the service to register.

implementationFactory ImplementationFactoryDelegate

The factory that creates the service.

Returns

IServiceCollection

A reference to this instance after the operation has completed.

Exceptions

ArgumentNullException

services can't be null.

ArgumentNullException

serviceType can't be null.

ArgumentNullException

implementationFactory can't be null.

See Also

AddSingleton(IServiceCollection, Type, object)

Adds a singleton service of the type specified in serviceType with an instance specified in implementationInstance to the specified IServiceCollection.

public static IServiceCollection AddSingleton(this IServiceCollection services, Type serviceType, object implementationInstance)

Parameters

services IServiceCollection

The IServiceCollection to add the service to.

serviceType Type

The type of the service to register.

implementationInstance object

The instance of the service.

Returns

IServiceCollection

A reference to this instance after the operation has completed.

Exceptions

ArgumentNullException

services can't be null.

See Also

AddSingleton(IServiceCollection, Type, Type)

Adds a singleton service of the type specified in serviceType with an implementation of the type specified in implementationType to the specified IServiceCollection.

public static IServiceCollection AddSingleton(this IServiceCollection services, Type serviceType, Type implementationType)

Parameters

services IServiceCollection

The IServiceCollection to add the service to.

serviceType Type

The type of the service to register.

implementationType Type

The implementation type of the service.

Returns

IServiceCollection

A reference to this instance after the operation has completed.

Exceptions

ArgumentNullException

services can't be null.

See Also

AddTransient(IServiceCollection, Type)

Adds a transient service of the type specified in serviceType to the specified IServiceCollection.

public static IServiceCollection AddTransient(this IServiceCollection services, Type serviceType)

Parameters

services IServiceCollection

The IServiceCollection to add the service to.

serviceType Type

The type of the service to register and the implementation to use.

Returns

IServiceCollection

A reference to this instance after the operation has completed.

Exceptions

ArgumentNullException

services can't be null.

See Also

AddTransient(IServiceCollection, Type, ImplementationFactoryDelegate)

Adds a transient service of the type specified in serviceType with a factory specified in implementationFactory to the specified IServiceCollection.

public static IServiceCollection AddTransient(this IServiceCollection services, Type serviceType, ImplementationFactoryDelegate implementationFactory)

Parameters

services IServiceCollection

The IServiceCollection to add the service to.

serviceType Type

The type of the service to register.

implementationFactory ImplementationFactoryDelegate

The factory that creates the service.

Returns

IServiceCollection

A reference to this instance after the operation has completed.

Exceptions

ArgumentNullException

services can't be null.

ArgumentNullException

serviceType can't be null.

ArgumentNullException

implementationFactory can't be null.

See Also

AddTransient(IServiceCollection, Type, Type)

Adds a transient service of the type specified in serviceType with an implementation of the type specified in implementationType to the specified IServiceCollection.

public static IServiceCollection AddTransient(this IServiceCollection services, Type serviceType, Type implementationType)

Parameters

services IServiceCollection

The IServiceCollection to add the service to.

serviceType Type

The type of the service to register.

implementationType Type

The implementation type of the service.

Returns

IServiceCollection

A reference to this instance after the operation has completed.

Exceptions

ArgumentNullException

services can't be null.

See Also

RemoveAll(IServiceCollection, Type)

Removes all services of type serviceType in IServiceCollection.

public static IServiceCollection RemoveAll(this IServiceCollection collection, Type serviceType)

Parameters

collection IServiceCollection

The IServiceCollection.

serviceType Type

The service type to remove.

Returns

IServiceCollection

The IServiceCollection for chaining.

Exceptions

ArgumentNullException

serviceType can't be null.

Replace(IServiceCollection, ServiceDescriptor)

Removes the first service in IServiceCollection with the same service type as descriptor and adds descriptor to the collection.

public static IServiceCollection Replace(this IServiceCollection collection, ServiceDescriptor descriptor)

Parameters

collection IServiceCollection

The IServiceCollection.

descriptor ServiceDescriptor

The ServiceDescriptor to replace with.

Returns

IServiceCollection

The IServiceCollection for chaining.

Exceptions

ArgumentNullException

collection or descriptor can't be null.

TryAdd(IServiceCollection, ServiceDescriptor)

Adds the specified descriptor to the collection if the service type hasn't already been registered.

public static void TryAdd(this IServiceCollection collection, ServiceDescriptor descriptor)

Parameters

collection IServiceCollection

The IServiceCollection.

descriptor ServiceDescriptor

The ServiceDescriptor to add.

Exceptions

ArgumentNullException

collection or descriptor can't be null.

TryAddEnumerable(IServiceCollection, ServiceDescriptor)

Adds a ServiceDescriptor if an existing descriptor with the same ServiceType and an implementation that does not already exist in services...

public static void TryAddEnumerable(this IServiceCollection services, ServiceDescriptor descriptor)

Parameters

services IServiceCollection

The IServiceCollection.

descriptor ServiceDescriptor

The ServiceDescriptor.

Remarks

Use TryAddEnumerable(IServiceCollection, ServiceDescriptor) when registering a service implementation of a service type that supports multiple registrations of the same service type. Using Add(ServiceDescriptor) is not idempotent and can add duplicate ServiceDescriptor instances if called twice. Using TryAddEnumerable(IServiceCollection, ServiceDescriptor) will prevent registration of multiple implementation types.

Exceptions

ArgumentException

Implementation type cannot be 'implementationType' because it is indistinguishable from other services registered for 'descriptor.ServiceType'.

ArgumentNullException

services or descriptor can't be null.

TryAddEnumerable(IServiceCollection, IEnumerable)

Adds the specified ServiceDescriptors if an existing descriptor with the same ServiceType and an implementation that does not already exist in services...

public static void TryAddEnumerable(this IServiceCollection services, IEnumerable descriptors)

Parameters

services IServiceCollection

The IServiceCollection.

descriptors IEnumerable

The ServiceDescriptors.

Remarks

Use TryAddEnumerable(IServiceCollection, ServiceDescriptor) when registering a service implementation of a service type that supports multiple registrations of the same service type. Using Add(ServiceDescriptor) is not idempotent and can add duplicate ServiceDescriptor instances if called twice. Using TryAddEnumerable(IServiceCollection, ServiceDescriptor) will prevent registration of multiple implementation types.

Exceptions

ArgumentNullException

services or descriptors can't be null.