Reputation: 243
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.
Upvotes: 1
Views: 2103
Reputation: 2728
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.
Upvotes: 3