Creating a console app with Dependency Injection in .NET Core
Tags: programming, net, c, core
Published: 2021-11-08 20:00:31

---
title: Creating a console app with Dependency Injection in .NET Core
published: true
date: 2021-11-08 19:43:37 UTC
tags: programming,net,c,core
canonical_url: https://siderite.dev/blog/creating-console-app-with-dependency-injection-in-/
---

Original post: https://siderite.dev/blog/creating-console-app-with-dependency-injection-in-/

## Intro

Dependency injection is baked in the ASP.Net Core projects (yes, I still call it Core), but it's missing from console app templates. And while it is easy to add, it's not that clear cut on how to do it. I present here three ways to do it:

1. The fast one: use the Worker Service template and tweak it to act like a console application
2. The simple one: use the Console Application template and add dependency injection to it
3. The hybrid: use the Console Application template and use the same system as in the Worker Service template

## Tweak the Worker Service template

It makes sense that if you want a console application you would select the Console Application template when creating a new project, but as mentioned above, it's just the default template, as old as console apps are. Yet there is another default template, called Worker Service, which almost does the same thing, only it has all the dependency injection goodness baked in, just like an ASP.Net Core Web App template.

So start your Visual Studio, add a new project and choose Worker Service:

![Choosing the Worker Service template](https://siderite.dev/Posts/files/WorkerServiceTemplate_637719974170753415.jpg)

It will create a project containing a Program.cs, a Worker.cs and an appsettings.json file. Program.cs holds the setup and Worker.cs holds the code to be executed.

![project file structure](https://siderite.dev/Posts/files/WorkerServiceFileStructure_637719974170807907.jpg)

Worker.cs has an ExecuteAsync method that logs some stuff every second, but even if we remove the while loop and add our own code, the application doesn't stop. This might be a good thing, as sometimes we just want stuff to work until we press Ctrl-C, but it's not a console app per se.

In order to transform it into something that works just like a console application you need to follow these steps:

1. inject an IHost instance into your worker
2. specifically instruct the host to stop whenever your code has finished

So, you go from:

```
public class Worker : BackgroundService
{
    private readonly ILogger<Worker> _logger;

    public Worker(ILogger<Worker> logger)
    {
        _logger = logger;
    }

    protected override async Task ExecuteAsync(CancellationToken stoppingToken)
    {
        while (!stoppingToken.IsCancellationRequested)
        {
            _logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now);
            await Task.Delay(1000, stoppingToken);
        }
    }
}
```

to:

```
public class Worker : BackgroundService
{
    private readonly ILogger<Worker> _logger;
    private readonly IHost _host;

    public Worker(ILogger<Worker> logger, IHost host)
    {
        _logger = logger;
        _host = host;
    }

    protected override async Task ExecuteAsync(CancellationToken stoppingToken)
    {
        Console.WriteLine("Hello world!");
        _host.StopAsync();
    }
}
```

Note that I did not "await" the StopAsync method because I don't actually need to. You are telling the host to stop and it will do it whenever it will see fit.

If we look into the Program.cs code we will see this:

```
public class Program
{
    public static void Main(string[] args)
    {
        CreateHostBuilder(args).Build().Run();
    }

    public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .ConfigureServices((hostContext, services) =>
            {
                services.AddHostedService<Worker>();
            });
}
```

I don't know why they bothered with creating a new method and then writing it as an expression body, but that's the template. You see that there is a lambda adding dependencies (by default just the Worker class), but everything starts with Host.CreateDefaultBuilder. In .NET source code, this leads to [HostingHostBuilderExtensions.ConfigureDefaults](https://github.com/dotnet/runtime/blob/main/src/libraries/Microsoft.Extensions.Hosting/src/HostingHostBuilderExtensions.cs#L188 "Configures an existing IHostBuilder instance with pre-configured defaults. This will overwrite previously configured values and is intended to be called before additional configuration calls."), which adds a lot of stuff:

- environment variables to config
- command line arguments to config (via CommandLineConfigurationSource)
- support for appsettings.json configuration
- logging based on operating system

That is why, if you want these things by default, your best bet is to tweak the Worker Service template

## Add Dependency Injection to an existing console application

Some people want to have total control over what their code is doing. Or maybe you already have a console application doing stuff and you just want to add Dependency Injection. In that case, these are the steps you want to follow:

1. create a ServiceCollection instance (needs a dependency to Microsoft.Extensions.DependencyInjection)
2. register all dependencies you want to use to it
3. create a starting class that will execute stuff (just like Worker above)
4. register starting class in the service collection
5. build a service provider from the collection
6. get an instance of the starting class and execute its one method

Here is an example:

```
class Program
{
    static void Main(string[] args)
    {
        var services = new ServiceCollection();
        ConfigureServices(services);
        services
            .AddSingleton<Executor,Executor>()
            .BuildServiceProvider()
            .GetService<Executor>()
            .Execute();
    }

    private static void ConfigureServices(IServiceCollection services)
    {
        services
            .AddSingleton<ITest, Test>();
    }
}

public class Executor
{
    private readonly ITest _test;

    public Executor(ITest test)
    {
        _test = test;
    }

    public void Execute()
    {
        _test.DoSomething();
    }
}
```

The only reason we register the Executor class is in order to get an instance of it later, complete with constructor injection support. You can even make Execute an async method, so you can get full async/await support. Of course, for this example appsettings.json configuration or logging won't work until you add them.

## Mix them up

Of course, one could try to get the best of both worlds. This would work kind of like this:

1. use Host.CreateDefaultBuilder() anyway (needs a dependency to Microsoft.Extensions.Hosting), but in a normal console app
2. use the resulting service provider to instantiate a starting class
3. start it

Here is an example:

```
class Program
{
    static void Main(string[] args)
    {
        Host.CreateDefaultBuilder()
            .ConfigureServices(ConfigureServices)
            .ConfigureServices(services => services.AddSingleton<Executor>())
            .Build()
            .Services
            .GetService<Executor>()
            .Execute();
    }

    private static void ConfigureServices(HostBuilderContext hostContext, IServiceCollection services)
    {
        services.AddSingleton<ITest,Test>();
    }
}
```

The Executor class would be just like in the section above, but now you get all the default logging and configuration options from the Worker Service section.

## Conclusion

What the quickest and best solution is depends on your situation. One could just as well start with a Worker Service template, then tweak it to never Run the builder and instead configure it as above. One can create a Startup class, complete with Configure and ConfigureServices as in an ASP.Net Core Web App template or even start with such a template, then tweak it to work as a console app/web hybrid. In .NET Core everything is a console app anyway, it's just depends on which packages you load and how you configure them.