What does `dotnet new worker` generate in .NET 10, and how does a Worker Service project differ from a plain console app?
answer
- a console app that boots a host
- a different project SDK
- CreateApplicationBuilder, AddHostedService, Run
- JSON files copied to output
- DOTNET_ENVIRONMENT in the launch profile
basics
~10 sA Worker Service is a console executable that boots the .NET generic host: Program.cs calls Host.CreateApplicationBuilder, registers a Worker class with AddHostedService and runs the host, in a project built on the Microsoft.NET.Sdk.Worker SDK.
solid answer
~40 s`dotnet new worker` creates a project on the `Microsoft.NET.Sdk.Worker` SDK with a `PackageReference` to `Microsoft.Extensions.Hosting`, a `Program.cs` that calls `Host.CreateApplicationBuilder(args)`, `builder.Services.AddHostedService<Worker>()`, `Build()` and `Run()`, a `Worker` class deriving from `BackgroundService`, `appsettings.json` and `appsettings.Development.json`, and a launch profile that sets `DOTNET_ENVIRONMENT=Development`. Compared with a plain `Microsoft.NET.Sdk` console app, the Worker SDK copies `*.json` and `*.config` files to the build and publish output, adds implicit usings for the Extensions configuration, DI, hosting and logging namespaces, and makes `dotnet run` start in the project folder. At run time you get the generic host: configuration, logging, DI and a console lifetime that waits for Ctrl+C or SIGTERM. Running it as a Windows Service or systemd unit takes a separate package and one call.
code
csharp · 7 linesusing QueueConsumer;
var builder = Host.CreateApplicationBuilder(args);
builder.Services.AddHostedService<Worker>();
var host = builder.Build();
host.Run();go deeper
Name the pieces: Microsoft.NET.Sdk.Worker, Host.CreateApplicationBuilder, AddHostedService<Worker>, Build and Run, appsettings files and the launch profile that sets the environment.
Explain what the Worker SDK adds over a console app, especially that it copies JSON configuration to the output, and what the generic host wires before your code runs.
Show you know the template is only a starting point: a worker becomes a Windows Service or systemd unit through a package and one call, and its content root depends on where it is started.
Treat the worker as a deployable unit: one binary that a team can run as a console process, a container, a Windows Service or a systemd unit with identical configuration and logging.
## What a Worker Service is A **Worker Service** is the name of a project template, not a special kind of process. It is an ordinary .NET console executable whose `Program.cs` builds the **generic host** from `Microsoft.Extensions.Hosting` and asks it to run one or more **hosted services** (classes implementing `IHostedService`). The host gives the app the same plumbing a web app gets from ASP.NET Core: configuration from files and environment variables, logging, dependency injection and a lifetime that reacts to Ctrl+C and SIGTERM. Nothing in it requires ASP.NET Core or Kestrel; the template references only `Microsoft.Extensions.Hosting` and runs on the base .NET runtime. ## What `dotnet new worker` generates The SDK 10.0.401 template (short name `worker`) produces: - **The project file**, declaring `<Project Sdk="Microsoft.NET.Sdk.Worker">`, targeting `net10.0`, with `Nullable` and `ImplicitUsings` enabled, a `UserSecretsId`, and a `PackageReference` to `Microsoft.Extensions.Hosting` version 10.0.12. - **`Program.cs`**, four statements long: `Host.CreateApplicationBuilder(args)`, `builder.Services.AddHostedService<Worker>()`, `builder.Build()` and `host.Run()`. - **`Worker.cs`**, a class deriving from `BackgroundService` whose `ExecuteAsync` logs the time once a second until the stopping token fires. How to design that loop is covered with background services in the ASP.NET Core material; here it is just the sample payload. - **`appsettings.json` and `appsettings.Development.json`**, both setting `Logging:LogLevel` for `Default` and `Microsoft.Hosting.Lifetime`. - **`Properties/launchSettings.json`**, one profile that sets `DOTNET_ENVIRONMENT` to `Development` when the app is started with `dotnet run` or an IDE. Template options exist for the edges: `--aot` adds `PublishAot` and `InvariantGlobalization` to the project, `--use-program-main` writes an explicit `Program` class with `Main`, `--exclude-launch-settings` drops the launch profile and `--framework` picks the target framework. ## What the Worker SDK changes `Microsoft.NET.Sdk.Worker` layers a few defaults over the plain `Microsoft.NET.Sdk`: | Concern | `Microsoft.NET.Sdk` console app | `Microsoft.NET.Sdk.Worker` | |---|---|---| | `OutputType` | must be set to `Exe` | set to `Exe` by the SDK | | `appsettings*.json` | a `None` item, not copied to `bin/` | `Content` with `CopyToOutputDirectory="PreserveNewest"`, also published | | `ExcludeFromSingleFile` on JSON/config | not set | `true`, so the files stay next to a single-file executable | | Implicit usings | `System.*` only | adds `Microsoft.Extensions.Configuration`, `.DependencyInjection`, `.Hosting`, `.Logging` | | `dotnet run` working directory | the SDK default | `RunWorkingDirectory` set to the project folder | | `Properties/` (launch profile) | n/a | never copied to output or publish | The JSON row is the one that bites people who start from `dotnet new console`: their `appsettings.json` never reaches `bin/`, and an optional JSON source silently loads nothing. ## What happens when you run it 1. `Host.CreateApplicationBuilder(args)` sets the content root to the current directory, loads `DOTNET_`-prefixed environment variables and the command line as host configuration, then `appsettings.json`, `appsettings.{Environment}.json`, user secrets in Development, environment variables and the command line as app configuration. 2. It registers the Console, Debug and EventSource logging providers (plus EventLog on Windows) and the console lifetime. 3. `Build()` creates the service provider; `Run()` starts every hosted service, logs "Application started. Press Ctrl+C to shut down." with the environment and content root, and blocks until a shutdown signal arrives. ## What the template leaves to you - **Service hosting**: it is a console process. Running under the Windows Service Control Manager needs `Microsoft.Extensions.Hosting.WindowsServices` and `AddWindowsService()`; running as a systemd unit needs `Microsoft.Extensions.Hosting.Systemd` and `AddSystemd()`. - **Garbage collector mode**: the template does not turn on server GC; choosing GC settings is covered with the garbage collector. - **Deployment**: containers, publish profiles and installers are separate decisions. ## From template to a real worker The first changes a team usually makes are small and stay inside the host's model: 1. Replace the sample `Worker` with the real consumer and register its dependencies on `builder.Services`. 2. Add settings to `appsettings.json`, override them per environment with `appsettings.Production.json` or environment variables, and remember that `DOTNET_ENVIRONMENT` is what names the environment for a worker. 3. Tune `Logging:LogLevel` so the `Microsoft.Hosting.Lifetime` lines and the worker's own categories log at the level operations wants. 4. Add the Windows Service or systemd package and its single call when the deployment target is known; the call is a no-op elsewhere, so the same build still runs from a terminal. One process can host several hosted services: each `AddHostedService<T>()` adds another, and the host starts them in registration order. The practical summary for an interview: a Worker Service is a console app plus the generic host plus a project SDK that ships its configuration files, and the same binary can later be wrapped as a Windows Service or systemd unit without changing what it does.
- What does `dotnet new worker --aot` change compared with the default template?It adds `<PublishAot>true</PublishAot>` and `<InvariantGlobalization>true</InvariantGlobalization>` to the project file; `Program.cs` and `Worker.cs` stay the same. The app still runs under the JIT during development, and a Native AOT publish then produces a self-contained native executable. Whether that trade is worth it for a long-running worker belongs with the JIT and AOT material.
- Does a Worker Service depend on ASP.NET Core?No. The template references only the `Microsoft.Extensions.Hosting` package and runs on the base `Microsoft.NETCore.App` runtime, so its container image can be the plain runtime image. ASP.NET Core's `WebApplication` is built on the same generic host, which is why configuration, logging and DI look identical in both.
- A Worker Service is a Windows Service template and only runs on Windows.
- Hosting background work in .NET requires ASP.NET Core and Kestrel.
- dotnet new worker builds its host with WebApplication.CreateBuilder.
- appsettings.json is read from the project folder, so copying it to output never matters.
- The Worker template switches on server garbage collection by default.