Skip to content

Connection strings

Experimental Containers Reviewed 2026-10-02 purview-dev/containers Star on GitHub 3 azurite containers csharp docker dotnet integration-testing mysql nats nuget postgresql rabbitmq redis sql-server testcontainers testing windows wsl wsl-containers wslc

Experimental. This project is an experiment: the public API, defaults and packaging can change between prereleases, and there is no production support guarantee.

Every container implements IConnectionStringProvider, so you can ask any container for its connection string without knowing the module type:

IContainer container = new PostgreSqlBuilder().Build();
await container.StartAsync();
string connectionString = container.GetConnectionString(); // ConnectionMode.Host
string same = container.GetConnectionString(ConnectionMode.Host); // explicit

ConnectionMode describes how the connection string targets the container:

ModeMeaningSupport
HostTest host → container, using the mapped host port (127.0.0.1:{port}).✅ both backends
ContainerContainer → container, using the container network.❌ not supported yet

ConnectionMode.Container throws ConnectionStringModeNotSupportedException. Neither backend supports container-to-container networking today: WSLC has no managed networks or inter-container DNS, and the Docker backend does not attach containers to a shared network. Multi-container wiring is deliberately deferred (see Networking).

A module’s builder registers a connection string provider that delegates to the module’s own GetConnectionString():

sealed class PostgreSqlConnectionStringProvider
: ContainerConnectionStringProvider<PostgreSqlContainer, PostgreSqlConfiguration>
{
protected override string GetHostConnectionString() => Container.GetConnectionString();
}

ContainerConnectionStringProvider<TContainer, TConfiguration> is the base class: Configure runs once, after the container has started (so runtime-assigned ports are available), and dispatches GetConnectionString(ConnectionMode) to GetHostConnectionString() / GetContainerConnectionString(). The named overload (GetConnectionString(name, mode)) exists for modules with several endpoints; the base throws ConnectionStringNameNotSupportedException unless a provider overrides it.

Override the connection string a container exposes with WithConnectionStringProvider:

sealed class ReadOnlyPostgreSql : ContainerConnectionStringProvider<PostgreSqlContainer, PostgreSqlConfiguration>
{
protected override string GetHostConnectionString() => $"{Container.GetConnectionString()};ApplicationName=readonly";
}
var postgres = new PostgreSqlBuilder()
.WithConnectionStringProvider(new ReadOnlyPostgreSql())
.Build();

Providers that produce an empty connection string throw ConnectionStringNotAvailableException, and a provider used before Configure throws ConnectionStringProviderNotConfiguredException.

See Modules for each module’s default connection string and any extra endpoint accessors.