How to implement Dependency Injection in .NET Core Console Application

Updated on: May 26, 2021

Below easy and simple 5 steps help to implement Dependency Injection in .NET Core Console Application which does not have Startup.cs class as we have it in .NET Core Web Application (Web API, MVC & Blazor Application) but we can implement it by adding required NuGet package and few lines of code to register services, read below code to understand complete implementation of Dependency Injection in Console Application:

Step 1: Create .NET Core Console App from .NET CLI command or using Visual Studio: 

dotnet new console -o DIConsoleAppExample

Step 2: Add the required NuGet Packages using NuGet Package Manager from Visual Studio or using .NET CLI commands:

dotnet add package Microsoft.Extensions.DependencyInjection

Step 3: Next step is add your service in your project, we have created a file called UserService.cs in our project and added below Service code:

using System.Collections.Generic;

namespace DIConsoleAppExample
{

    interface IUserService
    {
        List<string> GetUsers();
        void AddUser(string name);

    }

    class UserSerice : IUserService
    {
        private List<string> users = new List<string>();
        public List<string> GetUsers()
        {
            return users;
        }

        public void AddUser(string name)
        {
            users.Add(name);
        }
    }


    class UserApplication
    {
        IUserService _service;
        public UserApplication(IUserService service)
        {
            _service = service;
        }

        public List<string> GetUsers()
        {
            return _service.GetUsers();
        }

        public void AddUser(string name)
        {
            _service.AddUser(name);
        }
    }
}

In the above code, we have used Constructor Dependency Injection of UserService using interface IUserService. Using this we can indirectly call methods of UserService. In Visual Studio, if you right click on service.GetUsers() line  in UserApplication class and click on "Go to Implementation" it will take you to UserService=>GetUsers method.

Step 4: Now, we will add code in Main() method of Program.cs file to add the Dependency Injection between IUserService and UserService. In below code we have registered the Dependency Injection as AddSingleton, you can use other methods(AddScoped, AddTransient) based on your requirement. It is important to add the UserApplication into ServiceCollection (see line number 3 in Main() method), we will not be able to call methods of this class without adding it as Singleton in ServiceCollection:

using Microsoft.Extensions.DependencyInjection;
using System;

namespace DIConsoleAppExample
{
    class Program
    {
        static void Main(string[] args)
        {
            var services = new ServiceCollection();
            services.AddSingleton<IUserService, UserSerice>();
            services.AddSingleton<UserApplication>();
            var serviceProvider = services.BuildServiceProvider();
            var userAppService = serviceProvider.GetService<UserApplication>();
            userAppService.AddUser("John");
            userAppService.AddUser("Sunny");
            var allUsers = userAppService.GetUsers();
            Console.WriteLine($"Registered Users are: {string.Join(", ", allUsers)}");
            Console.ReadLine();
        }
    }
}

Step 5: Now, we can run the project and see the below output:

As shown above, you can implement Dependency Injection in your .NET Core Console application. You have to add your all services into ServiceCollections class like we have added as AddSingleton for UserService and UserApplication class. After that we can call BuildServiceProvider and then we can use GetSerice<UserApplication>() method to get the service and call the methods of that service.