Home > other >  CosmosDB Container not getting created
CosmosDB Container not getting created

Time:11-09

CreateContainerIfNotExistsAsync is throwing an exception with status code "Bad Request" if the container does not exist in the db. If the container exists in the db, then no exception is thrown. Can anyone help me why this is happening.

(Hid the url and key for online posting)

using Microsoft.Azure.Cosmos;
using Microsoft.Azure.Cosmos.Linq;
using System.Threading.Tasks;

namespace CosmosDB // Note: actual namespace depends on the project name.
{
    class Program
    {
        public static async Task Main(string[] args)
        {
            var cosmosUrl = "###########################";
            var cosmoskey = "###########################";
            var databaseName = "TestDB";
            // var containerId = "ToDo";

            CosmosClient client = new CosmosClient(cosmosUrl, cosmoskey);
            
            Database database = await client.CreateDatabaseIfNotExistsAsync(databaseName);
            
            Container container = await database.CreateContainerIfNotExistsAsync(
                id: "ToDoList",
                partitionKeyPath: "/category",
                throughput: 100
            );            
        }

    }
}

CodePudding user response:

The command fails because your input is invalid. The throughput must be a value between 400 and 10,000 RU/s (for a normal database or container) and since you are using 100 it will throw the exception.

The error won't occur if your container already exists, because it will not check (server-side) or perform an update on the throughput.

  • Related