Home > Back-end >  Exposed port 0 is not mapped. testcontainer c#
Exposed port 0 is not mapped. testcontainer c#

Time:10-12

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?

CodePudding user response:

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.

  • Related