Getting The request was aborted: The request was canceled

I’m trying to implement normal select operation (get all documents from collection - SELECT * FROM travel-sample.inventory.airline)

using .netsdk latest version

QueryAsync is working fine and giving proper result
but when I’m trying to read it then at 6th iterate , it gives error that The request was aborted: The request was canceled.

tries this with different options - increasing timeout etc but facing the same error at 6th row
below is my code

       documentList = new List<JObject>();
        try
        {
            var result = cluster.QueryAsync<dynamic>(command, queryOptions).GetAwaiter().GetResult();
            if (limitRowsForOutput > -1)
            {
                var rows = result.Rows.ToEnumerable().Take(limitRowsForOutput);

                foreach (var row in rows)
                {
                    documentList.Add(JObject.FromObject(row));
                }
            }
            else
            {
                var rows = result.Rows.ToEnumerable();
                foreach (var row in result.Rows.ToEnumerable())
                {
                    documentList.Add(JObject.FromObject(row));
                }
                //documentList =  rows.Select(row => JObject.FromObject(row) as JObject).ToList();
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
  • using couchbase trail account

Hi @ruchita.oza, sorry to hear you’re having an issue here. Have you had a chance to read through some of our error handling documentation: Handling Errors | Couchbase Docs or get more detailed logging: Logging | Couchbase Docs

You are using some problematic patterns here, as regards asynchronicity and streaming enumeration, that’s probably causing your timeout problems. That’s my best guess, anyway.

A) If at all possible, never use .GetAwaiter().GetResult(). There are some specialized cases where it’s okay, but if you don’t know precisely what you’re doing it will tend to cause esoteric problems.

Your best case would be to write actual asynchronous code using constructs like async/await. If this is not possible, there are ways to run async code from sync code more safely. An example is the library I wrote (based on some other stuff from StackOverflow). Feel free to use it from the package published on GitHub Packages or copy the code: GitHub - CenterEdge/CenterEdge.Async: When you gotta, you gotta

B) You don’t need to deserialize as dynamic and then do JObject.FromObject. If you’re using the default serializer, dynamic will already be a JObject. Or you can simply QueryAsync<JObject>.

C) If you’re going to stick with the sync-over-async pattern, you should also be sure to do the entire enumeration within it, not come out and ToEnumerable() the async iterator. Something like ToListAsync() to enumerate the collection up-front. Using await foreach is still preferable if you convert to async/await though. You should also keep to a single sync-over-async, not multiple.

So, if you’re sticking with sync-over-async, something like this will at least work better:

using CenterEdge.Async;

public static class QueryExtensions
{
    private async Task<List<JObject>> QueryJObjectAsync(this ICluster cluster, string command, int limitRowsForOutput)
    {
        var result = await cluster.QueryAsync<JObject>(command, queryOptions);
        var rows = result.Rows;
        if (limitRowsForOutput > -1)
        {
            rows = rows.Take(limitRowsForOutput);
        }
    
        return await rows.ToListAsync();
    }
    
    public List<JObject> QueryJObject(this ICluster cluster, string command, int limitRowsForOutput = -1)
    {
        return AsyncHelper.RunSync(() => cluster.QueryJObjectAsync(command, limitRowsForOutput));
    }
}

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.