CosmosDB Partition Keys and running Azurite from VS

CosmosDB partition keys got me and some others on Stackoverflow, so I’m gonna drop a short one here.
Also, getting Azurite to run when the project is launched in VS.

CosmosDB #

I haven’t had much to do with CosmosDB, but according to Microsoft it can do everything, but I’m just using the standard default SQL API.
The structure is (with SQL Server analogies):

Partition Keys #

As I write this, I’ve just found the example I needed :( I’ve never had to deal with partitioning in SQL Server, so all I can say is that partition keys are required here and I don’t know why.
A Stackoverflow search says that the item ID needn’t be unique in the container, but Id + partition key must be 🤷

Either way, I had trouble getting these partition keys to do what I wanted.

First, an example:
Example

var client = GetCosmosClient();
Database database = await client.CreateDatabaseIfNotExistsAsync("databaseName");
var container = await database.CreateContainerIfNotExistsAsync("resultscontainer", "/name");

// e.g.
await container.DeleteItemAsync<Result>(result.Id, new PartitionKey(result.Id));

Cosmos notes #

I ended up using id for partition keys which makes partitioning worthless.

Apparently I shouldn’t have to use it under 10gb, but the code seems to need it…

Run Azurite with VS #

Azurite can be run as a docker container automatically from VS.
Following the instructions, I went:

It’s almost certainly not required, and can just add the Azure.Storage.Blobs and Microsoft.Extensions.Azure Nuget packages with the following code in Program.cs/Startup.cs:

Expand/Collapse cs

builder.Services.AddAzureClients(clientBuilder =>
{
    clientBuilder.AddBlobServiceClient(appConfig.AzureStorageConnectionString, preferMsi: true);
});

internal static class AzureClientFactoryBuilderExtensions
{
    public static IAzureClientBuilder<BlobServiceClient, BlobClientOptions> AddBlobServiceClient(this AzureClientFactoryBuilder builder, string serviceUriOrConnectionString, bool preferMsi)
    {
        if (preferMsi && Uri.TryCreate(serviceUriOrConnectionString, UriKind.Absolute, out Uri? serviceUri))
        {
            return builder.AddBlobServiceClient(serviceUri);
        }
        else
        {
            return builder.AddBlobServiceClient(serviceUriOrConnectionString);
        }
    }
}
builder.Services.AddAzureClients(clientBuilder =>
{
    clientBuilder.AddBlobServiceClient(appConfig.AzureStorageConnectionString, preferMsi: true);
});

internal static class AzureClientFactoryBuilderExtensions
{
    public static IAzureClientBuilder<BlobServiceClient, BlobClientOptions> AddBlobServiceClient(this AzureClientFactoryBuilder builder, string serviceUriOrConnectionString, bool preferMsi)
    {
        if (preferMsi && Uri.TryCreate(serviceUriOrConnectionString, UriKind.Absolute, out Uri? serviceUri))
        {
            return builder.AddBlobServiceClient(serviceUri);
        }
        else
        {
            return builder.AddBlobServiceClient(serviceUriOrConnectionString);
        }
    }
}