Smart.Channel
2.0.3
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 2.0.3
NuGet\Install-Package Smart.Channel -Version 2.0.3
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="2.0.3" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Smart.Channel --version 2.0.3
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Smart.Channel, 2.0.3"
#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.
// Install Smart.Channel as a Cake Addin #addin nuget:?package=Smart.Channel&version=2.0.3 // Install Smart.Channel as a Cake Tool #tool nuget:?package=Smart.Channel&version=2.0.3
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 channel = new SmartChannelService<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
// 注册服务
services.AddSmartChannel();
// 注入使用
public class DataProcessor(SmartChannelService<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 |
异步 | 推荐消费端使用 |
TryRead |
同步 | 立即返回读取状态 |
WaitToReadAsync |
异步 | 节能型数据等待机制 |
最佳实践
1. Web 应用场景
csharp
public class BackgroundConsumer : IHostedService
{
private readonly SmartChannelService<LogData> _channel;
public BackgroundConsumer(SmartChannelService<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. |
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.2)
-
net8.0
- Microsoft.Extensions.DependencyInjection (>= 9.0.2)
-
net9.0
- Microsoft.Extensions.DependencyInjection (>= 9.0.2)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.