© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•2y ago•
28 replies
alpacasierra

Testcontainers CosmosDB / WebApplication Factory ConnectionString Not Working

anyone familiar with WebApplicationFactory / Testcontainers?

The below is giving me
System.ArgumentException: Format of the initialization string does not conform to specification starting at index 0.
System.ArgumentException: Format of the initialization string does not conform to specification starting at index 0.


public abstract class IntegrationTest
{
    protected readonly HttpClient TestClient;
    protected CosmosDbContainer cosmosDbContainer;

    protected IntegrationTest()
    {
        cosmosDbContainer = new CosmosDbBuilder()
            .WithImage("mcr.microsoft.com/cosmosdb/linux/azure-cosmos-emulator:latest")
            .Build();
        cosmosDbContainer.StartAsync().GetAwaiter().GetResult();

        var appFactory = new WebApplicationFactory<Program>()
            .WithWebHostBuilder(builder =>
            {
                builder.ConfigureServices(services =>
                {
                    var connectionString = cosmosDbContainer.GetConnectionString();
                    var cosmosOptions = new CosmosClientOptions()
                    { 
                        Serializer = new CosmosJsonSerializer()
                    };
                    services.RemoveAll(typeof(CosmosClient));
                    services.AddSingleton(new CosmosClient(connectionString, cosmosOptions));
                });

                builder.UseEnvironment("Development");
            });

        TestClient = appFactory.CreateClient();
    }

    public void Dispose()
    {
        cosmosDbContainer.DisposeAsync();
    }
}
public abstract class IntegrationTest
{
    protected readonly HttpClient TestClient;
    protected CosmosDbContainer cosmosDbContainer;

    protected IntegrationTest()
    {
        cosmosDbContainer = new CosmosDbBuilder()
            .WithImage("mcr.microsoft.com/cosmosdb/linux/azure-cosmos-emulator:latest")
            .Build();
        cosmosDbContainer.StartAsync().GetAwaiter().GetResult();

        var appFactory = new WebApplicationFactory<Program>()
            .WithWebHostBuilder(builder =>
            {
                builder.ConfigureServices(services =>
                {
                    var connectionString = cosmosDbContainer.GetConnectionString();
                    var cosmosOptions = new CosmosClientOptions()
                    { 
                        Serializer = new CosmosJsonSerializer()
                    };
                    services.RemoveAll(typeof(CosmosClient));
                    services.AddSingleton(new CosmosClient(connectionString, cosmosOptions));
                });

                builder.UseEnvironment("Development");
            });

        TestClient = appFactory.CreateClient();
    }

    public void Dispose()
    {
        cosmosDbContainer.DisposeAsync();
    }
}

someone mentioned ports earlier?
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

Integration testing with testcontainers and cosmosdb
C#CC# / help
2y ago
CosmosDB Linq GroupBy
C#CC# / help
3y ago
❔ ConnectionString to Azure SQL Server
C#CC# / help
3y ago
✅ Factory method
C#CC# / help
16mo ago