I'm calling my DocumentDB database to query for a person. If the person is not in the database, I'm then trying to insert the person into my collection.
When I check the collection, I see that the new person is being created but my code just seems to hang where I make the second call to insert the person into the collection. Any idea why my code is hanging? I'm not including all the code to save space e.g. GetDatabaseAsync(), GetCollectionAsync(), etc. are all working.
using (client = new DocumentClient(new Uri(endPointUrl), authorizationKey))
{
//Get the database
var database = await GetDatabaseAsync();
//Get the Document Collection
var collection = await GetCollectionAsync(database.SelfLink, "People");
string sqlQuery = "SELECT * FROM People f WHERE f.id = \"" + user.PersonId + "\"";
dynamic doc = client.CreateDocumentQuery(collection.SelfLink, sqlQuery).AsEnumerable().FirstOrDefault();
if (doc == null)
{
// User is not in the database. Add user to the database
try
{
**// This is where the code is hanging. It creates the user in my collection though!**
await client.CreateDocumentAsync(collection.DocumentsLink, user);
}
catch
{
// Handle error
}
}
else
{
// User is already in the system.
user = doc;
}
}
Is it possible that the code hangs because I'm trying to both query and insert a document inside the same USING statement.
Is it a better idea for me to create a new instance of the client and create a separate block to handle the document INSERT?
If a call to an async method hangs it usually is because it being called synchronously by calling it with .Wait() or .Result instead of await ing. You have not shown your calling code, so please include it here.
Option 1: Don't call your async method synchronously. This is the right approach.
Option 2: You should use .ConfigureAwait(false) in your async calls to DocDB if you are calling this method synchronously. Try this:
var database = await GetDatabaseAsync()**.ConfigureAwait(false)**;
...
var collection = await GetCollectionAsync(database.SelfLink, "People")**.ConfigureAwait(false)**;
...
await client.CreateDocumentAsync(collection.DocumentsLink, user)**.ConfigureAwait(false)**;
Further details on ConfigureAwait
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With