LognPoolingLib 1.3.0

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

A simple implementation of long pooling for asp.net with an example. LognPoolingLib is actually the library itself, WebApplicationTestLongPooling is an example of usage.

Usage example: Creating a client.

var url = "http://localhost:5000/loongPooling";
var key = Encoding.UTF8.GetBytes("1111111111111111");
var iv = Encoding.UTF8.GetBytes("1111111111111111");
var client = new LongPoolingClient(key, iv);

The client is authorized using the request header. It is recommended to put an encrypted token string in this header. The example shows the simplest authorization, it should not be used in a production environment.

var headers = new Dictionary<string, string>();
headers.Add("user", "user");
headers.Add("Accept", "text/message");
client.SetHeaders(headers);

Client

var url = "http://localhost:5000/loongPooling";
var key = Encoding.UTF8.GetBytes("1111111111111111");
var iv = Encoding.UTF8.GetBytes("1111111111111111");

var client = new LongPoolingClient(key, iv);

var headers = new Dictionary<string, string>();
headers.Add("user", "user");
headers.Add("Accept", "text/message");

client.SetHeaders(headers);

client.MessageDelivered += (m) =>
{
    Console.WriteLine($"{m.Channel} {m.Text}");
};

client.SubscribeToChannel("channel1", (m) => Console.WriteLine($"the Channel = channel1, {m}"));
client.SubscribeToChannel("channel2", (m) => Console.WriteLine($"the Channel = channel2, {m}"));
client.SubscribeToChannel("channel3", (m) => Console.WriteLine($"the Channel = channel2, {m}"));

client.Start(url);

while (true) 
{ 
    await Task.Delay(1000);
}

Server: long pooling constroller

[Route("[controller]")]
[ApiController]
public class LoongPoolingController : ControllerBase, IDisposable
{
    private readonly MessageQueuePool messageQueuePool;
    UserCancellationTokenSource cancellationTokenSource;
    CancellationTokenSource getCancellationTokenSource;

    public LoongPoolingController(MessageQueuePool message)
    {
        getCancellationTokenSource = new CancellationTokenSource();
        this.messageQueuePool = message;

        this.messageQueuePool.AddMessageEvent += (m) =>
        {
            cancellationTokenSource?.Cancel(m.User);
        };
    }

    [HttpGet]
    public async IAsyncEnumerable<string> GetNew()
    {
        var key = Encoding.UTF8.GetBytes("1111111111111111");
        var iv = Encoding.UTF8.GetBytes("1111111111111111");
        var cripto = new Cripto<MessageDTO>(key, iv);
        var user = HttpContext.Request.Headers["user"];
        cancellationTokenSource = new UserCancellationTokenSource(user);

        while (!getCancellationTokenSource.IsCancellationRequested)
        {
            if (this.HttpContext.RequestAborted.IsCancellationRequested)
            {
                getCancellationTokenSource.Cancel();
            }

            var count = this.messageQueuePool.Count(user);
            if (count > 0)
            {
                var longPoolingServiceMessage = this.messageQueuePool.Dequeue(user);
                var messageDTO = (MessageDTO)longPoolingServiceMessage;

                var encryptObject = cripto.EncryptObject(messageDTO);
                yield return encryptObject.PadRight(1024, new char());                    
            }
            else
            {
                try
                {
                    await Task.Delay(10000, cancellationTokenSource.Token);
                }
                catch (TaskCanceledException e)
                {
                    cancellationTokenSource = new UserCancellationTokenSource(user);
                }
            }                
        }
    }

    public void Dispose()
    {
        cancellationTokenSource.Cancel();
        getCancellationTokenSource.Cancel();
        cancellationTokenSource.Dispose();
        getCancellationTokenSource.Dispose();
    }
}

Adding a message to the queue

MessageQueuePool message;
message.Enqueue(new Message() { 
    Channel = "Channel1", 
    Text = "Text", 
    User = "User"
});        
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 was computed.  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.
  • net8.0

    • No dependencies.

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.3.0 300 11/30/2025