简体   繁体   中英

Delete a document from DocumentDb

I'm trying to delete a specific document from my DocumentDb collection called Employees. When I run the following code, I get "A task was cancelled" error.

What am I doing wrong?

private async static void RemoveEmployeeAsync(string colSelfLink)
        {
            var doc = client.CreateDocumentQuery<Document>(colSelfLink, "SELECT * FROM Employees e WHERE e.EmployeeId = 1").AsEnumerable().FirstOrDefault();

            if(doc != null)
            {
                await client.DeleteDocumentAsync(doc.SelfLink);
            }
        }

It's probably due to your use of async void . Probably whatever is calling this code is disposing some managing resource (ie, client ) before RemoveEmployeeAsync completes.

The best solution is to make RemoveEmployeeAsync return a Task instead of void , and have the calling code await it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM