© 2026 Hedgehog Software, LLC

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

How to Override CosmosClient Configuration in WebApplicationFactory for Integration Testing

Hi everyone,

I'm trying to override the CosmosClient configuration in my Program.cs for my integration tests using WebApplicationFactory, but I'm running into issues. Here's the relevant code from Program.cs:

clientsBuilder.AddClient<CosmosClient, CosmosClientOptions>(options =>
 {
     options.Serializer = new CosmosJsonSerializer();
     options.ApplicationRegion = cosmosConfig.ApplicationRegion;
     return new CosmosClient(cosmosConfig.ConnectionString, options);
 })
clientsBuilder.AddClient<CosmosClient, CosmosClientOptions>(options =>
 {
     options.Serializer = new CosmosJsonSerializer();
     options.ApplicationRegion = cosmosConfig.ApplicationRegion;
     return new CosmosClient(cosmosConfig.ConnectionString, options);
 })


I tried removing the existing CosmosClient service like this, but it doesn't seem to work:
services.RemoveAll(typeof(CosmosClient));
services.RemoveAll(typeof(CosmosClient));
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

❔ WebApplicationFactory and integration tests
C#CC# / help
3y ago
✅ Integration testing
C#CC# / help
16mo ago
Integration Testing
C#CC# / help
4y ago
Configuration of Testcontainer, WebApplicationFactory and ConfigurationSource in ASP.NET Core.
C#CC# / help
14mo ago