3

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?

Farhad Zamani
  • 5,381
  • 2
  • 16
  • 41
  • 1
    Having the same problem here :) If you do figure it out, please post the solution here. (And I will do the same) – Zvone Oct 12 '22 at 09:28
  • @Zvone The problem was solved by Andre Hofmeister aswner ;) – Farhad Zamani Oct 12 '22 at 13:57
  • 1
    glad it worked for you! I actually pinged the very nice community of testcontainers on their slack and he responded to me so we troubleshooted for some time and then he replied with a fix here! – Zvone Oct 12 '22 at 15:13

1 Answers1

4

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.

Farhad Zamani
  • 5,381
  • 2
  • 16
  • 41
Andre Hofmeister
  • 3,185
  • 11
  • 51
  • 74