MVFC.LongPolling
1.0.7
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 MVFC.LongPolling --version 1.0.7
NuGet\Install-Package MVFC.LongPolling -Version 1.0.7
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="MVFC.LongPolling" Version="1.0.7" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="MVFC.LongPolling" Version="1.0.7" />
<PackageReference Include="MVFC.LongPolling" />
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 MVFC.LongPolling --version 1.0.7
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: MVFC.LongPolling, 1.0.7"
#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 MVFC.LongPolling@1.0.7
#: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=MVFC.LongPolling&version=1.0.7
#tool nuget:?package=MVFC.LongPolling&version=1.0.7
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
MVFC.LongPolling
π§π· Leia em PortuguΓͺs
A lightweight and efficient long polling via Redis Pub/Sub library for .NET,
with configurable CancellationToken support and typed payload.
Objective
Allow HTTP clients to await asynchronous results (jobs, webhooks, processing) in a simple way, without blind polling or WebSockets, using the Redis Pub/Sub channel as the notification mechanism.
Features
- Redis Pub/Sub: Real subscription per channel, no key polling.
- Configurable timeout: Maximum wait time globally or per call via
LongPollingOptions. - CancellationToken: Automatically respects HTTP client disconnection.
- Typed payload:
WaitAsync<T>deserializes the result directly viaSystem.Text.Json. - Confirmed delivery:
PublishAsyncreturnsfalseif no subscriber was active. - Deterministic synchronization:
WaitUntilReadyAsyncguarantees the subscription is active before publishing. - Fluent configuration: Simple setup in
Program.cs. - Channel prefix: Environment isolation via
KeyPrefix.
Packages
| Package | Downloads |
|---|---|
| MVFC.LongPolling |
Installation
dotnet add package MVFC.LongPolling
Configuration
Basic
builder.Services.AddLongPolling("localhost:6379", cfg =>
{
cfg.DefaultTimeout = TimeSpan.FromSeconds(30);
cfg.KeyPrefix = "poll";
});
With existing IConnectionMultiplexer
builder.Services.AddLongPolling(existingMultiplexer, cfg =>
{
cfg.DefaultTimeout = TimeSpan.FromSeconds(20);
});
Usage
Await result (string)
app.MapGet("/poll/{jobId}", async (
string jobId,
ILongPollingService polling,
CancellationToken ct) =>
{
var result = await polling.WaitAsync(jobId, cancellationToken: ct);
return result is null
? Results.NoContent()
: Results.Ok(result);
});
Await typed result
app.MapGet("/poll/{jobId}/typed", async (
string jobId,
ILongPollingService polling,
CancellationToken ct) =>
{
var result = await polling.WaitAsync<OrderCompletedEvent>(jobId, cancellationToken: ct);
return result is null
? Results.NoContent()
: Results.Ok(result);
});
Notify job completion
app.MapPost("/notify/{jobId}", async (
string jobId,
NotifyRequest req,
ILongPollingService polling) =>
{
var delivered = await polling.PublishAsync(jobId, req.Payload);
return delivered
? Results.Accepted()
: Results.NotFound($"No active subscriber for channel '{jobId}'.");
});
Per-call options
var options = new LongPollingOptions(
Timeout: TimeSpan.FromSeconds(10),
KeyPrefix: "custom");
var result = await polling.WaitAsync(jobId, options, cancellationToken: ct);
Configuration Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
DefaultTimeout |
TimeSpan |
30 seconds |
Maximum wait time per message |
KeyPrefix |
string |
longpolling |
Redis channel prefix |
How It Works
HTTP Client Server Redis
βββββββββββ ββββββ βββββ
GET /poll/{id} βββΊ WaitAsync()
SubscribeAsync(channel) βββΊ SUBSCRIBE poll:id
WaitUntilReadyAsync() βββ (confirmed)
βββ Worker: PublishAsync()
message received βββ PUBLISH poll:id payload
GET returns βββ Results.Ok(payload)
Production Flow Example β Payment Scenario
Default flow β message received
HTTP Client Orders API Payments API
βββββββββββ ββββββββββ ββββββββββββ
POST /orders βββΊ Creates order
WaitAsync(orderId) βββΊ Processes payment
PublishAsync(orderId, "approved")
βββ "approved"
200 OK βββ Results.Ok(status)
Timeout β no payment response
HTTP Client Orders API Payments API
βββββββββββ ββββββββββ ββββββββββββ
POST /orders βββΊ Creates order
WaitAsync(orderId) βββΊ Processes payment
(waiting...) (no PublishAsync)
timeout reached
504 GW Timeout βββ Results.StatusCode(504)
Payment declined
HTTP Client Orders API Payments API
βββββββββββ ββββββββββ ββββββββββββ
POST /orders βββΊ Creates order
WaitAsync(orderId) βββΊ Processes payment
PublishAsync(orderId, "rejected")
βββ "rejected"
422 Unprocessable βββ Results.UnprocessableEntity(status)
Client disconnects before response
HTTP Client Orders API Payments API
βββββββββββ ββββββββββ ββββββββββββ
POST /orders βββΊ Creates order
WaitAsync(orderId) βββΊ Processes payment
β disconnects
CancellationToken cancelled
WaitAsync throws OperationCanceledException
(response discarded)
Publish with no active subscriber
HTTP Client Orders API Payments API
βββββββββββ ββββββββββ ββββββββββββ
PublishAsync(orderId, "approved")
delivered = false
(no active WaitAsync for orderId)
404 Not Found βββ Results.NotFound(...)
Project Structure
- src: Source code for the
MVFC.LongPollinglibrary. - playground: Sample API to validate behavior with Aspire.
- tests: Integration tests with Aspire + Redis.
Requirements
.NET 9.0+
Contributing
See CONTRIBUTING.md.
License
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | 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.
-
net10.0
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 10.0.3)
- Microsoft.Extensions.Logging.Abstractions (>= 10.0.3)
- Microsoft.Extensions.Options (>= 10.0.3)
- SonarAnalyzer.CSharp (>= 10.19.0.132793)
- StackExchange.Redis (>= 2.11.3)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.