.NET Core Dependency Injection with IServiceCollection

Leave a Comment

Modern applications may easily manage dependencies thanks to.NET Core's built-in support for Dependency Injection (DI), a design pattern that facilitates the creation of loosely connected code. Using the IServiceCollection interface, we will examine how DI functions in.NET Core in this post and go over a basic example. 

What Is Dependency Injection?

Dependency Injection is a technique where an object receives its dependencies from an external source rather than creating them itself. This makes the application easier to maintain, test, and scale.

Key Components in .NET Core DI

  • IServiceCollection – Used to register dependencies (services).
  • ServiceProvider – Used to resolve dependencies.
  • Service Lifetimes
    • Singleton: One instance for the application lifetime.
    • Scoped: One instance per request.
    • Transient: A new instance every time.

Example. Setting Up DI in a .NET Core Console Application

Let’s create a simple console app that demonstrates DI.

Step 1. Define Interfaces and Implementations

// IGreeter.cs
public interface IGreeter
{
    void Greet(string name);
}
// ConsoleGreeter.cs
public class ConsoleGreeter : IGreeter
{
    public void Greet(string name)
    {
        Console.WriteLine($"Hello, {name}!");
    }
}

Step 2. Set Up DI with IServiceCollection

// Program.cs
using Microsoft.Extensions.DependencyInjection;
using System;

class Program
{
    static void Main(string[] args)
    {
        // Step 1: Create a new service collection
        var serviceCollection = new ServiceCollection();

        // Step 2: Register services
        serviceCollection.AddTransient<IGreeter, ConsoleGreeter>();

        // Step 3: Build the service provider
        var serviceProvider = serviceCollection.BuildServiceProvider();

        // Step 4: Resolve and use the service
        var greeter = serviceProvider.GetRequiredService<IGreeter>();
        greeter.Greet("Alice");
    }
}

Explanation

  • AddTransient<IGreeter, ConsoleGreeter>(): Registers ConsoleGreeter as the implementation for IGreeter. A new instance is created every time it’s requested.
  • BuildServiceProvider(): Compiles the service registrations into a container that can resolve services.
  • GetRequiredService<IGreeter>(): Requests an instance of IGreeter. The DI container automatically injects the correct implementation.

When to Use Which Lifetime?

Lifetime Use When...
Singleton Service should be shared for the app lifetime
Scoped Per web request (mostly in ASP.NET Core apps)
Transient Lightweight, stateless services

Bonus: Use DI in ASP.NET Core

In ASP.NET Core, you don’t need to manually build the ServiceProvider. Just use ConfigureServices in Startup.cs:

public void ConfigureServices(IServiceCollection services)
{
    services.AddScoped<IGreeter, ConsoleGreeter>();
}

Then, inject via the constructor:

public class HomeController : Controller
{
    private readonly IGreeter _greeter;

    public HomeController(IGreeter greeter)
    {
        _greeter = greeter;
    }

    public IActionResult Index()
    {
        _greeter.Greet("User");
        return View();
    }
}
Conclusion

Dependency Injection in .NET Core is simple and powerful. Using IServiceCollection, you can register and manage dependencies with different lifetimes to keep your code clean and maintainable. Whether you're building a console app or a large web application, understanding and applying DI is a must-have skill in .NET Core development.

Windows Hosting Recommendation

HostForLIFEASP.NET receives Spotlight standing advantage award for providing recommended, cheap and fast ecommerce Hosting including the latest Magento. From the leading technology company, Microsoft. All the servers are equipped with the newest Windows Server 2022 R2, SQL Server 2022, ASP.NET Core 10.0, ASP.NET MVC, Silverlight 5, WebMatrix and Visual Studio Lightswitch. Security and performance are at the core of their Magento hosting operations to confirm every website and/or application hosted on their servers is highly secured and performs at optimum level. mutually of the European ASP.NET hosting suppliers, HostForLIFE guarantees 99.9% uptime and fast loading speed. From €3.49/month , HostForLIFE provides you with unlimited disk space, unlimited domains, unlimited bandwidth,etc, for your website hosting needs.
 
https://hostforlifeasp.net/
Next PostNewer Post Previous PostOlder Post Home

0 comments:

Post a Comment