0

I am using Microsoft.Azure.Cosmos.Client for connecting cosmos database from blazor web assembly project. My Cosmos database has multiple read regions, i.e South Korea (base location) and Japan East(Read region).

I want my blazor client application to load data from nearest cosmos database location. For that I was hoping that cosmos database automatically routes request to the nearest server, but it was always loading data from South Korea. To solve that I tried to specify CosmosClientOptions.ApplicationRegion property with fixed value like code below:

            CosmosClientOptions clientOptions = new CosmosClientOptions()
            {
                ConnectionMode = ConnectionMode.Gateway,
                ApplicationRegion = Regions.JapanEast // need to set this property dynamically based on users location
                
                //ApplicationPreferredRegions = new List<string>() { Regions.JapanEast, Regions.KoreaSouth } //Also tried this, but always loading from Japan east even if accessed from South Korea
            };

            client = new CosmosClient(Credentials.EndPoint, Credentials.ResourceToken, clientOptions);

In that case it always load data from Japan East server. I also tried setting LimitToEndpoint = true property but couldn't get myself much clarified on it.

Any suggestions?

Ashish
  • 114
  • 2
  • 10
  • Are you able to get the geolocation of the user in your project – Sabarish Sathasivan Jul 10 '20 at 05:03
  • No, I haven't found any way to get user's geolocation by blazor framework yet. I was expecting it could have been possible by blazor framework to get users geographic region that can be mapped with cosmos region (even though user blocks location permission in browser) – Ashish Jul 10 '20 at 06:10
  • 1
    Not sure about the type of the application , there are multiple ways to find the country 1) You can use the I/P address (https://stackoverflow.com/questions/4327629/get-user-location-by-ip-address) 2) if there are users for the application , may be you can derive it from their profile Once you get the county , you can have a look up to map the country with region and use the ConnectionPolicy.PreferredLocations to set the liocation – Sabarish Sathasivan Jul 10 '20 at 07:06

1 Answers1

1

The Cosmos client does not have any network metrics that automatically tells it what is the closest region to connect to. You have to specify that value. Preferred regions is simply an array of regions to connect to in order should a region become unavailable.

Customers often specify the value for region name in the regional deployment for the application. So you should look to your deployment scripts to pass this value.

Mark Brown
  • 8,113
  • 2
  • 17
  • 21