Qishao.Worker.MQRedisWorker 2026.8.7

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

Qishao.Worker.MQRedisWorker

Qishao.Worker.MQRedisWorker 提供 Redis Streams 后台消费循环、并发处理和消息租约自动续期。

安装

dotnet add package Qishao.Worker.MQRedisWorker

支持 netstandard2.1net6.0net8.0

实现 Handler

using Qishao.MQ.Abstractions;

public sealed class OrderHandler : IMessageHandler
{
    public async ValueTask<ConsumeDisposition> HandleAsync(
        ConsumeContext context,
        CancellationToken cancellationToken)
    {
        await ProcessAsync(context.Message, cancellationToken);
        return ConsumeDisposition.Complete;
    }

    private static Task ProcessAsync(
        MessageEnvelope message,
        CancellationToken cancellationToken)
    {
        return Task.CompletedTask;
    }
}

注册 Worker

services.AddMQRedisWorker<OrderHandler>(options =>
{
    options.Topic = "orders";
    options.GroupName = "billing";
    options.ChannelCount = 4;
    options.PullCount = 4;
    options.MaxParallelism = 4;
    options.VisibilityTimeout = TimeSpan.FromMinutes(5);
    options.LeaseRenewalInterval = TimeSpan.FromMinutes(1);
});

同一个服务可以注册多个不同 Handler。每个 Handler 使用独立 Options,不会互相覆盖。

配置

配置 默认值 说明
Topic 必填 Redis Stream Topic。
GroupName DefaultGroup Consumer Group 名称。
ChannelCount CPU 核心数 独立消费通道数。
PullCount 4 单次最多拉取消息数,不得高于 MaxParallelism
MaxParallelism 4 每个通道的最大并行处理数。
VisibilityTimeout 30 分钟 超过此时间未确认的消息可以被重新接管。
LeaseRenewalInterval 10 分钟 处理期间的续租间隔,必须小于可见性超时。
PullTimeInterval 250 毫秒 两次拉取之间的等待时间。
Debug false 启用后只使用一个通道且每次拉取一条。

处理流程

  1. Worker 创建或复用消费组。
  2. 每个通道使用稳定的消费者 ID 拉取消息。
  3. Handler 处理期间,Worker 按 LeaseRenewalInterval 自动续租。
  4. Handler 返回 CompleteRetryDeadLetter
  5. Worker 将结果交给 IMessageConsumer.SettleAsync 结算,并检查结算阶段是否丢失租约。

续租返回 false 或发生异常时,Worker 会取消 Handler 的 CancellationToken,并且不再结算该消息,避免失去所有权后误 ACK。

Handler 抛出普通异常时按 Retry 处理。业务需要立即死信时,应捕获对应异常并返回 DeadLetter

可靠性建议

  • Handler 必须使用 MessageEnvelope.Id 或业务唯一键保证幂等。
  • LeaseRenewalInterval 建议不超过 VisibilityTimeout 的三分之一。
  • PullCount 必须小于或等于 MaxParallelism,避免消息进入 PEL 后等待处理期间无法及时续租。
  • Handler 必须响应取消令牌;租约丢失后继续执行可能造成并发副作用。
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 is compatible.  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 netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen 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

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
2026.8.7 103 8/7/2026
2026.7.22 109 7/22/2026
2026.7.21 109 7/22/2026