简体   繁体   English

从DocumentDb删除文档

[英]Delete a document from DocumentDb

I'm trying to delete a specific document from my DocumentDb collection called Employees. 我正在尝试从我的DocumentDb集合中删除一个名为“雇员”的特定文档。 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 . 这可能是由于您使用了async void Probably whatever is calling this code is disposing some managing resource (ie, client ) before RemoveEmployeeAsync completes. RemoveEmployeeAsync完成之前,无论调用什么代码,都可能在RemoveEmployeeAsync一些管理资源(即client )。

The best solution is to make RemoveEmployeeAsync return a Task instead of void , and have the calling code await it. 最好的解决方案是使RemoveEmployeeAsync返回一个Task而不是void ,并让调用代码await它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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