none
Problems for create new documents in async RRS feed

  • Question

  • Hi all,
    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
    [code]await client.CreateDocumentAsync("dbs/" + database.Id + "/colls/" + coll.Id, this);[/code]
    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.
    Thursday, February 11, 2016 10:25 PM

Answers

All replies