DKNet.AspNetCore.BackgroundJobs
9.0.38
dotnet add package DKNet.AspNetCore.BackgroundJobs --version 9.0.38
NuGet\Install-Package DKNet.AspNetCore.BackgroundJobs -Version 9.0.38
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="DKNet.AspNetCore.BackgroundJobs" Version="9.0.38" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="DKNet.AspNetCore.BackgroundJobs" Version="9.0.38" />
<PackageReference Include="DKNet.AspNetCore.BackgroundJobs" />
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add DKNet.AspNetCore.BackgroundJobs --version 9.0.38
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: DKNet.AspNetCore.BackgroundJobs, 9.0.38"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package DKNet.AspNetCore.BackgroundJobs@9.0.38
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=DKNet.AspNetCore.BackgroundJobs&version=9.0.38
#tool nuget:?package=DKNet.AspNetCore.BackgroundJobs&version=9.0.38
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
DKNet.AspNetCore.BackgroundJobs
A lightweight, easy-to-use library for managing background jobs that need to run when your ASP.NET Core application starts.
Features
- Simple API to register background jobs that execute on application startup
- Jobs run in scoped lifetime with proper dependency injection
- Automatic detection and registration of background jobs via assembly scanning
- Graceful error handling - errors in one job won't affect others
- Built on top of ASP.NET Core's IHostedService for proper lifecycle management
Installation
dotnet add package DKNet.AspNetCore.BackgroundJobs
Quick Start
1. Create a Background Job
Implement the IBackgroundJob
interface:
public class DataInitializationJob : IBackgroundJob
{
private readonly IMyService _service;
private readonly ILogger<DataInitializationJob> _logger;
public DataInitializationJob(IMyService service, ILogger<DataInitializationJob> logger)
{
_service = service;
_logger = logger;
}
public async Task RunAsync(CancellationToken cancellationToken = default)
{
_logger.LogInformation("Initializing data...");
await _service.InitializeAsync(cancellationToken);
_logger.LogInformation("Data initialization complete");
}
}
2. Register the Background Job
In your Program.cs
or startup configuration:
// Register a specific job
services.AddBackgroundJob<DataInitializationJob>();
// Or scan assemblies for all IBackgroundJob implementations
services.AddBackgroundJobFrom(new[] { typeof(Program).Assembly });
How It Works
When your application starts:
- The
BackgroundJobHost
(registered as a hosted service) executes all registered jobs in parallel - Each job runs in its own scoped context with proper dependency resolution
- Errors in individual jobs are caught and logged, ensuring other jobs still complete
- All jobs must finish before the host reports completion
Advanced Usage
Multiple Jobs
Register multiple jobs individually:
services.AddBackgroundJob<FirstJob>();
services.AddBackgroundJob<SecondJob>();
services.AddBackgroundJob<ThirdJob>();
Assembly Scanning
Automatically detect and register all IBackgroundJob
implementations:
// Scan current assembly
services.AddBackgroundJobFrom(new[] { Assembly.GetExecutingAssembly() });
// Scan multiple assemblies
services.AddBackgroundJobFrom(new[] {
typeof(Program).Assembly,
typeof(ExternalComponent).Assembly
});
Best Practices
- Keep jobs focused on a single responsibility
- Use dependency injection for services needed by your jobs
- Respect cancellation tokens for graceful shutdown
- Don't block the main thread with long-running synchronous operations
- Use logging to track job execution progress and issues
Compatibility
- .NET 9.0 and above
- Compatible with ASP.NET Core and any application using Microsoft's Generic Host
License
This project is licensed under the MIT License - see the LICENSE file for details.
About
Developed by Steven Hoang.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
net9.0
- Microsoft.Extensions.Hosting.Abstractions (>= 9.0.9)
- Scrutor (>= 6.1.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.