2

I am working with the new CosmosDB SDK v3 https://learn.microsoft.com/en-us/azure/cosmos-db/sql-api-sdk-dotnet-standard and a very simple insert, I have verified all the objects are indeed not null and have reasonable values but I still get the error message:

[1/12/2019 10:35:04] System.Private.CoreLib: Exception while executing function: HAPI_HM_Seasons. Microsoft.Azure.Cosmos.Direct: Object reference not set to an instance of an object.

I dont see why this is I must be missing something really basic here but I cant put my finger on it.

The code is as below:

List<SeasonInformation> seasonInformationList = new List<SeasonInformation>();

foreach(JObject document in listOfSeasons)
{
    SeasonInformation seasonInformation = new SeasonInformation
    {
        id = Guid.NewGuid().ToString(),
        Brand = brand,
        IntegrationSource = source,
        DocumentType = Enums.DocumentType.Season,
        UpdatedBy = "HAPI_HM_Seasons",
        UpdatedDate = DateTime.Now.ToString(),
        UpdatedDateUtc = string.Format("{0:yyyy-MM-ddTHH:mm:ss.FFFZ}", DateTime.UtcNow),
        OriginalData = document
    };
    seasonInformationList.Add(seasonInformation);
}

database = cosmosClient.GetDatabase(cosmosDBName);
container = database.GetContainer(cosmosDBCollectionNameRawData);

log.LogInformation(string.Format("HAPI_HM_Seasons BASIC setup done at {0:yyyy-MM-ddTHH:mm:ss.FFFZ}", DateTime.UtcNow));

log.LogInformation(string.Format("HAPI_HM_Seasons import {1} items BEGIN at {0:yyyy-MM-ddTHH:mm:ss.FFFZ}", DateTime.UtcNow, seasonInformationList.Count));

foreach(var season in seasonInformationList)
{

    ItemResponse<SeasonInformation> response = await container.CreateItemAsync(season);

}

I have verified that the List is populated and that the season variable in the loop contains the correct data so I am a bit stuck here.

The exception happens in the last foreach loop where I try CreateItemAsync into CosmosDB

Mark Schultheiss
  • 32,614
  • 12
  • 69
  • 100
Matt Douhan
  • 2,053
  • 1
  • 21
  • 40

1 Answers1

0

As a best practice, you need to use Async method with await in all the Cosmosdb methods just to make sure that they are getting executed and you get the response,

and modify your CreateItemAsync as follows,

ItemResponse<SeasonInformation> response = await container.CreateItemAsync(season, new PartitionKey(season.whatever));

Here is the Sample Repository

Sajeetharan
  • 216,225
  • 63
  • 350
  • 396