简体   繁体   中英

Azure Cosmos DB error in creating database: The remote name could not be resolved

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...?

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 (eg 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).

Am I using the Endpoint and Key wrong...?

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 .

var client = new MongoClient(Environment.GetEnvironmentVariable("MONGO_CONNECTION"));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM