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
<PackageReference Include="Soenneker.Atomics.Resources" Version="11.0.67" />
<PackageVersion Include="Soenneker.Atomics.Resources" Version="11.0.67" />
<PackageReference Include="Soenneker.Atomics.Resources" />
paket add Soenneker.Atomics.Resources --version 11.0.67
#r "nuget: Soenneker.Atomics.Resources, 11.0.67"
#:package Soenneker.Atomics.Resources@11.0.67
#addin nuget:?package=Soenneker.Atomics.Resources&version=11.0.67
#tool nuget:?package=Soenneker.Atomics.Resources&version=11.0.67
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
Tmust 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
Resetcalls 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 | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net11.0 is compatible. |
-
net11.0
- Soenneker.Atomics.ValueResources (>= 11.0.2)
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 |
Upgrade .NET 11 release candidates to stable