Smart.Channel 5.2.1

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

Smart.Channel

NuGet

Smart.Channel 是基于 System.Threading.Channels 的生产者/消费者通道封装库,支持有界通道、无界通道和依赖注入注册,兼容 .NET 8/9/10。

功能特性

  • 通过 ChannelOptions.BoundedChannelOptions.Unbounded 创建有界或无界通道。
  • 提供 WriteAsyncReadAsyncReadAllAsyncWaitToReadAsync 等异步 API。
  • 提供 TryWriteTryReadTryComplete 等同步 API。
  • AddSmartChannel 注册开放泛型 SmartChannel<T>,默认生命周期为 Singleton。

安装

dotnet add package Smart.Channel

基础使用

using Smart.Channel;

var channel = new SmartChannel<string>(ChannelOptions.Bounded(255));

await channel.WriteAsync("Hello", CancellationToken.None);
channel.TryWrite("World");

while (await channel.WaitToReadAsync(CancellationToken.None))
{
    while (channel.TryRead(out string? message))
    {
        Console.WriteLine($"收到:{message}");
    }
}

依赖注入

using Microsoft.Extensions.DependencyInjection;
using Smart.Channel;
using System.Threading.Channels;

IServiceCollection services = new ServiceCollection();

services.AddSmartChannel(ChannelOptions.Bounded(
    capacity: 1024,
    fullMode: BoundedChannelFullMode.Wait));
public class DataProcessor
{
    private readonly SmartChannel<EventData> _channel;

    public DataProcessor(SmartChannel<EventData> channel)
    {
        _channel = channel;
    }

    public Task ProduceAsync(EventData data, CancellationToken cancellationToken)
    {
        return _channel.WriteAsync(data, cancellationToken);
    }

    public async Task ConsumeAsync(CancellationToken cancellationToken)
    {
        await foreach (EventData data in _channel.ReadAllAsync(cancellationToken))
        {
            // 处理消费逻辑
        }
    }
}

注意事项

  • 无界通道不会限制堆积数量,生产速度高于消费速度时需要额外监控内存。
  • 有界通道容量必须大于 0。
  • Web 请求或后台服务中建议向异步 API 传入 CancellationToken
  • 调用 TryComplete 后不再接受新数据,但仍可消费已写入的数据。

Developed by zenglei

Product 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 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 is compatible.  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.

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.1 40 5/16/2026
5.2.0 128 2/17/2026
5.1.1 112 12/30/2025
5.1.0 314 11/11/2025
5.0.3 209 10/15/2025
5.0.2 214 9/8/2025
5.0.1 267 8/30/2025
5.0.0 224 8/10/2025
4.0.0 199 4/5/2025
3.0.2 223 3/16/2025
3.0.1 203 2/15/2025
3.0.0 189 2/15/2025
2.0.3 188 2/13/2025
2.0.2 200 2/9/2025
2.0.1 206 12/7/2024
2.0.0 195 11/26/2024
1.0.0.1 202 10/9/2024
1.0.0 208 9/24/2024