-1

I'm having some dramas with some CosmosDB code I'm trying to implement in a .NET Core console app. I'm trying to add documents to a container but am getting unhandled errors which are quitting my methods without any exception.

Here is a bit of code that's calling the method:

using (StreamReader file = File.OpenText(@"Orgs.json"))
{
    JsonSerializer serializer = new JsonSerializer();
    List<Org> org1 = (List<Org>)serializer.Deserialize(file, typeof(List<Org>));
    foreach (var org in org1)
    {
        await orgClient.AddItemAsync(org);
    }
}

and here is the method:

public async Task AddItemAsync(T item)
{
    try
    {
        Type myType = item.GetType();
        PropertyInfo propertyInfo = myType.GetProperty("Id");
        await this._container.CreateItemAsync<T>(item,
            new PartitionKey(propertyInfo.GetValue(item).ToString()));
    }
    catch (AggregateException e)
    {
        Console.WriteLine(e.Message);
    }
}

When this runs, the entire calling method fails without exception and returns to the caller. But when I do this from the caller, the code works fine:

using (StreamReader file = File.OpenText(@"Orgs.json"))
{
    JsonSerializer serializer = new JsonSerializer();
    List<Org> org1 = (List<Org>)serializer.Deserialize(file, typeof(List<Org>));
    foreach (var org in org1)
    {
        orgClient.AddItemAsync(org).Wait();
    }
}

(i.e. waiting on the AddItemAsync method)

I've got the "Just My Code" debugger option disabled. And this is on .NET 5 under VS2022 preview.

Can anyone enlighten me on how I can get this to work properly using the async patterns?

Theodor Zoulias
  • 34,835
  • 7
  • 69
  • 104
timyoungCW
  • 71
  • 1
  • 3
  • Thumb rule: if you say exception or errors, you need to provide it – Vivek Nuna Oct 13 '21 at 21:07
  • *the entire calling method fails without exception and returns to the caller* I am not sure what you mean. If something didn't throw an exception and returned to the caller, that seems like a success, not a failure. What else happened that makes you call it a failure? – John Wu Oct 13 '21 at 21:14
  • It's really hard to tell what is going on here since you are clearly mixing code. What is `T` in the second block for example? Also I think you are using the `Id` property for your partition key - don't do that, go read up on how partition keys work in CosmosDB. – DavidG Oct 13 '21 at 21:27
  • Here is your answer https://stackoverflow.com/questions/12980712/what-is-the-best-way-to-catch-exception-in-task/12981091 – Nigel Oct 13 '21 at 21:29
  • 1
    Can you show signature of the caller method? Based on symptoms you have `async void` or not awaiting for the results in the caller or further in the call stack – Fabio Oct 13 '21 at 21:58
  • Thanks for the reminder on the caller method, Fabio. It was an async void, and changing it to async Task it works fine. – timyoungCW Oct 13 '21 at 22:47

1 Answers1

0

Thanks for the pointers, all.

The problem was that the caller method was async void, where it should have been async Task.

Also, I noted the suggestions around the cosmos db partition fields - for smaller datasets I believe it's OK to use the ID as the partition field too.

timyoungCW
  • 71
  • 1
  • 3