Sai.Messaging
1.0.3
dotnet add package Sai.Messaging --version 1.0.3
NuGet\Install-Package Sai.Messaging -Version 1.0.3
<PackageReference Include="Sai.Messaging" Version="1.0.3" />
paket add Sai.Messaging --version 1.0.3
#r "nuget: Sai.Messaging, 1.0.3"
// Install Sai.Messaging as a Cake Addin #addin nuget:?package=Sai.Messaging&version=1.0.3 // Install Sai.Messaging as a Cake Tool #tool nuget:?package=Sai.Messaging&version=1.0.3
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 | 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 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. |
-
net6.0
- System.Threading.Channels (>= 6.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.