XYS.Worker
3.0.0
.NET 8.0
This package targets .NET 8.0. The package is compatible with this framework or higher.
.NET Core 3.1
This package targets .NET Core 3.1. The package is compatible with this framework or higher.
.NET Standard 2.0
This package targets .NET Standard 2.0. The package is compatible with this framework or higher.
.NET Framework 4.6.2
This package targets .NET Framework 4.6.2. The package is compatible with this framework or higher.
dotnet add package XYS.Worker --version 3.0.0
NuGet\Install-Package XYS.Worker -Version 3.0.0
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="XYS.Worker" Version="3.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="XYS.Worker" Version="3.0.0" />
<PackageReference Include="XYS.Worker" />
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 XYS.Worker --version 3.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: XYS.Worker, 3.0.0"
#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 XYS.Worker@3.0.0
#: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=XYS.Worker&version=3.0.0
#tool nuget:?package=XYS.Worker&version=3.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
XYS.Worker
后台任务服务组件:基于 BlockingCollection 的生产者-消费者线程池,提供入队接口 Handler、抽象消费方法 Handle、异常兜底钩子 OnError 与优雅停机 Stop。
- 目标框架:
net8.0/net462/netcoreapp3.1/netstandard2.0/netstandard2.1 - 依赖:
XYS(同解决方案项目引用)
快速使用
1. 定义工作数据
复用内置的 WorkData(键值参数字典,默认已初始化),或实现 IWorkData 自定义结构:
using XYS.Worker;
// 方式 A:直接用 WorkData
var data = new WorkData();
data.KeyParams["patientId"] = "P00001";
data.KeyParams["reportId"] = 123L;
// 方式 B:自定义
public class LabPrintWorkData : IWorkData
{
public string ReportId { get; set; }
public int Copies { get; set; }
}
2. 继承 BaseWorker 实现消费者
using System;
using XYS.Worker;
public sealed class LabPrintWorker : BaseWorker
{
public LabPrintWorker(int workerCount)
{
WorkerCount = workerCount; // 消费线程数
LoggerName = "LabPrintWorker"; // 用于线程名与 Trace 前缀
ActualType = typeof(LabPrintWorker); // 可选:供反射/日志使用
InitWorker(); // 启动线程池
}
protected override void Handle(IWorkData data)
{
var wd = (WorkData)data;
string reportId = (string)wd.KeyParams["reportId"];
// ... 打印逻辑
}
// 可选:接入项目日志系统(默认走 System.Diagnostics.Trace)
protected override void OnError(IWorkData data, Exception ex)
{
// 例如:LogManager.GetLogger(LoggerName).Error(ex);
base.OnError(data, ex);
}
}
3. 生产者入队
_worker.Handler(data); // data 为 null 或 Worker 已停止时静默丢弃
4. 优雅停机
宿主进程停止时(Windows Service OnStop / Topshelf WhenStopped / IIS 应用退出 / ASP.NET Core IHostApplicationLifetime.ApplicationStopping)显式调用:
_worker.Stop(); // 默认最多等待 5 秒
_worker.Stop(TimeSpan.FromSeconds(30)); // 自定义超时
Stop 语义:停止入队 → 消费完队列剩余项 → 超时后取消。重复调用无副作用。
关键契约
| 成员 | 语义 |
|---|---|
Handler(IWorkData) |
生产者入队;null 或已停止时静默丢弃 |
Handle(IWorkData) |
子类实现的处理逻辑;抛异常不会杀线程,会走 OnError |
OnError(IWorkData, Exception) |
异常兜底钩子,默认输出到 Trace,子类可重写 |
InitWorker() |
启动线程池,幂等;子类构造完成后调用 |
Stop(TimeSpan?) |
优雅停机,幂等;宿主停止时调用 |
WorkerCount |
线程数,≤0 时按 1 处理 |
LoggerName |
用于 Trace 前缀与线程名 |
ActualType |
基类不使用,仅暴露给子类做反射/日志 |
3.0.0 变更说明
- 目标框架增
net8.0,移除已 EOL 的netcoreapp2.0 - 依赖从 NuGet 包
XYS 2.1.0.9改为同解决方案的ProjectReference,避免版本漂移 - 消费循环由
ConcurrentQueue + Thread.Sleep(1000)轮询改为BlockingCollection.GetConsumingEnumerable:空闲无 1 秒延迟,无线程空转 Handle异常不再杀消费者线程:单条数据的业务异常走OnError,其他数据继续处理- 新增
Stop(TimeSpan?)支持优雅停机 - 构造函数初始化内部字段,消除子类忘记初始化导致的 NRE
InitWorker/Stop幂等,重复调用安全- 破坏性变更:移除
protected ConcurrentQueue<IWorkData> RequestQueue与protected List<Thread> ThreadPool属性(封装为 private)。若外部子类曾直接读写这两个成员,需改用Handler入队、Handle消费的官方入口
建议
WorkerCount按业务量设置:I/O 密集型(打印、HTTP、数据库)可设 CPU 核数的 2~4 倍;CPU 密集型接近核数即可OnError尽量接入项目日志:默认Trace在生产环境很少被采集Handle内部尽量短平快:长耗时任务应拆分或换成异步 IO,避免单条数据长时间占用消费者线程
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. 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 was computed. 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. |
| .NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 is compatible. |
| .NET Standard | netstandard2.0 is compatible. netstandard2.1 is compatible. |
| .NET Framework | net461 was computed. net462 is compatible. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen40 was computed. tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
NuGet packages (2)
Showing the top 2 NuGet packages that depend on XYS.Worker:
| Package | Downloads |
|---|---|
|
XYS.Worker.LabReport2Mongo
Description |
|
|
XYS.Worker.HosReport2Mongo
Description |
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 3.0.0 | 92 | 8/17/2026 |
| 2.1.0.5 | 302 | 5/12/2024 |
| 2.1.0.4 | 262 | 5/12/2024 |
| 2.1.0.3 | 636 | 5/12/2022 |
| 2.1.0.2 | 668 | 1/23/2022 |
| 2.1.0.1 | 638 | 1/23/2022 |
| 2.0.1.3 | 477 | 12/22/2021 |
| 2.0.1.2 | 2,281 | 6/1/2021 |
| 2.0.1.1 | 2,073 | 4/22/2021 |
| 2.0.0.2 | 547 | 4/22/2021 |
| 2.0.0.1 | 544 | 4/22/2021 |
| 1.0.1.1 | 729 | 7/3/2020 |
| 1.0.0.10 | 689 | 5/27/2020 |