MVFC.Messaging.Nats.IO 2.0.2

There is a newer version of this package available.
See the version list below for details.
dotnet add package MVFC.Messaging.Nats.IO --version 2.0.2
                    
NuGet\Install-Package MVFC.Messaging.Nats.IO -Version 2.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="MVFC.Messaging.Nats.IO" Version="2.0.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="MVFC.Messaging.Nats.IO" Version="2.0.2" />
                    
Directory.Packages.props
<PackageReference Include="MVFC.Messaging.Nats.IO" />
                    
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 MVFC.Messaging.Nats.IO --version 2.0.2
                    
#r "nuget: MVFC.Messaging.Nats.IO, 2.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.
#:package MVFC.Messaging.Nats.IO@2.0.2
                    
#: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.Messaging.Nats.IO&version=2.0.2
                    
Install as a Cake Addin
#tool nuget:?package=MVFC.Messaging.Nats.IO&version=2.0.2
                    
Install as a Cake Tool

MVFC.Messaging.Nats.IO

🇧🇷 Leia em Português

CI codecov License Platform NuGet Version NuGet Downloads

A .NET messaging provider for NATS, built on top of MVFC.Messaging.Core. Provides NatsPublisher<T> and NatsConsumer<T> for publishing and consuming JSON-serialized messages on NATS subjects with the official NATS.Client.Core library.

Package

Package Downloads
MVFC.Messaging.Nats.IO Downloads

Installation

dotnet add package MVFC.Messaging.Nats.IO

This package depends on MVFC.Messaging.Core (installed automatically) and NATS.Client.Core.

Configuration

NATS Server URL

Both NatsPublisher<T> and NatsConsumer<T> accept a URL and a subject name. The URL points to your NATS server:

nats://localhost:4222
nats://user:password@nats-server:4222

Subjects

NATS uses a flat, string-based subject hierarchy (e.g. orders.created, orders.>). You provide the subject to both publisher and consumer — matching subjects allows messages to flow.

appsettings.json Example

{
  "Nats": {
    "Url": "nats://localhost:4222",
    "Subject": "orders.created"
  }
}
var url = builder.Configuration["Nats:Url"]!;
var subject = builder.Configuration["Nats:Subject"]!;

Usage

Publishing a Single Message

using MVFC.Messaging.Nats.IO.Nats;

var url = "nats://localhost:4222";
var subject = "orders.created";

await using var publisher = new NatsPublisher<OrderCreated>(url, subject);

var order = new OrderCreated(1, "Keyboard", 149.90m);
await publisher.PublishAsync(order);

The message is serialized to JSON and published to the NATS subject.

Publishing a Batch

Batch publishing sends all messages concurrently using Task.WhenAll:

var orders = new[]
{
    new OrderCreated(1, "Keyboard", 149.90m),
    new OrderCreated(2, "Mouse", 59.90m),
    new OrderCreated(3, "Monitor", 899.00m)
};

await publisher.PublishBatchAsync(orders);

Consuming Messages

The consumer subscribes to the NATS subject and runs a background loop that yields messages as they arrive:

using MVFC.Messaging.Nats.IO.Nats;

var url = "nats://localhost:4222";
var subject = "orders.created";

await using var consumer = new NatsConsumer<OrderCreated>(url, subject);

await consumer.StartAsync(async (message, ct) =>
{
    Console.WriteLine($"Processing order #{message.OrderId}: {message.Product}");
    // Your business logic here
}, cancellationToken);

// ... later, when shutting down:
await consumer.StopAsync();

Consumer behavior:

  • Uses NatsConnection.SubscribeAsync<string> to receive messages as an async stream.
  • Messages with null or empty data are skipped.
  • Each valid message is deserialized from JSON and passed to the handler.
  • OperationCanceledException in the handler is re-thrown for proper cancellation propagation; other exceptions are caught to avoid breaking the consume loop.
  • DisposeAsync cancels the consume loop, waits for completion, and disposes the NATS connection.

Complete Publish + Consume Example

using MVFC.Messaging.Nats.IO.Nats;

var url = "nats://localhost:4222";
var subject = "orders.created";

await using var publisher = new NatsPublisher<OrderCreated>(url, subject);
await using var consumer = new NatsConsumer<OrderCreated>(url, subject);

// Start consuming
var received = new TaskCompletionSource<OrderCreated>();
await consumer.StartAsync(async (msg, ct) =>
{
    Console.WriteLine($"Received: Order #{msg.OrderId} — {msg.Product}");
    received.SetResult(msg);
}, CancellationToken.None);

// Publish
await publisher.PublishAsync(new OrderCreated(42, "Keyboard", 149.90m));

// Wait for the message to be consumed
var result = await received.Task.WaitAsync(TimeSpan.FromSeconds(10));

// Cleanup
await consumer.StopAsync();

API Reference

NatsPublisher<T>

Constructor Parameters
NatsPublisher<T>(string url, string subject) NATS server URL and the target subject
Method Description
PublishAsync(T message, CancellationToken ct) Serializes the message to JSON and publishes to the subject
PublishBatchAsync(IEnumerable<T> messages, CancellationToken ct) Publishes all messages concurrently
DisposeAsync() Disposes the underlying NatsConnection

NatsConsumer<T>

Constructor Parameters
NatsConsumer<T>(string url, string subject) NATS server URL and the subject to subscribe to
Method Description
StartAsync(Func<T, CancellationToken, Task> handler, CancellationToken ct) Subscribes to the subject and starts the consume loop
StopAsync(CancellationToken ct) Cancels the consume loop
DisposeAsync() Cancels, waits for completion, and disposes the connection

Requirements

  • .NET 9.0+
  • NATS.Client.Core (installed automatically)
  • A running NATS server (or Docker: docker run -p 4222:4222 nats)

License

Apache-2.0

Product Compatible and additional computed target framework versions.
.NET 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
3.0.2 113 4/5/2026
3.0.1 103 4/3/2026
2.0.4 106 3/21/2026
2.0.3 96 3/21/2026
2.0.2 101 3/21/2026
2.0.1 98 3/19/2026
1.0.2 236 12/19/2025
1.0.1 266 12/19/2025
1.0.0 265 12/19/2025