1

I'm at the beginning for using DocumentDB so I've downloaded the .NET SDK. Following the using example downloaded from Azure website, I'm trying to create some documents for saving to DocumentDB. The example code is the following

   public static void Main(string[] args)
    {
        try
        {
            GetStartedDemo().Wait();
        }
        catch (DocumentClientException de)
        {
            // omissis
        }
    }
    private static async Task GetStartedDemo()
    {
        var client = new DocumentClient(new Uri(EndpointUrl), AuthorizationKey);

        Family andersonFamily = new Family
        {
            Id = "AndersenFamily",
            LastName = "Andersen",
            Parents = new Parent[] {
                new Parent { FirstName = "Thomas" },
                new Parent { FirstName = "Mary Kay"}
                },
            Children = new Child[] {
                    new Child
                    {
                        FirstName = "Henriette Thaulow",
                        Gender = "female",
                        Grade = 5,
                        Pets = new Pet[] {
                            new Pet { GivenName = "Fluffy" }
                        }
                    }
                },
            Address = new Address { State = "WA", County = "King", City = "Seattle" },
            IsRegistered = true
        };

        await client.CreateDocumentAsync("dbs/" + database.Id + "/colls/" + documentCollection.Id, andersonFamily);

        client.Dispose();
    }

This code works good. My code is almost the same except for the class I pass for saving in JSON into DB. It always stops on the line

await client.CreateDocumentAsync("dbs/" + database.Id + "/colls/" + coll.Id, this);

My entire code is the following

public static async Task Save(int UserId, int productID, string code, string language, string dataOraLettura, string longitudine, string latitudine, string nazione_lettura)
    {
        CultureInfo ciEN = CultureInfo.GetCultureInfo("en-US");
        CodeType ScannedCode = new CodeType
        {
            Code = code,
            ProductId = productID,
            ScanLog = new List<CodeType.ScanDataType>()
        };
        ScannedCode.ScanLog.Add(new CodeType.ScanDataType
        {
            Location = new Microsoft.Azure.Documents.Spatial.Point(double.Parse(longitudine, ciEN.NumberFormat), double.Parse(latitudine, ciEN.NumberFormat)),
            ScanType = CodeType.ScanDataType.eScanType.Alert,
            UserId = UserId.ToString(),
            TimeStamp = long.Parse(DateTime.Now.ToString("yyyyMMddHHmmssffff"))
        });
        await ScannedCode.AddAsync();

    }

   public async Task AddAsync()
    {
        using (DocumentClient client = new DocumentClient(new Uri(WebConfigurationManager.AppSettings["DbURI"]), WebConfigurationManager.AppSettings["DbKEY"]))
        {
            var CodeDocuments = client.CreateDocumentQuery<CodeType>("dbs/" + database.Id + "/colls/" + coll.Id).Where(a => a.Code == this.Code).ToArray();
            if (CodeDocuments.Length == 0)
                await client.CreateDocumentAsync("dbs/" + database.Id + "/colls/" + coll.Id, this);
        }
    }

It seems as the asynchronous task blocks and it doesn't get back the control to the caller then remain in loop execution.

4
  • Buncha little things: ScannedCode.AddAsync() doesn't seem static, yet it's called as such. Are you certain you don't have an overload? How many times do you need to repeat the "dbs/.../colls/" pattern before you extract it? If you have 20 parameters on a method you've surely missed one - put them in a class already. Don't follow how the example hides an exception Commented Feb 11, 2016 at 22:41
  • 1
    @StenPetrov This isn't a code-review; the OP asked about an async issue. No need to throw out criticisms about the coding style. Commented Feb 12, 2016 at 21:08
  • ScannedCode.AddAsync() doesn't seem static, that's what I'd check first. The question needs a review itself, there's not sufficient information to go on.... and in some such cases if the OP were to refactor their own code they'd find the error themselves. And your point is good, hence my reply is a comment, not an answer Commented Feb 12, 2016 at 21:51
  • 1
    Try changing await client.CreateDocumentAsync to client.CreateDocumentAsync(...).Result Commented Feb 12, 2016 at 23:19

1 Answer 1

3

Correct, you have a classic case of async blocking another async. You should set ConfigureAwait to false on await ScannedCode.AddAsync();

 await ScannedCode.AddAsync().ConfigureAwait(false);

This makes it resume on a thread pool thread instead of the ASP.NET request context.

You can read the following article http://blog.stephencleary.com/2012/07/dont-block-on-async-code.html which explains this very well as well as other best practices while using Async code.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.