Sai.Messaging 1.0.2

dotnet add package Sai.Messaging --version 1.0.2
NuGet\Install-Package Sai.Messaging -Version 1.0.2
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="Sai.Messaging" Version="1.0.2" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Sai.Messaging --version 1.0.2
#r "nuget: Sai.Messaging, 1.0.2"
#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 Sai.Messaging as a Cake Addin
#addin nuget:?package=Sai.Messaging&version=1.0.2

// Install Sai.Messaging as a Cake Tool
#tool nuget:?package=Sai.Messaging&version=1.0.2

Sai.Messaging

Messaging utility library.

Usage

Publisher/Subscriber Channel

var messageService = new InProcessMessageService();
var pubSubChannel = messageService.GetPublisherSubscriberChannel<int>("my-topic"); // Creates or gets a channel for the topic
var reader1 = pubSubChannel.Subscribe();
var reader2 = pubSubChannel.Subscribe();

var writerTask = Task.Run(async () =>
{
    for (int i = 0; i < MESSAGE_COUNT; i++)
    {
        await pubSubChannel.WriteAsync(i);
    }
});

var readerTask1 = Task.Run(async () =>
{
    int value;
    do
    {
        value = await reader1.ReadAsync();
    } while (value < MESSAGE_COUNT - 1);
});

var readerTask2 = Task.Run(async () =>
{
    int value;
    do
    {
        value = await reader2.ReadAsync();
    } while (value < MESSAGE_COUNT - 1);
});

Queue Channel

var messageService = new InProcessMessageService();
var queue = messageService.GetQueueChannel<int>("my-queue"); // Creates or gets a queue by name

var writerTask = Task.Run(async () =>
{
    for (int i = 0; i < MESSAGE_COUNT; i++)
    {
        await queue.WriteAsync(i);
    }
});

var readerTask = Task.Run(async () =>
{
    int value;
    do
    {
        value = await queue.ReadAsync();
    } while (value < MESSAGE_COUNT - 1);
});

General Comments

Channel<T> is used for pub/sub and queue implementations as it is fast (see here: https://michaelscodingspot.com/performance-of-producer-consumer/, plus benchmarks in this project). Blocking implementations may be useful in some scenarios, to avoid async/await - however it was found that using BlockingCollection is significantly slower than channels (again, see benchmarks). ConcurrentQueue<T> is much faster than BlockingCollection<T>, however it has a very limited API: Enqueue(), TryDequeue() and TryPeek(), so adding timeout and cancellation would probably make it equivalent to BlockingCollection<T> anyway.

Product 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 was computed.  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. 
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
1.0.2 415 5/22/2022
1.0.1 384 5/22/2022
1.0.0 380 5/20/2022