尝试在DocumentDB中创建文档时获取“任务已取消”

当我尝试使用以下代码在我的DocumentDB数据库中创建文档时,代码挂起我调用CreateDocumentAsync()的地方,并最终给我“一个任务被取消”错误。

知道为什么吗?

public static async Task CreateEmployee(Employee emp) { try { 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, "Employees"); await client.CreateDocumentAsync(collection.SelfLink, emp); // Do something else with employee } } catch { // Handle error } return emp; } 

PS为简洁起见,我没有包含GetDatabaseAsync()和GetCollectionsAsync()部分的代码。 请注意,员工文档是在我的数据库中创建的。 因此,此代码清楚地连接到DocumentDB数据库,查找集合并创建文档。

只是不确定它为什么会挂起并最终返回错误。

*****更新******

这是错误详细信息:任务已取消。

描述:执行当前Web请求期间发生未处理的exception。 请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。

exception详细信息:System.Threading.Tasks.TaskCanceledException:任务已取消。 来源错误:

 HttpResponseMessage response = await client.PostAsJsonAsync("api/hr/register", employee); 

堆栈跟踪:

 [TaskCanceledException: A task was canceled.] System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +10915395 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +52 System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() +24 ConsumerWebApi.Controllers.d__0.MoveNext() in c:\Users\Sam\Documents\Visual Studio 2013\Projects\ConsumerWebApi\ConsumerWebApi\Controllers\HomeController.cs:36 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() +22 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +10915367 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +52 System.Runtime.CompilerServices.TaskAwaiter.GetResult() +21 System.Threading.Tasks.TaskHelpersExtensions.ThrowIfFaulted(Task task) +61 System.Web.Mvc.Async.TaskAsyncActionDescriptor.EndExecute(IAsyncResult asyncResult) +114 System.Web.Mvc.Async.c__DisplayClass37.b__36(IAsyncResult asyncResult) +66 System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +47 System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +135 System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +102 System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult) +49 System.Web.Mvc.Async.AsyncInvocationWithFilters.b__3d() +117 System.Web.Mvc.Async.c__DisplayClass46.b__3f() +323 System.Web.Mvc.Async.c__DisplayClass33.b__32(IAsyncResult asyncResult) +44 System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +47 System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +135 System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +102 System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult asyncResult) +50 System.Web.Mvc.Async.c__DisplayClass2b.b__1c() +72 System.Web.Mvc.Async.c__DisplayClass21.b__1e(IAsyncResult asyncResult) +185 System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +42 System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +132 System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +56 System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +40 System.Web.Mvc.Controller.b__1d(IAsyncResult asyncResult, ExecuteCoreState innerState) +34 System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +70 System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +138 System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +59 System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +40 System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +44 System.Web.Mvc.Controller.b__15(IAsyncResult asyncResult, Controller controller) +39 System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +62 System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +138 System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +59 System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +40 System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +39 System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +39 System.Web.Mvc.MvcHandler.b__5(IAsyncResult asyncResult, ProcessRequestState innerState) +39 System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +70 System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +138 System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +59 System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +40 System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +40 System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +38 System.Web.CallHandlerExecutionStep.OnAsyncHandlerCompletion(IAsyncResult ar) +129 

问题是由于没有正确的异步调用引起的。 我正在调用异步方法,即CreateDocumentAsync()不正确。

我发布了一个关于异步编程的单独问题,我得到的答案帮助我解决了这个问题。 这是post: 想了解异步