Michael Dera
Michael Dera

Reputation: 123

Entity Framework Core with Azure CosmosDB is repeatedly initialised

I am creating an REST API with Azure Cosmos DB (SQL API) as my database with Entity Framework Core. I am add the DbContext as a dependency when I configure services like so:

            services.AddDbContext<MyContext>(
                options => options.UseCosmos(CosmosDbEndpoint, CosmosDbAuthKey, CosmosDbName, cosmosOptionsAction => cosmosOptionsAction.ConnectionMode(ConnectionMode.Direct)));

The DBContext itself is implemented in the standard way

    public class PharmacyExpressContext : IdentityDbContext<User>
    {
        protected PharmacyExpressContext()
        {
            Database.EnsureCreated();
        }

        public PharmacyExpressContext (DbContextOptions<PharmacyExpressContext> options): base(options)
        {
            Database.EnsureCreated();
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {...
        }
        
        //DbSets here
        ....
}

When I inspect the logs I noticed that each time I call my API, Entity Framework is initialised all over again this is logged:

2020-09-07T11:28:28.790492961Z: [INFO] Entity Framework Core 3.1.7 initialized 'MyContext' using provider 'Microsoft.EntityFrameworkCore.Cosmos' with options: ServiceEndPoint=https:[Redacted] Database=[Redacted]

Suggesting that the connection is not persistent. Then after a couple of requests I will get

2020-09-07T11:37:49.899167416Z: [INFO] CosmosException;StatusCode=TooManyRequests;SubStatusCode=3200;ActivityId=176a9a48-9e57-4c17-9135-8ac776406c81;RequestCharge=0;Message=Response status code does not indicate success: 429 Substatus: 3200 Reason: (Message: {"Errors":["Request rate is large. More Request Units may be needed, so no changes were made. Please retry this request later. Learn more: http://aka.ms/cosmosdb-error-429"]}

I tried increase the RUs on my Database from Azure but that did not help. Other implementations I went through created a client from which CRUD is done but those did not include EF Core. I am not sure what I am missing.

Edit Not sure if this is relevant but I also creating a partition storage on the same database for storing User State and Conversation State.

 var storage = new CosmosDbPartitionedStorage(
                new CosmosDbPartitionedStorageOptions
                {
                    CosmosDbEndpoint = CosmosDbEndpoint, 
                    AuthKey = CosmosDbAuthKey, 
                    DatabaseId = CosmosDbName, 
                    ContainerId = CosmosContainerId, 
                    CompatibilityMode = false, 
                });


            var conversationState = new ConversationState(storage);
            services.AddSingleton(conversationState);

            var userState = new UserState(storage);
            services.AddSingleton(userState);

Upvotes: 0

Views: 2479

Answers (1)

Michael Dera
Michael Dera

Reputation: 123

This solution solved the Error 429 problem for me....

services.AddDbContext<PharmacyExpressContext>(
   options => options.UseCosmos(
       CosmosDbEndpoint, 
       CosmosDbAuthKey, 
       CosmosDbName,  
       cosmosCLientOptions =>  
           new CosmosClientOptions  
           { 
              IdleTcpConnectionTimeout = new System.TimeSpan(1, 0, 0 , 0)
           }));

I still get the Entity Framework Core 3.1.7 initialized in my logs but the responses for my API calls are markedly faster and I am no longer getting a too many requests error.

Details on further improving performance are recorded here

Upvotes: 2

Related Questions