A hosted startup batch for .NET developers who implement once-per-start background work.
Use a .NET 10 console project.
dotnet new console -f net10.0 -n PackageDemo
cd PackageDemo
dotnet add package DKNet.AspCore.Tasks
Replace Program.cs with the example, then run dotnet run.
Add Microsoft.Extensions.Hosting to the console project as well (dotnet add package Microsoft.Extensions.Hosting).
using DKNet.AspCore.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
var builder = Host.CreateApplicationBuilder(args);
builder.Services.AddBackgroundJob<PrintTask>();
await builder.Build().RunAsync();
public sealed class PrintTask : IBackgroundTask
{
public Task RunAsync(CancellationToken cancellationToken = default)
{
Console.WriteLine("Startup task ran.");
return Task.CompletedTask;
}
}
The host prints Startup task ran. once. Stop it with Ctrl+C; task completion does not stop the host.
One hosted service owns one async scope for all tasks. Tasks share scoped dependencies and run concurrently; this is neither a scheduler nor a readiness gate.
There is no ordering guarantee or automatic retry. The hostβs stopping token reaches every task. Task completion leaves the application host running.