I’m trying to make a functional basic Azure Cosmos Database workflow.
From my Azure account, I’m getting the endpoint address from the HOST
param in the "Connection String" in "Settings", and the key from the "PRIMARY PASSWORD" param.
string EndpointUri = "https://<my-database-name>.mongo.cosmos.azure.com";
string PrimaryKey ="<my-key>";
then I try to create the client (following these instructions):
CosmosClient cosmosClient = new(
uri,
key,
new CosmosClientOptions(){
ApplicationRegion = Regions.WestUS
}
);
database = cosmosClient.CreateDatabaseIfNotExistsAsync(id: databaseId).Result;
But I keep getting and error
The remote name could not be resolved
Am I using the Endpoint and Key wrong…?
3
Answers
Yes. You are using the Endpoint incorrectly.
The endpoint should be
https://<account-name>.documents.azure.com
.I guess you are using the Mongo API and using the SQL .Net SDK against it,
Here is the example to connect with Mongo API using the driver.
You’ve mixed-n-matched protocols. Every instance of Cosmos DB has an associated protocol (SQL API, MongoDB API, etc). Your example shows you chose the MongoDB API (which has a MongoDB endpoint) with the Cosmos DB API’s SDK (and that won’t work). Note: When I mentioned instance of Cosmos DB, I mean a complete namespace (which you created), and that namespace encapsulates all of your databases and related containers, permissions, etc.
When using the MongoDB API, you need to use standard MongoDB SDKs (provided by MongoDB or by the community), and then code against it just as you would code against native MongoDB databases.
The only protocol that uses the Cosmos DB SDK is the native SQL API (which is the SDK referenced in the code in your question).
If you really wanted to use different protocols (e.g. some Cosmos DB native containers, some MongoDB containers, some Cassandra keyspaces…), you’d need to create individual instances of Cosmos DB (three instances, in this case).