jinzaz.CAP.MQTT
2.0.0
dotnet add package jinzaz.CAP.MQTT --version 2.0.0
NuGet\Install-Package jinzaz.CAP.MQTT -Version 2.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="jinzaz.CAP.MQTT" Version="2.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="jinzaz.CAP.MQTT" Version="2.0.0" />
<PackageReference Include="jinzaz.CAP.MQTT" />
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 jinzaz.CAP.MQTT --version 2.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: jinzaz.CAP.MQTT, 2.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 jinzaz.CAP.MQTT@2.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=jinzaz.CAP.MQTT&version=2.0.0
#tool nuget:?package=jinzaz.CAP.MQTT&version=2.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
jinzaz.CAP.MQTT
dotnetcore/CAP的MQTT扩展支持
基于 MQTTnet 5.x,适配 DotNetCore.CAP 10.x,支持 .NET 8 及以上。
PM> Install-Package jinzaz.CAP.MQTT
配置
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddCap(x =>
{
x.UseMySql("..."); // 或 UseSqlite / UseEntityFramework<AppDbContext>() 等任意 CAP 存储
x.UseMQTT(options =>
{
options.Server = "localhost";
options.Port = 1883;
options.UserName = "admin";
options.Password = "password";
options.ClientId = "order-service";
});
// 也可以直接绑定配置节:x.UseMQTT(o => builder.Configuration.GetSection("MQTT").Bind(o));
x.UseDashboard();
});
配置项
| 属性 | 默认值 | 说明 |
|---|---|---|
Server |
localhost |
Broker 地址 |
Port |
1883 |
Broker 端口 |
ClientId |
cap |
ClientId 前缀,实际为 {ClientId}-{pub/sub}-{随机},保证发布端和各消费端连接互不踢线 |
UserName / Password |
认证信息 | |
KeepAlivePeriod |
60s | 心跳周期 |
Timeout |
10s | 通讯超时 |
CleanSession |
true |
清除会话 |
ProtocolVersion |
V500 |
协议版本,共享订阅需要 MQTT 5 |
UseTls |
false |
启用 TLS |
QualityOfServiceLevel |
AtLeastOnce |
发布和订阅使用的 QoS |
UseSharedSubscription |
true |
以 $share/{group}/{topic} 订阅,实现 CAP 的分组语义(见下文) |
ConfigureClientOptions |
高级定制 MQTTnet 客户端选项(证书、WebSocket、遗嘱消息等) |
说明
- 分组语义:CAP 要求同一 Group 的多个实例竞争消费、不同 Group 各收一份。这依赖 MQTT 5 共享订阅,EMQX、HiveMQ、Mosquitto 2.x 等主流 Broker 均支持。如 Broker 不支持,需设置
UseSharedSubscription = false,此时每个实例都会收到全部消息(多实例部署会重复消费)。 - 可靠性:消息由 CAP 持久化后才会 ACK。存储失败时消息不会被 ACK;MQTT 没有 NACK,是否重投取决于 Broker(如 EMQX 的 in-flight 重试,或持久会话重连)。
- 断线重连:发布端在下次发送时自动重连;消费端断线后上报 CAP,由 CAP 的传输健康检查(30s)自动重建。
- 消息格式:Header 与 Body 以 JSON 信封传输,兼容 MQTT 3.1.x,也兼容 1.x 版本发出的消息。
从 1.x 升级
- 目标框架由
netstandard2.1改为net8.0(CAP 10 已不再支持 netstandard)。 - 移除
IConnectionChannelPool和ConnectionPoolSize,发布端改为单个线程安全的共享连接。 Port默认值由0改为1883;ClientId变为前缀;默认协议改为 MQTT 5 并启用共享订阅。- 移除 Newtonsoft.Json 依赖。
示例
examples/Sample.MQTT.Mysql:MySQL + ADO.NET(Dapper) 事务examples/Sample.MQTT.Sqlite:SQLite + EF Core 事务
启动 Broker 和 MySQL:
docker compose -f examples/docker-compose.yml up -d
| 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 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. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
net8.0
- DotNetCore.CAP (>= 10.0.2)
- MQTTnet (>= 5.2.0.1603)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.