-
-
Notifications
You must be signed in to change notification settings - Fork 338
feat: Add connection string provider #1588
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
HofmeisterAn
merged 8 commits into
develop
from
feature/add-connection-string-provider-4
Dec 6, 2025
+367
−4
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
1acffc2
feat: Implement IConnectionStringProvider
HofmeisterAn 23169c5
feat: Add ConnectionStringProviderNotConfiguredException
HofmeisterAn 893342e
feat: Add tests
HofmeisterAn 37be051
chore: Test Configure call
HofmeisterAn d0c849e
docs: Add Connection String Provider API doc
HofmeisterAn bb569b9
chore: Switch to content tabs
HofmeisterAn 113d6df
docs: Add host and port to the example
HofmeisterAn 6ef12b1
chore: Add missing test
HofmeisterAn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| # Connection String Provider | ||
|
|
||
| The Connection String Provider API provides a standardized way to access and manage connection information for Testcontainers (modules). It allows developers to customize module-provided connection strings or add their own, and to access module-specific connection strings or endpoints (e.g., database connection strings, HTTP API base addresses) in a uniform way. | ||
|
|
||
| !!!note | ||
|
|
||
| Testcontainers modules do not yet implement this feature. Developers can use the provider to define and manage their own connection strings or endpoints. Providers will be integrated by modules in future releases. | ||
|
|
||
| ## Example | ||
|
|
||
| Register a custom connection string provider via the container builder: | ||
|
|
||
| ```csharp | ||
| IContainer container = new ContainerBuilder() | ||
| .WithConnectionStringProvider(new MyProvider1()) | ||
| .Build(); | ||
|
|
||
| // Implicit host connection string (default) | ||
| var hostConnectionStringImplicit = container.GetConnectionString(); | ||
|
|
||
| // Explicit host connection string | ||
| var hostConnectionStringExplicit = container.GetConnectionString(ConnectionMode.Host); | ||
|
|
||
| // Container-to-container connection string | ||
| var containerConnectionString = container.GetConnectionString(ConnectionMode.Container); | ||
| ``` | ||
|
|
||
| ## Implementing a custom provider | ||
|
|
||
| To create a custom provider, implement the generic interface: `IConnectionStringProvider<TContainer, TConfiguration>`. The `Configure(TContainer, TConfiguration)` method is invoked after the container has successfully started, ensuring that all runtime-assigned values are available. | ||
|
|
||
| === "Generic builder" | ||
| ```csharp | ||
| public sealed class MyProvider1 : IConnectionStringProvider<IContainer, IContainerConfiguration> | ||
| { | ||
| public void Configure(IContainer container, IContainerConfiguration configuration) | ||
| { | ||
| // Initialize provider with container information. | ||
| } | ||
|
|
||
| public string GetConnectionString(ConnectionMode connectionMode = ConnectionMode.Host) | ||
| { | ||
| // This method returns a default connection string. The connection mode argument | ||
| // lets you choose between a host connection or a container-to-container connection. | ||
| return "..."; | ||
| } | ||
|
|
||
| public string GetConnectionString(string name, ConnectionMode connectionMode = ConnectionMode.Host) | ||
| { | ||
| // This method returns a connection string for the given name. Useful for modules | ||
| // with multiple endpoints (e.g., Azurite blob, queue, or table). | ||
| return "..."; | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| === "Module builder" | ||
| ```csharp | ||
| public sealed class MyProvider2 : IConnectionStringProvider<PostgreSqlContainer, PostgreSqlConfiguration> | ||
| { | ||
| private string _host; | ||
|
|
||
| private ushort _port; | ||
|
|
||
| public void Configure(PostgreSqlContainer container, PostgreSqlConfiguration configuration) | ||
| { | ||
| // Initialize provider with container information. | ||
| _host = container.Hostname; | ||
| _port = container.GetMappedPublicPort(PostgreSqlBuilder.PostgreSqlPort); | ||
| } | ||
|
|
||
| public string GetConnectionString(ConnectionMode connectionMode = ConnectionMode.Host) | ||
| { | ||
| return $"Host={_host};Port={_port};..."; | ||
| } | ||
|
|
||
| public string GetConnectionString(string name, ConnectionMode connectionMode = ConnectionMode.Host) | ||
| { | ||
| return $"Host={_host};Port={_port};...;SSL Mode=Require"; | ||
| } | ||
| } | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
src/Testcontainers/Configurations/Containers/ConnectionMode.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| namespace DotNet.Testcontainers.Configurations | ||
| { | ||
| using JetBrains.Annotations; | ||
|
|
||
| /// <summary> | ||
| /// Represents the connection mode. | ||
| /// </summary> | ||
| [PublicAPI] | ||
| public enum ConnectionMode | ||
| { | ||
| /// <summary> | ||
| /// The connection string for the container. | ||
| /// </summary> | ||
| Container, | ||
|
|
||
| /// <summary> | ||
| /// The connection string for the host. | ||
| /// </summary> | ||
| Host, | ||
| } | ||
| } |
39 changes: 39 additions & 0 deletions
39
src/Testcontainers/Configurations/Containers/ConnectionStringProvider.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| namespace DotNet.Testcontainers.Configurations | ||
| { | ||
| using DotNet.Testcontainers.Containers; | ||
|
|
||
| /// <inheritdoc cref="IConnectionStringProvider{TContainerEntity, TConfigurationEntity}" /> | ||
| internal sealed class ConnectionStringProvider<TContainerEntity, TConfigurationEntity> : IConnectionStringProvider<IContainer, IContainerConfiguration> | ||
| where TContainerEntity : IContainer | ||
| where TConfigurationEntity : IContainerConfiguration | ||
| { | ||
| private readonly IConnectionStringProvider<TContainerEntity, TConfigurationEntity> _connectionStringProvider; | ||
|
|
||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="ConnectionStringProvider{TContainerEntity, TConfigurationEntity}" /> class. | ||
| /// </summary> | ||
| /// <param name="connectionStringProvider">The connection string provider.</param> | ||
| public ConnectionStringProvider(IConnectionStringProvider<TContainerEntity, TConfigurationEntity> connectionStringProvider) | ||
| { | ||
| _connectionStringProvider = connectionStringProvider; | ||
| } | ||
|
|
||
| /// <inheritdoc /> | ||
| public void Configure(IContainer container, IContainerConfiguration configuration) | ||
| { | ||
| _connectionStringProvider.Configure((TContainerEntity)container, (TConfigurationEntity)configuration); | ||
| } | ||
HofmeisterAn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| /// <inheritdoc /> | ||
| public string GetConnectionString(ConnectionMode connectionMode = ConnectionMode.Host) | ||
| { | ||
| return _connectionStringProvider.GetConnectionString(connectionMode); | ||
| } | ||
|
|
||
| /// <inheritdoc /> | ||
| public string GetConnectionString(string name, ConnectionMode connectionMode = ConnectionMode.Host) | ||
| { | ||
| return _connectionStringProvider.GetConnectionString(name, connectionMode); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
src/Testcontainers/Configurations/Containers/IConnectionStringProvider.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| namespace DotNet.Testcontainers.Configurations | ||
| { | ||
| using DotNet.Testcontainers.Containers; | ||
| using JetBrains.Annotations; | ||
|
|
||
| /// <summary> | ||
| /// A connection string provider. | ||
| /// </summary> | ||
| [PublicAPI] | ||
| public interface IConnectionStringProvider | ||
| { | ||
| /// <summary> | ||
| /// Gets the connection string. | ||
| /// </summary> | ||
| /// <param name="connectionMode">The connection mode.</param> | ||
| /// <returns>The connection string.</returns> | ||
| /// <exception cref="ConnectionStringProviderNotConfiguredException">Thrown when the connection string provider is not configured.</exception> | ||
| [NotNull] | ||
| string GetConnectionString(ConnectionMode connectionMode = ConnectionMode.Host); | ||
|
|
||
| /// <summary> | ||
| /// Gets the connection string. | ||
| /// </summary> | ||
| /// <param name="name">The connection string name.</param> | ||
| /// <param name="connectionMode">The connection mode.</param> | ||
| /// <returns>The connection string.</returns> | ||
| /// <exception cref="ConnectionStringProviderNotConfiguredException">Thrown when the connection string provider is not configured.</exception> | ||
| [NotNull] | ||
| string GetConnectionString(string name, ConnectionMode connectionMode = ConnectionMode.Host); | ||
| } | ||
| } |
19 changes: 19 additions & 0 deletions
19
src/Testcontainers/Configurations/Containers/IConnectionStringProvider`2.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| namespace DotNet.Testcontainers.Configurations | ||
| { | ||
| using DotNet.Testcontainers.Containers; | ||
| using JetBrains.Annotations; | ||
|
|
||
| /// <inheritdoc cref="IConnectionStringProvider" /> | ||
| [PublicAPI] | ||
| public interface IConnectionStringProvider<in TContainerEntity, in TConfigurationEntity> : IConnectionStringProvider | ||
| where TContainerEntity : IContainer | ||
| where TConfigurationEntity : IContainerConfiguration | ||
| { | ||
| /// <summary> | ||
| /// Configures the connection string provider. | ||
| /// </summary> | ||
| /// <param name="container">The container instance.</param> | ||
| /// <param name="configuration">The container configuration.</param> | ||
| void Configure(TContainerEntity container, TConfigurationEntity configuration); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
src/Testcontainers/Containers/ConnectionStringProviderNotConfiguredException.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| namespace DotNet.Testcontainers.Containers | ||
| { | ||
| using System; | ||
| using JetBrains.Annotations; | ||
|
|
||
| /// <summary> | ||
| /// Represents an exception that is thrown when the connection string provider is not configured. | ||
| /// </summary> | ||
| [PublicAPI] | ||
| public sealed class ConnectionStringProviderNotConfiguredException : Exception | ||
| { | ||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="ConnectionStringProviderNotConfiguredException" /> class. | ||
| /// </summary> | ||
| public ConnectionStringProviderNotConfiguredException() | ||
| : base("No connection string provider is configured for this container.") | ||
| { | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.