BackgroundServiceBase 1.0.1

dotnet add package BackgroundServiceBase --version 1.0.1
                    
NuGet\Install-Package BackgroundServiceBase -Version 1.0.1
                    
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="BackgroundServiceBase" Version="1.0.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="BackgroundServiceBase" Version="1.0.1" />
                    
Directory.Packages.props
<PackageReference Include="BackgroundServiceBase" />
                    
Project file
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 BackgroundServiceBase --version 1.0.1
                    
#r "nuget: BackgroundServiceBase, 1.0.1"
                    
#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 BackgroundServiceBase@1.0.1
                    
#: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=BackgroundServiceBase&version=1.0.1
                    
Install as a Cake Addin
#tool nuget:?package=BackgroundServiceBase&version=1.0.1
                    
Install as a Cake Tool

BackgroundServiceBase

通用可控后台服务基类,适用于 Blazor、ASP.NET Core 等 .NET 项目。

安装

通过 NuGet 安装(发布后):

dotnet add package BackgroundServiceBase

用法示例

  1. 继承 BaseBackgroundService,实现你的后台任务逻辑:
using BackgroundServiceBase;
using Microsoft.Extensions.Logging;
using System.Threading;
using System.Threading.Tasks;

public class MyBackgroundService : BaseBackgroundService
{
    public MyBackgroundService(ILogger<MyBackgroundService> logger) : base(logger) { }

    protected override async Task ExecuteAsync(CancellationToken stoppingToken)
    {
        EnqueueLog("服务开始执行");
        while (!stoppingToken.IsCancellationRequested)
        {
            // 你的任务逻辑
            EnqueueLog("服务正在运行...");
            await Task.Delay(1000, stoppingToken);
        }
        EnqueueLog("服务已停止");
    }
}
  1. 在 DI 容器中注册你的服务(如 Blazor/ASP.NET Core):
builder.Services.AddSingleton<MyBackgroundService>();
  1. 在页面或组件中注入并控制服务:
@inject MyBackgroundService BackgroundService

<button @onclick="BackgroundService.StartAsync">启动</button>
<button @onclick="BackgroundService.StopAsync">停止</button>
<button @onclick="BackgroundService.RestartAsync">重启</button>

@foreach (var log in BackgroundService.GetLogs())
{
    <div>@log</div>
}

特性

  • 支持服务启动、停止、重启
  • 日志自动存储(最多1000条),可用于页面展示
  • 线程安全,适合高并发场景
  • 事件分发,支持日志实时推送

License

MIT

仓库

https://github.com/nygula/BackgroundServiceBlazorControlTest

Product 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.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.1 221 9/2/2025