|
| 1 | +# Connection String Provider |
| 2 | + |
| 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 | + |
| 5 | +!!!note |
| 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 modules in future releases. |
| 8 | + |
| 9 | +## Example |
| 10 | + |
| 11 | +Register a custom connection string provider via the container builder: |
| 12 | + |
| 13 | +```csharp |
| 14 | +IContainer container = new ContainerBuilder() |
| 15 | + .WithConnectionStringProvider(new MyProvider1()) |
| 16 | + .Build(); |
| 17 | + |
| 18 | +// Implicit host connection string (default) |
| 19 | +var hostConnectionStringImplicit = container.GetConnectionString(); |
| 20 | + |
| 21 | +// Explicit host connection string |
| 22 | +var hostConnectionStringExplicit = container.GetConnectionString(ConnectionMode.Host); |
| 23 | + |
| 24 | +// Container-to-container connection string |
| 25 | +var containerConnectionString = container.GetConnectionString(ConnectionMode.Container); |
| 26 | +``` |
| 27 | + |
| 28 | +## Implementing a custom provider |
| 29 | + |
| 30 | +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 builder" |
| 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 builder" |
| 58 | + ```csharp |
| 59 | + public sealed class MyProvider2 : IConnectionStringProvider<PostgreSqlContainer, PostgreSqlConfiguration> |
| 60 | + { |
| 61 | + private string _host; |
| 62 | + |
| 63 | + private ushort _port; |
| 64 | + |
| 65 | + public void Configure(PostgreSqlContainer container, PostgreSqlConfiguration configuration) |
| 66 | + { |
| 67 | + // Initialize provider with container information. |
| 68 | + _host = container.Hostname; |
| 69 | + _port = container.GetMappedPublicPort(PostgreSqlBuilder.PostgreSqlPort); |
| 70 | + } |
| 71 | + |
| 72 | + public string GetConnectionString(ConnectionMode connectionMode = ConnectionMode.Host) |
| 73 | + { |
| 74 | + return $"Host={_host};Port={_port};..."; |
| 75 | + } |
| 76 | + |
| 77 | + public string GetConnectionString(string name, ConnectionMode connectionMode = ConnectionMode.Host) |
| 78 | + { |
| 79 | + return $"Host={_host};Port={_port};...;SSL Mode=Require"; |
| 80 | + } |
| 81 | + } |
| 82 | + ``` |
0 commit comments