Understanding IServiceCollection in Net Core
In C#, IServiceCollection is an interface provided by the Microsoft.Extensions.DependencyInjection namespace that is used to define a container for managing dependencies in an application. It is a part of the .NET Core dependency injection framework and is commonly used in ASP.NET Core web applications.
The IServiceCollection interface represents a container for registering and resolving services that can be used throughout an application. It provides methods to register different types of services, such as transient, scoped, and singleton services. These services can be registered with or without implementation types and can be easily resolved using constructor injection or property injection in the classes that depend on them.
By using IServiceCollection, developers can decouple the dependencies between classes and promote loose coupling, which can lead to better testability, maintainability, and scalability of their applications.
Here is an example of how IServiceCollection can be used to register a service in an ASP.NET Core application:
public void ConfigureServices(IServiceCollection services)
{
services.AddTransient<ICusomerService, CustomerService>();
}In this example, ICustomerService is an interface that defines the contract for the service, and CustomerService is the implementation of the service. The AddTransient method is used to register the service as a transient service, which means that a new instance of the service will be created each time it is requested from the container.
Once the service is registered, it can be easily injected into other classes that depend on it, as shown below:
public class MyController : Controller
{
private readonly ICustomerService _customerService;
public MyController(ICustomerService customerService)
{
_customerService = customerService;
}
// Controller actions here...
}In this example, MyController depends on ICustomerService, which is injected into the constructor using dependency injection. The instance of CustomerService is automatically provided by the container when the controller is created.
Developer: “Yoooo I ordered a pizza and it came with no toppings on it or anything, it’s just bread.”
Domino’s Customer Service: “We’re sorry to hear about this!”
Developer (minutes later): “Never mind, I opened the pizza upside down.” ☹
If you’re interested in seeing an example source code for the IServiceCollection in a working project, you can check out the file ServiceExtensions.cs in the TalentManagement-ApiResources-Net7 repository. This repository contains the backend API for the full-stack Angular 15 and .NET 7 tutorial series.
Recommended Contents
- Understanding Service Extension in Net Core ♥
- Understanding Abstract Classes in C#
- Understanding Virtual Methods in C#
- What is Generic in C#?
- Seven Object-Oriented Programming Jokes ☻
Thanks for reading! Hope you found it useful. Want more? Please follow me and become a member on medium for more articles. With your support, I’ll keep creating awesome content for you. Have a great day ahead! — Fuji Nguyen






