47

I'm building a simple .NET Core console application that will read in basic options from the command line, then execute and terminate without user interaction. I'd like to take advantage of DI, so that lead me to using the .NET Core generic host.

All of the examples I've found that build a console app create a class that either implements IHostedService or extends BackgroundService. That class then gets added to the service container via AddHostedService and starts the application's work via StartAsync or ExecuteAsync. However, it seems that in all of these examples, they are implemementing a background service or some other application that runs in a loop or waits for requests until it gets shut down by the OS or receives some request to terminate. What if I just want an app that starts, does its thing, then exits? For example:

Program.cs:

namespace MyApp
{
    using System;
    using System.Threading.Tasks;
    using Microsoft.Extensions.DependencyInjection;
    using Microsoft.Extensions.Hosting;
    using Microsoft.Extensions.Logging;

    public static class Program
    {
        public static async Task Main(string[] args)
        {
            await CreateHostBuilder(args).RunConsoleAsync();
        }

        private static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .UseConsoleLifetime()
                .ConfigureLogging(builder => builder.SetMinimumLevel(LogLevel.Warning))
                .ConfigureServices((hostContext, services) =>
                {
                    services.Configure<MyServiceOptions>(hostContext.Configuration);
                    services.AddHostedService<MyService>();
                    services.AddSingleton(Console.Out);
                });
    }
}

MyServiceOptions.cs:

namespace MyApp
{
    public class MyServiceOptions
    {
        public int OpCode { get; set; }
        public int Operand { get; set; }
    }
}

MyService.cs:

namespace MyApp
{
    using System.IO;
    using System.Threading;
    using System.Threading.Tasks;
    using Microsoft.Extensions.Hosting;
    using Microsoft.Extensions.Options;

    public class MyService : IHostedService
    {
        private readonly MyServiceOptions _options;
        private readonly TextWriter _outputWriter;

        public MyService(TextWriter outputWriter, IOptions<MyServiceOptions> options)
        {
            _options = options.Value;
            _outputWriter = outputWriter;
        }

        public async Task StartAsync(CancellationToken cancellationToken)
        {
            _outputWriter.WriteLine("Starting work");

            DoOperation(_options.OpCode, _options.Operand);

            _outputWriter.WriteLine("Work complete");
        }

        public async Task StopAsync(CancellationToken cancellationToken)
        {
            _outputWriter.WriteLine("StopAsync");
        }

        protected void DoOperation(int opCode, int operand)
        {
            _outputWriter.WriteLine("Doing {0} to {1}...", opCode, operand);

            // Do work that might take awhile
        }
    }
}

This code compiles and runs just fine, producing the following output:

Starting work
Doing 1 to 2...
Work complete

However, after that, the application will just sit there waiting until I press Ctrl+C. I know I could force the application to shutdown after the work is complete, but at this point, I feel like I'm not using IHostedService correctly. It seems as though it's designed for recurring background processes, and not simple console applications like this. However, in an actual application where DoOperation might take 20-30 minutes, I would like to take advantage of the StopAsync method to do cleanup before terminating. I also know I could create the service container myself and all that, but the .NET Core generic host already does a lot of stuff I would want to do anyway. It seems to be the right way to write console applications, but without adding a hosted service that kicks off the actual work, how do I get the app to actually do anything?

2
  • If all you want is a DI container, why not just crate a ServiceCollection in a standard console application? Commented Apr 8, 2021 at 1:56
  • 10
    That's not all I want. I want to use things like logging, configuration, and host lifetime management. I know I could just configure and use these services myself, but it seems like the .NET Core generic host is specifically designed for it. Commented Apr 8, 2021 at 2:02

2 Answers 2

47

Instead of a hosted service, I would recommend the following;

using (var host = CreateHostBuilder(args).Build())
{
    await host.StartAsync();
    var lifetime = host.Services.GetRequiredService<IHostApplicationLifetime>();

    // do work here / get your work service ...

    lifetime.StopApplication();
    await host.WaitForShutdownAsync();
}
3
  • I like this approach a lot. It allows for the use of the generic host and all the niceties that get configured by CreateDefaultBuilder while still allowing you to kick off the actual application logic without tying a class to an IHostedService or BackgroundService. Commented Apr 8, 2021 at 19:00
  • 2
    what about a cancellation token? host should manage things like ctrl-c for us, and if our main service is async it's simpler to use HostedService since it receives a token automatically
    – Rast
    Commented Jun 23, 2022 at 22:55
  • 2
    That token is derived from lifetime.ApplicationStopping, which is easily accessible from the above sample. If you prefer hosted services, use them. The question was about an approach to avoid them. Commented Jun 24, 2022 at 1:15
10

I know I could force the application to shutdown after the work is complete, but at this point, I feel like I'm not using IHostedService correctly.

I agree it does seem odd. I actually always stop the application at the end of all my IHostedService implementations. This is true even for long-running server apps. If a hosted service stops (or faults), then I explicitly want the application to end.

It does feel like this design was unfinished when .NET Core was pushed out. There are parts of the design that are made so that hosted services and their apps can have independent lifetimes, but since they can not be restarted, this just isn't useful in practice. So it ends up feeling like a poor design because the lifetimes can't be independent, but they are independent by default.

All of my hosted services end up tying their lifetime to the application lifetime.

finally
{
  _hostApplicationLifetime.StopApplication();
}
2
  • Do you see any benefits in using Host.StartAsync and IHostApplicationLifetime at all for a simple console application (which is executed once and not a hosted service)? Commented Aug 18, 2022 at 16:10
  • 3
    Sometimes I use a full host stack (worker service template) even if it just runs and exits, if I want enough of the DI/config/etc parts of the framework. It's a judgement call. Commented Aug 18, 2022 at 23:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.