Smart.Channel
5.0.0
There is a newer version of this package available.
See the version list below for details.
See the version list below for details.
dotnet add package Smart.Channel --version 5.0.0
NuGet\Install-Package Smart.Channel -Version 5.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="Smart.Channel" Version="5.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Smart.Channel" Version="5.0.0" />
<PackageReference Include="Smart.Channel" />
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 Smart.Channel --version 5.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Smart.Channel, 5.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 Smart.Channel@5.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=Smart.Channel&version=5.0.0
#tool nuget:?package=Smart.Channel&version=5.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
Smart.Channel
基于 System.Threading.Channels 封装的轻量级生产者-消费者模式实现库,提供同步/异步 API 和依赖注入支持。
功能特性
- 通道类型:可通过配置使用有界或者无界通道
- DI 集成:
AddSmartChannel扩展方法服务一键注册,在服务注册时可使用通道选项配置通道类型 - 双模式操作:同步(
TryWrite/TryRead) + 异步(WriteAsync/ReadAsync) - 单例服务:默认注册为 Singleton 生命周期
- 跨平台: 支持.NET 6/8/9
安装
bash
dotnet add package Smart.Channel
基础使用
csharp
// 初始化有界通道
var channel1 = new SmartBoundedChannel<string>(255); // 指定有界通道容量为255
// 初始化无界通道
var channel1 = new SmartUnboundedChannel<string>(); // 无界通道容量大小不受控制
// 生产者写入
await channel.WriteAsync("Hello", CancellationToken.None); // 异步写入
channel.TryWrite("World"); // 同步写入
// 消费者读取
while (await channel.WaitToReadAsync(CancellationToken.None))
{
if (channel.TryRead(out var msg)) // 同步读取
{
Console.WriteLine($"收到:{msg}");
}
var asyncMsg = await channel.ReadAsync(CancellationToken.None); // 异步读取
}
依赖注入集成
csharp
// 注册服务
ChannelOptions options = new ChannelOptions(); // 指定通道类型及有界通道容量
services.AddSmartChannel(options);
// 注入使用
public class DataProcessor(SmartChannel<EventData> channel)
{
public async Task ProduceEvent()
{
await channel.WriteAsync(new EventData(), CancellationToken.None);
}
[HostedService]
public async Task ConsumeEvents()
{
while (await channel.WaitToReadAsync())
{
// 消费逻辑
}
}
}
方法说明
| 方法 | 模式 | 说明 |
|---|---|---|
WriteAsync |
异步 | 推荐生产端使用,避免线程阻塞 |
TryWrite |
同步 | 立即返回写入状态 |
ReadAsync |
异步 | 推荐消费端使用 |
ReadAllAsync |
异步 | 推荐消费端使用 |
TryRead |
同步 | 立即返回读取状态 |
WaitToReadAsync |
异步 | 节能型数据等待机制 |
TryComplete |
同步 | 生产或者消费完成,关闭通道 |
最佳实践
1. Web 应用场景
csharp
public class BackgroundConsumer : IHostedService
{
private readonly SmartChannel<LogData> _channel;
public BackgroundConsumer(SmartChannel<LogData> channel) => _channel = channel;
public async Task StartAsync()
{
while (await _channel.WaitToReadAsync())
{
// 持久化到数据库
}
}
}
2. 性能优化
csharp
var channel = Channel.CreateUnbounded<T>(new UnboundedChannelOptions
{
SingleWriter = true, // 单一写入者模式
SingleReader = true // 单一读取者模式
});
注意事项
- 内存管理:无界通道需监控内存增长,建议生产端控制写入频率
- 同步方法:需处理
TryWrite/TryRead的返回值(可能返回 false) - 任务泄露:Web 请求中应传播
CancellationToken - 阻塞操作:单例服务中建议全链路异步操作
Developed by zenglei
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | 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 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.
-
net6.0
- Microsoft.Extensions.DependencyInjection (>= 9.0.0)
-
net8.0
- Microsoft.Extensions.DependencyInjection (>= 9.0.0)
-
net9.0
- Microsoft.Extensions.DependencyInjection (>= 9.0.0)
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 |
|---|---|---|
| 5.2.0 | 94 | 2/17/2026 |
| 5.1.1 | 96 | 12/30/2025 |
| 5.1.0 | 302 | 11/11/2025 |
| 5.0.3 | 196 | 10/15/2025 |
| 5.0.2 | 199 | 9/8/2025 |
| 5.0.1 | 254 | 8/30/2025 |
| 5.0.0 | 209 | 8/10/2025 |
| 4.0.0 | 190 | 4/5/2025 |
| 3.0.2 | 206 | 3/16/2025 |
| 3.0.1 | 193 | 2/15/2025 |
| 3.0.0 | 179 | 2/15/2025 |
| 2.0.3 | 177 | 2/13/2025 |
| 2.0.2 | 189 | 2/9/2025 |
| 2.0.1 | 198 | 12/7/2024 |
| 2.0.0 | 178 | 11/26/2024 |
| 1.0.0.1 | 186 | 10/9/2024 |
| 1.0.0 | 199 | 9/24/2024 |