Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exposed port 0 is not mapped. testcontainer c#

I have the following code

private readonly MsSqlTestcontainer _msSqlTestcontainer;

public DbSessionConnectionFactory()
{
    _msSqlTestcontainer = new TestcontainersBuilder<MsSqlTestcontainer>()
        .WithImage("mcr.microsoft.com/mssql/server:2022-latest")
        .WithPortBinding(1433, 1433)
        .WithEnvironment("ACCEPT_EULA", "Y")
        .WithEnvironment("SA_PASSWORD", "Admin_12345")
        .Build();


}
public async Task InitializeAsync()
{
    await _msSqlTestcontainer.StartAsync();
}

A few days ago the StartAsync method was working without any error.

But now when I run the code I get the following exception:

Port '_msSqlTestcontainer.Port' threw an exception of type 'System.InvalidOperationException' int {System.InvalidOperationException}

enter image description here

What should I do?

like image 778
Farhad Zamani Avatar asked Sep 20 '25 16:09

Farhad Zamani


1 Answers

Please try the following configuration:

.WithDatabase(new MsSqlTestcontainerConfiguration { Password = "Admin_12345" })
.WithExposedPort(1433)
.WithPortBinding(1433, true)

This was not a great moment, when I added an extension class to configure modules. The extension WithDatabase sets on the property ContainerPort. This property is not set by your configuration. That is why it cannot find the port.

You can skip the port configuration for modules. You can simplify your configuration to something like this.


Note:

You should add TrustServerCertificate=true; into your sqlserver connection string.

like image 50
Andre Hofmeister Avatar answered Sep 22 '25 07:09

Andre Hofmeister