NatsSandbox 0.1.0

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

🧩 NATS Sandbox

Simple local orchestration of temporary NATS server for .NET 6–9 — built for tests, benchmarks, and experiments.


✨ Overview

NATS Sandbox spins up disposable NATS server with zero manual setup.
It automatically downloads the NATS binary, starts it locally, and tears it down when finished — so you can focus on messaging, not ops.

Why you'll love it

  • 🧱 Self-contained — no external dependencies; no docker; everything runs in-process
  • Temporary — perfect for unit & integration testing
  • 🧭 Cross-platform — works on Windows, Linux, macOS
  • 🪶 Simple API — a single call: using var runner = NatsRunner.Run();

📦 Installation

dotnet add package NatsSandbox

Requires .NET 6 or newer.


Example

using NATS.Client.Core;
using NatsSandbox;
using Xunit;

public class IntegrationTests : IAsyncLifetime
{
    private INatsRunner _runner = null!;
    private NatsConnection _nats = null!;

    public async Task InitializeAsync()
    {
        _runner = NatsRunner.Run();
        _nats = new NatsConnection(new NatsOpts { Url = _runner.Url });
        await _nats.ConnectAsync();
    }

    public async Task DisposeAsync()
    {
        await _nats.DisposeAsync();
        _runner.Dispose();
    }

    [Fact]
    public async Task Publish_And_Subscribe_Works()
    {
        var tcs = new TaskCompletionSource<bool>();

        _ = Task.Run(async () =>
        {
            await foreach (var msg in _nats.SubscribeAsync<string>("test.subject"))
            {
                Assert.Equal("Hello", msg.Data);
                tcs.TrySetResult(true);
                break;
            }
        });

        await Task.Delay(200);
        await _nats.PublishAsync("test.subject", "Hello");

        await tcs.Task.WaitAsync(TimeSpan.FromSeconds(5));
    }
}

NatsRunnerOptions

Property Description Default
Port TCP port for the NATS server. Leave unset to auto-select a free port. null (auto)
MonitoringPort HTTP monitoring port. Leave unset to auto-select a free port. null (auto)
EnableJetStream Enable JetStream for persistence and streaming capabilities. false
StandardOutputLogger Delegate that receives the NATS server's standard output. null
EnableDebugLogging Enable debug-level logging from the NATS server. false
EnableTraceLogging Enable trace-level logging from the NATS server (very verbose). false
DataDirectory Directory where the NATS server will store its data. Leave unset for a temporary directory that will be cleaned up on disposal. null (auto)
BinaryDirectory Provide your own NATS server binary in this directory. Leave unset to download automatically. null (auto)
ConnectionTimeout Timeout for the NATS server to start and be ready to accept connections. 30 seconds
AdditionalArguments Additional arguments to pass to the NATS server. null
DataDirectoryLifetime Duration for which temporary data directories will be kept. Ignored when you provide your own data directory. 12 hours
Transport HTTP transport to use for downloading NATS binaries. Useful when behind a proxy or firewall. Default HttpClient transport
Version NATS server version to download. 2.12.1

You can fully customise HTTP behavior (headers, auth token, rate-limit handling) via your own IHttpTransport implementation.

Use a Specific Port
var runner = NatsRunner.Run(new NatsRunnerOptions
{
    Port = 4222
});
Enable Logging
var runner = NatsRunner.Run(new NatsRunnerOptions
{
    StandardOutputLogger = Console.WriteLine,
    EnableDebugLogging = true
});

Troubleshooting

Test Hangs or Times Out

The NATS binary download might be slow on first run. Subsequent runs use the cached binary.

Port Already in Use

Don't specify a port - let NatsSandbox choose a random available port (default behavior).

Windows Firewall Prompt

Click "Allow" when prompted. NATS needs to open a network port for communication.


🖥️ Supported Platforms

OS Architecture Supported
Windows x64 / arm64
Linux x64, arm64, arm
macOS x64, arm64

🧰 Development Notes

  • Caches binaries per version tag in %LOCALAPPDATA%/nats-sandbox/bin/vX.Y.Z
  • Performs platform-specific extraction and sets executable permissions on Unix
  • Thread-safe via internal SemaphoreSlim during version checks

🪪 License

MIT © Wassim K— see LICENSE

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 is compatible.  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 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 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 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.

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
0.1.0 366 10/27/2025