In the previous article, we have seen the introduction of Cosmos DB and EF Core with Cosmos DB-SQL API. If you haven’t read my previous articles then I highly encourage you to do so:
Geo-replication can be enabled by clicking on “Enable Geo Replication”.
In this example, I have configured East US 2, North Europe and SouthEast Asia region for reads and writes.
Coding
As compared to the previous article, I’m implementing the same program in Web API rather than the console App. I’ll not be explaining the code until it is absolutely necessary.
You can go through my previous article by referring to the recap section.
Now, register the ProfileContext in the ConfigureServices method of Startup class
Now run the application and it works! But wait, as we have configured geo-replication to which region of database instance the application is called? Good question.
To verify this we can enable Application Insight in our application by adding a nuget package Microsoft.ApplicationInsights.AspNetCore
Register Application Insight in the ConfigureServices method of Startup class.
- services.AddApplicationInsightsTelemetry();
- "ApplicationInsights": {
- "InstrumentationKey": "<Instrumentation Key here>"
- },
You can get the instrumentation key by executing below command
- # Displays Instrumentation key
- az resource show -g $resourceGroup -n
- $name --resource-type "Microsoft.Insights/components"
- --query properties.InstrumentationKey
Now deploy your application to Azure app service either to “AppServiceSI” or “AppServiceEU”. You can navigate to Application Insight by click on the resource group (GeoReplication) and now click on Application Map. Now precisely, you will be able to see on which database instance the query has been executed.
As you know, we have 2 App Service
- AppServiceSI- Deployed in South India
- AppServiceEU- Deployed in East US
Now the application executes randomly on both the instances of the database. But is there a way to set the prefered location of the database? ex: App service deployed in South India should point to SouthEast Asia database region whereas, the app service deployed in East US should point to East US 2 database region because these database instances are geographically nearer to the app service location.
I would like to thank Julie Lerman, Jeremy Likness, and Smit Patel for giving the answer to configure the preferred location. I was struggling to get the answer and there are no articles/questions around preferred location anywhere.
You can configure the preferred region in the AddDbContext method:
- public void ConfigureServices(IServiceCollection services)
- {
- var accountEndpoint = Configuration.GetValue<string>("ConnectionString:AccountEndpoint");
- var accountKey= Configuration.GetValue<string>("ConnectionString:AccountKey");
- var dbName= Configuration.GetValue<string>("ConnectionString:DatabaseName");
- var region = Configuration.GetValue<string>("ApiServerRegion");
- services.AddDbContext<ProfileContext>(x => x.UseCosmos(accountEndpoint, accountKey, dbName
- ,cosmosOptionsAction=>cosmosOptionsAction.Region(region)));
- services.AddControllers();
- services.AddApplicationInsightsTelemetry();
- }
In the appsettings.json, add the below lines for East US App service whereas, set the ApiServerRegion=”SouthEastAsia” for the South Indian App service.
- "ApiServerRegion": "EastUS2",
Now deploy both the app service by setting the proper ApiServerRegion in the appsettings.json.
Now run both the app services. You will be able to see the result in the application map present in Application Insight.
I hope you like the article. In case you find the article as interesting then kindly like and share it.