Service or Program

In .NET, a “Windows Service” app is either an old-school SCM-first service that only runs properly when started by the Service Control Manager, or a modern “Generic Host” worker that can run as a console app in dev but also integrates with SCM in prod.


A .NET Framework classic service usually looks like this, and it will often refuse to run from the command line because it expects SCM to drive it:

SCM-first: this process expects to be started by the Service Control Manager.

If you run the exe directly, it often prints "Cannot start service from the command line..."

using System.ServiceProcess;

static class Program
{
    static void Main()
    {
        ServiceBase.Run(new ServiceBase[] { new MyWindowsService() });
    }
}


A modern .NET worker service typically looks like this, and it can run as a normal process (console) but will “attach” to SCM when installed as a Windows Service:

using Microsoft.Extensions.Hosting;

public class Program
{
    public static void Main(string[] args)
    {
        Host.CreateDefaultBuilder(args)
		        // When started by SCM: wire up start/stop/status callbacks.
            .UseWindowsService() 
            // When run from terminal: usually just runs like a console app.
            .ConfigureServices(services =>
            {
		            // Your long-running background loop.
                services.AddHostedService<MyWorker>();
            })
            .Build()
            .Run();
    }
}