ASP.NET Core Tutorialprovides basic and advanced concepts of C# for beginners and professionals.

ASP.NET Core - Implement Dependency Injection

Back to: ASP.NET Core Tutorial

In a .NET Core console application, you can set up dependency injection (DI) in a way similar to an ASP.NET Core app, even though console apps don't have a built-in DI container by default. Here’s how to configure and use DI in a .NET Core console app.

Steps to Set Up Dependency Injection in a .NET Core Console App

  1. Create a Console Application:

    bash
    dotnet new console -o MyConsoleApp
    cd MyConsoleApp
  2. Install Required Packages: Ensure you have the necessary NuGet packages. The default dependency injection support is available through Microsoft.Extensions.DependencyInjection.

    bash
    dotnet add package Microsoft.Extensions.DependencyInjection
  3. Configure Services and DI Container: Create a ServiceCollection and register your dependencies in Program.cs.

    csharp
    using System;
    using Microsoft.Extensions.DependencyInjection;
    namespace MyConsoleApp
    {
    public class Program
    {
    public static void Main(string[] args)
    {
    // Create a new ServiceCollection and configure it with dependencies
    var serviceCollection = new ServiceCollection();
    ConfigureServices(serviceCollection);
    // Build the ServiceProvider and create a scope
    using (var serviceProvider = serviceCollection.BuildServiceProvider())
    {
    var app = serviceProvider.GetRequiredService<App>();
    app.Run();
    }
    }
    private static void ConfigureServices(IServiceCollection services)
    {
    // Register dependencies
    services.AddTransient<App>();
    // Register App class as a dependency
    services.AddSingleton<IMyService,MyService>();
    }
    }
    public class App
    {
    private readonly IMyService _myService;
    public App(IMyService myService)
    {
    _myService = myService;
    }
    public void Run()
    {
    Console.WriteLine("Running the app...");
    _myService.PerformTask();
    }
    }
    public interface IMyService
    {
    void PerformTask();
    }
    public class MyService : IMyService
    {
    public void PerformTask()
    {
    Console.WriteLine("Task is being performed.");
    }
    }

Explanation

  • ServiceCollection: This is where you register your services. For instance, services.AddSingleton<IMyService, MyService>() registers MyService as a singleton for the IMyService interface.
  • ServiceProvider: After configuring all dependencies, calling serviceCollection.BuildServiceProvider() builds the container.
  • App Class: This is the entry point for the app's functionality, which receives dependencies (e.g., IMyService) through constructor injection.

Running the Application

Run your application:

bash
dotnet run

This will print:

Running the app...
Task is being performed.

Using dependency injection like this in a console application allows you to manage dependencies efficiently and adopt a clean, testable architecture.

Scroll to Top