Cosmos Client object getting disposed

Dubey, Yogesh Vishnudev 41 Reputation points
2022-08-03T14:07:54.627+00:00

Hi,

The am using .Net SDK version 3 in my .Net 6 web api. This is used to establish connection with the backend cosmos db. AS per the Performance tips and guidelines, Its mentioned to have a singleton instanse of a cosmos client for the application lifecycle. in Order to achieve the above I have registered a cosmos service as singelton in the program.cs as below

builder.Services.AddSingleton<ICosmosService, CosmosService>();

Then in the cosmos service class the connection is initialised as below

private static readonly CosmosClient? cosmosClient;

static CosmosService()
{

        cosmosClient =SetCosmosConnectionProperties(); // Initializes the cosmos client object   
    }  

public static CosmosClient SetCosmosConnectionProperties()
{
List<string> cosmosRegion = new List<string>();

        cosmosRegion.Add(ConfigValues.CosmosPreferredRegion);  

        foreach (string s in ConfigValues.CosmosOtherRegions.Split(','))  
        {  
            cosmosRegion.Add(s);  
        }  

       var cosmosConnectionProperties =  GetCosmosConnection(); //Sets Containers and DB name for Cosmos client  
        CosmosClientOptions options = new CosmosClientOptions();  
        options.ApplicationName = ConfigValues.ApplicationName;  
        options.ApplicationPreferredRegions = cosmosRegion;  
        options.ConnectionMode = ConnectionMode.Direct;  
        return  new CosmosClient(EndPointUri,PrimaryKeyReadOnly, options);  
    }  

//The dependency is inject in controller class as below

private ICosmosService cosmosService { get; }
public DemoController(ICosmosService _cosmosService)
{
cosmosService =_cosmosService
}

//Then in the api action method use the connection as below

[Route("[action]")]
[HttpGet]
public async Task<ObjectResult> GetData(string appName)
{
using (var _cosmosClient = cosmosClient)
{

//Use the cosmos client to fetch data from Cosmosbackend

                  }  

}

This works as expected for the first api call, however for the second call it throws the cosmosClient is disposed error in the using block.

Not sure why the object is getting disposed here? Other singelton initilization works as expected not sure whats the case with CosmosClient here.

Azure Cosmos DB
Azure Cosmos DB
An Azure NoSQL database service for app development.
1,906 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Dubey, Yogesh Vishnudev 41 Reputation points
    2022-08-08T12:36:15.03+00:00

    Hi ,

    Actually i have identified the root cause of the issue , it was becase of the
    using (var _cosmosClient = cosmosClient) .My understanding was that only the left hand side of the operator would be disposed, but turn out that was not the case even the static object on the right hand side of the operator was getting disposed. Resolved the issue once i removed the using statment and made some design changes


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.