Skip to content

GuidesRun without a host

Run without a host

Build the generator and its beacons in a console tool or library with no dependency injection, and dispose them when you are done.

A console tool, or a library that does not use dependency injection, has no generic host to compose the generator and run the beacon collection, and SystemRandomSource.Instance alone takes no beacon input. RandomnessHost composes the same generator, beacons and validation without one. This page shows how to build it, how to set its options in code, how to see its logs, how to dispose it, and what to watch for.

RandomnessHost.CreateBuilder returns a builder that takes the same beacon extensions as AddRandomness() does, such as AddCurbyBeacon(). CreateBuilder takes nothing, an Action<RandomnessOptions> or a configuration section, as AddRandomness does. BuildAsync validates every setting, starts the collection and returns the host, whose SecureRandom is the generator. The generator is composed without a host by a builder over a private service container records why.

// The Randomness section of any IConfiguration.
var section = configuration.GetSection(RandomnessOptions.SectionName);
var builder = RandomnessHost.CreateBuilder(section);
builder.AddCurbyBeacon().AddNistBeacon().AddDrandBeacon();
await using var randomness = await builder.BuildAsync(cancellationToken);
Guid id = randomness.SecureRandom.NextGuid();

Pass an Action<RandomnessOptions> to set the options without configuration.

var builder = RandomnessHost.CreateBuilder(options =>
{
options.Mode = BeaconMode.Offline;
options.ReseedInterval = TimeSpan.FromSeconds(30);
});
await using var randomness = await builder.BuildAsync(CancellationToken.None);

The beacon collection logs through ILogger, and a host without a logging provider writes nothing. The builder’s Services stays open, so add a provider before BuildAsync.

builder.Services.AddLogging(logging => logging.AddConsole());
await using var randomness = await builder.BuildAsync(CancellationToken.None);

DisposeAsync stops the beacon collection, waiting up to 30 seconds, and disposes the generator, zeroing its state. A second call does nothing. Keep one host for the life of the application and dispose it when the application ends.

var builder = RandomnessHost.CreateBuilder();
var randomness = await builder.BuildAsync(CancellationToken.None);
var random = randomness.SecureRandom;
Guid id = random.NextGuid();
await randomness.DisposeAsync();
// random.NextGuid() now throws ObjectDisposedException: disposing zeroed
// the generator's state, so keep the host for as long as you draw values.
  • Never disposing the host.
    • You write await using nowhere, and drop the host when Main returns.
    • What happens: the beacon loops keep running until the process exits, and the generator’s state is never zeroed.
    • Write instead: await using var randomness = await builder.BuildAsync(cancellationToken);, or call DisposeAsync when the application ends.
  • Building a host per call.
    • You write await using var randomness = await RandomnessHost.CreateBuilder().BuildAsync(token); inside the method that needs a value.
    • What happens: each host has its own generator and its own beacon loops, so every call seeds and starts them again.
    • Write instead: build one host at start and keep it for the process, passing randomness.SecureRandom to whatever needs it.
  • Using the generator after disposal.
    • You write random.NextGuid(); after randomness.DisposeAsync().
    • What happens: it throws ObjectDisposedException, because disposing zeroed the generator’s state.
    • Write instead: draw values only while the host lives, and dispose it last.
  • Add beacons shows how to register the beacons and bind their settings.
  • Choose a mode shows how to keep fetching, fetch once or contact no beacon.