IServiceCollection Interface
Namespace: Microsoft.Extensions.DependencyInjection
Represents a collection of service descriptors that can be used to register services with a dependency injection container.
The
IServiceCollection
interface is the primary mechanism for configuring the dependency injection container in .NET. It provides methods to add, replace, and remove service descriptors, allowing you to define how services are resolved and their lifetimes.
Methods
Add(ServiceDescriptor serviceDescriptor)
Appends a to the collection.
void Add(ServiceDescriptor serviceDescriptor);
Parameters
Name | Type | Description |
---|---|---|
serviceDescriptor |
ServiceDescriptor | The ServiceDescriptor to append. |
AddSingleton(Type serviceType)
Adds a singleton service with the specified to the collection.
IServiceCollection AddSingleton(Type serviceType);
Parameters
Name | Type | Description |
---|---|---|
serviceType |
System.Type | The type of the service to register. |
Returns
Type | Description |
---|---|
IServiceCollection | A reference to this instance after the operation has completed. |
AddScoped(Type serviceType)
Adds a scoped service with the specified to the collection.
IServiceCollection AddScoped(Type serviceType);
Parameters
Name | Type | Description |
---|---|---|
serviceType |
System.Type | The type of the service to register. |
Returns
Type | Description |
---|---|
IServiceCollection | A reference to this instance after the operation has completed. |
AddTransient(Type serviceType)
Adds a transient service with the specified to the collection.
IServiceCollection AddTransient(Type serviceType);
Parameters
Name | Type | Description |
---|---|---|
serviceType |
System.Type | The type of the service to register. |
Returns
Type | Description |
---|---|
IServiceCollection | A reference to this instance after the operation has completed. |