Azure移动应用程序服务异常"该项目不存在"而InsertAsync

时间:2016-06-28 12:11:00

标签: c# azure exception azure-mobile-services

我有兴趣的情况。我在Azure上有类和表:

public class InmeItem
{
      public string Id { get; set; }
      [JsonProperty(PropertyName = "heartrate")]
      public string Heartrate { get; set; }
      [JsonProperty(PropertyName = "pulsewave")]
      public string Pulsewave { get; set; }
}

我已按照代码将新项目添加到表格中:

public static async Task InsertInmeItem(InmeItem inmeitem)
{
     try
     {
         await App.MobileService.GetTable<InmeItem>().InsertAsync(inmeitem);
     }

     catch (Microsoft.WindowsAzure.MobileServices.MobileServiceInvalidOperationException ex)
     {
           Debug.WriteLine("This is f***** situation which post data but generate exception: " + ex.ToString());
     }

     catch (Exception ex)
     {
           Debug.WriteLine(ex);
     }
}

但我有一些感兴趣的情况 - 跑步抛出异常&#34;该项目不存在&#34;但数据已插入Azure表中,没有任何例外

异常信息:

This is f***** situation which post data  but generate exception: Microsoft.WindowsAzure.MobileServices.MobileServiceInvalidOperationException: The item does not exist
   at Microsoft.WindowsAzure.MobileServices.MobileServiceHttpClient.<ThrowInvalidResponse>d__18.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.WindowsAzure.MobileServices.MobileServiceHttpClient.<SendRequestAsync>d__1d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.WindowsAzure.MobileServices.MobileServiceHttpClient.<RequestAsync>d__4.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.WindowsAzure.MobileServices.MobileServiceTable.<>c__DisplayClass14.<<InsertAsync>b__13>d__16.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.WindowsAzure.MobileServices.MobileServiceTable.<TransformHttpException>d__4d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.WindowsAzure.MobileServices.MobileServiceTable.<InsertAsync>d__1a.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.WindowsAzure.MobileServices.MobileServiceTable`1.<TransformHttpException>d__41.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.WindowsAzure.MobileServices.MobileServiceTable`1.<InsertAsync>d__b.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.WindowsAzure.MobileServices.MobileServiceTable`1.<InsertAsync>d__5.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
   at InmeTesting.Models.Backoffice.<InsertInmeItem>d__2.MoveNext()

2 个答案:

答案 0 :(得分:3)

您需要确保从insert函数返回正确的结果,即它希望您返回插入的项目。您不一定需要返回插入的项目,但是您必须返回要呈现给客户端的内容,否则框架将返回404而不是消息&#34;该项目不存在&#34;。< / p>

第一个示例成功,因为context.execute返回插入的项目。在第二个示例中,显然没有从.then()调用中的代码块返回该项。

答案 1 :(得分:0)

我找到了理由。

对于此例,可能有两个原因

  1. 不等于数据库类的名称和云上表的名称(但类相等)

  2. 如果在table.insert(function (context) {...}); then() return context.execute();

  3. 中有table.insert(function (context) { //context.item.userId = context.user.id; //Retranslate data to inme server retranslateToInme(context.item.heartrate, context.item.pulsewave); return context.execute(); }); 的后端(在我的情况下这是Node JS),Theese异常也会抛出

    例如此代码

    table.insert(function (context) {
       //context.item.userId = context.user.id;
    
       //Retranslate data to inme server
       retranslateToInme(context.item.heartrate, context.item.pulsewave);
    
       return context.execute().then(...);
    });
    

    不要抛出异常。但是:

    ReSharper | Options -> Code Editing | C# | Formatting style | Other

    将抛出异常