Cosmos Db ApplicationRegion is not acknowledged

Jean-Paul 21 Reputation points
2021-12-27T18:28:21.527+00:00

I have an API deployed in two regions, and the API has a Cosmos Db resource as data storage.
Cosmos Db is configured with multi-region writes, and I'm trying to redirect the API calls to the closest region.

Before moving to SDK v3, I had this implemented in V2 and it worked like a charm.
With v3 it should work similarly with specifying the client option ApplicationRegion, but it seems not acknowledged when I look at the metrics.

All traffic is handled by my primary region, and not by the closest write region.
I use the code below to configure this in my startup.cs.
The reason I have the 'if' statement with 'cdbPreferredLocation' is read from the config and is because I test locally with CosmosDb emulator and that doesn't (seem to) support the ApplicationRegion setting.

Microsoft.Azure.Cosmos.CosmosClient client = new Microsoft.Azure.Cosmos.CosmosClient(cdbEndpoint, cdbAuthkey, new Microsoft.Azure.Cosmos.CosmosClientOptions
{
    SerializerOptions = new Microsoft.Azure.Cosmos.CosmosSerializationOptions 
    { 
        IgnoreNullValues = true,
        PropertyNamingPolicy = Microsoft.Azure.Cosmos.CosmosPropertyNamingPolicy.CamelCase
    }
});
if (!string.IsNullOrWhiteSpace(cdbPreferredLocation))
    client.ClientOptions.ApplicationRegion = cdbPreferredLocation;

var service = new CosmosDbService(client, cdbDbName, cdbContainerName);

What am I doing wrong?

Azure Cosmos DB
Azure Cosmos DB
An Azure NoSQL database service for app development.
1,448 questions
0 comments No comments
{count} votes

Accepted answer
  1. Mark Brown - MSFT 2,761 Reputation points Microsoft Employee
    2021-12-27T23:33:10.867+00:00

    You need to set the ApplicationRegion before you instantiate the Cosmos DB client. It will ignore if you try setting it after you have created it.

    Your code should look like this...

    CosmosClient client = new CosmosClient(cdbEndpoint, cdbAuthkey, new CosmosClientOptions
    {
        ApplicationRegion = cdbPreferredLocation,
        SerializerOptions = new CosmosSerializationOptions
        {
             IgnoreNullValues = true,
             PropertyNamingPolicy = CosmosPropertyNamingPolicy.CamelCase
         }
     });
    

1 additional answer

Sort by: Most helpful
  1. GeethaThatipatri-MSFT 27,642 Reputation points Microsoft Employee
    2022-01-24T14:35:01.817+00:00

    Thank you @Jean-Paul for visiting Microsoft QA forums! Have a great day!

    Regards
    Geetha

    0 comments No comments