SpawnDev.BackgroundServices
2.0.0
dotnet add package SpawnDev.BackgroundServices --version 2.0.0
NuGet\Install-Package SpawnDev.BackgroundServices -Version 2.0.0
<PackageReference Include="SpawnDev.BackgroundServices" Version="2.0.0" />
<PackageVersion Include="SpawnDev.BackgroundServices" Version="2.0.0" />
<PackageReference Include="SpawnDev.BackgroundServices" />
paket add SpawnDev.BackgroundServices --version 2.0.0
#r "nuget: SpawnDev.BackgroundServices, 2.0.0"
#:package SpawnDev.BackgroundServices@2.0.0
#addin nuget:?package=SpawnDev.BackgroundServices&version=2.0.0
#tool nuget:?package=SpawnDev.BackgroundServices&version=2.0.0
SpawnDev.BackgroundServices
Add support for auto-starting background services based on the running scope. Register services that start automatically in specific contexts - browser windows, web workers, desktop/server, or all of the above.
Getting Started
Add the NuGet package:
dotnet add package SpawnDev.BackgroundServices
Setup
using SpawnDev.BackgroundServices;
var builder = WebAssemblyHostBuilder.CreateDefault(args);
// Add the background service manager
builder.Services.AddBackgroundServiceManager();
// Register services that auto-start in ALL scopes
builder.Services.AddSingleton<IBackgroundService, MyService>();
// Register services that only auto-start in specific scopes
builder.Services.AddSingleton<IBackgroundService, MyWindowService>(GlobalScope.Window);
builder.Services.AddSingleton<IBackgroundService, MyWorkerService>(GlobalScope.AllWorkers);
builder.Services.AddSingleton<IBackgroundService, MyDesktopService>(GlobalScope.NonBrowser);
// Start all background services for the current scope
await builder.Build().Services.StartBackgroundServices();
Interfaces
IBackgroundService
Marker interface. Services implementing this are auto-started when StartBackgroundServices() is called, filtered by the registered GlobalScope.
public class MyService : IBackgroundService
{
public MyService(ILogger<MyService> logger)
{
logger.LogInformation("MyService started");
}
}
IAsyncBackgroundService
Extends IBackgroundService with a Task Ready property. The background service manager awaits Ready during startup, ensuring the service is fully initialized before other services that depend on it start.
public class MyAsyncService : IAsyncBackgroundService
{
public Task Ready { get; }
public MyAsyncService()
{
Ready = InitAsync();
}
private async Task InitAsync()
{
// async initialization
await Task.Delay(100);
}
}
IAsyncService
Interface with a Task Ready property. When resolved via GetServiceAsync<T>(), the container awaits Ready before returning the service.
GlobalScope
Flags enum controlling which scopes a service auto-starts in:
| Value | Description |
|---|---|
Window |
Browser window |
DedicatedWorker |
Dedicated web worker |
SharedWorker |
Shared web worker |
ServiceWorker |
Service worker |
NonBrowser |
Desktop/server (.NET) |
AllWorkers |
All worker types combined |
AllBrowser |
Window + all workers |
All |
Every scope |
None |
Never auto-start |
Targets
.NET 6.0, 7.0, 8.0, 9.0
License
MIT
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. 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 is compatible. 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. |
-
net10.0
- Microsoft.Extensions.DependencyInjection (>= 10.0.0)
-
net8.0
- Microsoft.Extensions.DependencyInjection (>= 8.0.1)
-
net9.0
- Microsoft.Extensions.DependencyInjection (>= 9.0.11)
NuGet packages (2)
Showing the top 2 NuGet packages that depend on SpawnDev.BackgroundServices:
| Package | Downloads |
|---|---|
|
SpawnDev.BlazorJS
Full Blazor WebAssembly and Javascript interop. Supports all Javascript data types and web browser APIs. Create, access properties, call methods, and add/remove event handlers of Javascript objects the .Net way without writing Javascript. |
|
|
SpawnDev.SpawnJS
SpawnJS provides Javascript interop for .Net WebAssembly. |
GitHub repositories (1)
Showing the top 1 popular GitHub repositories that depend on SpawnDev.BackgroundServices:
| Repository | Stars |
|---|---|
|
LostBeard/SpawnDev.BlazorJS
Full Blazor WebAssembly and Javascript interop. Supports all Javascript data types and web browser APIs.
|