Soenneker.Atomics.Resources 11.0.67

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

alternate text is missing from this package README image alternate text is missing from this package README image alternate text is missing from this package README image alternate text is missing from this package README image

Soenneker.Atomics.Resources

Lazy, atomic publication and replacement of a reference-type resource with application-defined teardown.

AtomicResource<T> is useful for shared resources that can be recreated, such as a client or compiled snapshot. It publishes one winning reference and cleans up replaced or losing instances through the supplied callback.

Installation

dotnet add package Soenneker.Atomics.Resources

Create a resource holder

This package has no DI registrar. Construct the holder with a synchronous factory and a teardown callback, then register or own that holder as appropriate:

using Soenneker.Atomics.Resources;

var resource = new AtomicResource<HttpClient>(
    factory: () => new HttpClient
    {
        BaseAddress = new Uri("https://api.example.com")
    },
    teardown: client =>
    {
        client.Dispose();
        return ValueTask.CompletedTask;
    });

HttpClient? client = resource.GetOrCreate();

GetOrCreate returns null after disposal. Before disposal, concurrent callers may each invoke the factory, but only one created instance is published. Instances that lose the publication race are passed to teardown without awaiting its ValueTask.

Factories should therefore be safe to run concurrently, and teardown for race losers should complete synchronously or safely continue without an observer.

Read and replace

TryGet reads the current reference without creating it:

HttpClient? existing = resource.TryGet();

Reset creates and publishes a replacement, then awaits teardown of the previous instance:

await resource.Reset();

The replacement is visible before old-resource teardown completes. Existing callers may still hold and use the old reference while teardown is running; AtomicResource<T> does not provide leases or usage tracking. Use higher-level coordination when a resource cannot be torn down until all borrowers finish.

Teardown exceptions from Reset, Dispose, and DisposeAsync are intentionally swallowed.

Disposal

await resource.DisposeAsync();

Disposal marks the holder closed, removes the currently published instance, and invokes teardown. Dispose blocks on the teardown ValueTask; DisposeAsync awaits it. Both are safe to call repeatedly.

Do not call Reset concurrently with disposal. Reset checks disposal before creating the replacement, so a disposal race after that check can publish a new instance during shutdown. Coordinate lifecycle operations in the owning service.

Operational constraints

  • T must be a reference type.
  • Creation is synchronous; use another abstraction when initialization itself must be awaited.
  • The holder makes reference publication atomic, not operations performed by T.
  • Avoid concurrent Reset calls unless the factory and teardown tolerate multiple replacements and overlapping cleanup.
  • A returned reference is not protected from a later reset or disposal.

API

Member Behavior
GetOrCreate() Returns the published instance, creates a candidate, or returns null after disposal.
TryGet() Reads the current instance without creating it.
Reset() Publishes a fresh instance and tears down the previous one.
IsDisposed Indicates that the holder has been closed.
Dispose() / DisposeAsync() Removes and tears down the current instance.
Product Compatible and additional computed target framework versions.
.NET net11.0 is compatible. 
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
11.0.67 92 11/11/2026
11.0.66 148 10/27/2026
11.0.65 91 10/26/2026
11.0.64 91 10/26/2026
11.0.63 94 10/26/2026
4.0.62 117 10/15/2026
4.0.61 106 10/15/2026
4.0.60 1,701 10/15/2026
4.0.59 949 10/14/2026
4.0.58 108 10/14/2026
4.0.57 1,018 10/14/2026
4.0.56 104 10/14/2026
4.0.55 274 10/13/2026
4.0.52 103 10/13/2026
4.0.51 819 10/12/2026
4.0.49 503 10/12/2026
4.0.48 2,221 10/11/2026
4.0.47 519 10/11/2026
4.0.46 15,975 9/24/2026
4.0.45 111 9/24/2026
Loading failed

Upgrade .NET 11 release candidates to stable