You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
3
+
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.
4
4
5
5
!!!note
6
6
7
-
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 the modules in future releases.
7
+
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.
8
8
9
9
## Example
10
10
@@ -27,52 +27,50 @@ var containerConnectionString = container.GetConnectionString(ConnectionMode.Con
27
27
28
28
## Implementing a custom provider
29
29
30
-
To create a custom provider, implement the generic interface: `IConnectionStringProvider<TContainer, TConfiguration>`.
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.
31
+
32
+
=== "Generic provider"
33
+
```csharp
34
+
public sealed class MyProvider1 : IConnectionStringProvider<IContainer, IContainerConfiguration>
35
+
{
36
+
public void Configure(IContainer container, IContainerConfiguration configuration)
37
+
{
38
+
// Initialize provider with container information.
39
+
}
40
+
41
+
public string GetConnectionString(ConnectionMode connectionMode = ConnectionMode.Host)
42
+
{
43
+
// This method returns a default connection string. The connection mode argument
44
+
// lets you choose between a host connection or a container-to-container connection.
45
+
return "...";
46
+
}
47
+
48
+
public string GetConnectionString(string name, ConnectionMode connectionMode = ConnectionMode.Host)
49
+
{
50
+
// This method returns a connection string for the given name. Useful for modules
51
+
// with multiple endpoints (e.g., Azurite blob, queue, or table).
52
+
return "...";
53
+
}
54
+
}
55
+
```
56
+
57
+
=== "Module provider"
58
+
```csharp
59
+
public sealed class MyProvider2 : IConnectionStringProvider<PostgreSqlContainer, PostgreSqlConfiguration>
60
+
{
61
+
public void Configure(PostgreSqlContainer container, PostgreSqlConfiguration configuration)
62
+
{
63
+
// Initialize provider with PostgreSQL-specific container information.
64
+
}
65
+
66
+
public string GetConnectionString(ConnectionMode connectionMode = ConnectionMode.Host)
67
+
{
68
+
return "Host=localhost;Port=5432;...";
69
+
}
70
+
71
+
public string GetConnectionString(string name, ConnectionMode connectionMode = ConnectionMode.Host)
0 commit comments