We can use Azure CosmosDB emulator to simulate Azure CosmosDB without actually connect to Azure environment. The emulator is running inside a container.
To setup the container using the latest image:
docker run -it --name cosmosDB -p 8081:8081 mcr.microsoft.com/cosmosdb/linux/azure-cosmos-emulator
This will create a container named cosmosDB and the emulator access port is 8081.
When .Net code connect to the emulator, you might receive error about “The SSL connection could not be established” and “The remote certificate is invalid because of errors in the certificate chain: UntrustedRoot”.
One way to fix the error is to properly export and install the certificate generated by the emulator. This might involve complex steps.
Alternativly, you can use the following code in the connection option to disable SSL validation:
C#...
CosmosClientOptions options = new CosmosClientOptions()
{
/*HttpClientFactory is required for the cosmosDB emulator
This function by pass SSL certificate validation. Without this, will receive untrustedroot error.
*/
HttpClientFactory = () =>
{
HttpMessageHandler httpMessageHandler = new HttpClientHandler()
{
ServerCertificateCustomValidationCallback = (req, cert, chain, errors) => true
};
return new HttpClient(httpMessageHandler);
}
};
CosmosClient client = new(endpoint,key,options);
...
Disabling SSL validation is only recommended for development purposes.
Noet: Based on some experiment, the container version emulator doesn’t perform, and the desktop version performs much better.