C# WCF数据服务BeginSaveChanges只运行一次

C# WCF数据服务BeginSaveChanges只运行一次,c#,wcf-data-services,xamarin.forms,C#,Wcf Data Services,Xamarin.forms,我在Xamarin表单应用程序中有以下WCF数据服务代码。 它更新表中的一个简单行 Static.Dialogs.Alert("Starting"); DataServiceQuery<SRef.SimpleObject> query = (DataServiceQuery<SRef.SimpleObject>)Entities.SimpleObject.Where(x => x.ID == Guid.Parse("DEF47A0F-AF1E-404

我在Xamarin表单应用程序中有以下WCF数据服务代码。 它更新表中的一个简单行

    Static.Dialogs.Alert("Starting");
    DataServiceQuery<SRef.SimpleObject> query = (DataServiceQuery<SRef.SimpleObject>)Entities.SimpleObject.Where(x => x.ID == Guid.Parse("DEF47A0F-AF1E-4043-B8C8-56084841E80B"));
    query.BeginExecute((result) =>
             {
                 try
                 {
                     Static.Dialogs.Alert("Getting the object");
                     var actData = query.EndExecute(result).FirstOrDefault();
                     if (actData != null)
                     {
                         actData.Info = "Info"+randomNumber;
                         Entities.UpdateObject(actData);
                         Entities.ChangeState(actData, EntityStates.Modified);
                         Static.Dialogs.Alert("Before the update");
                         Entities.BeginSaveChanges(SaveChangesOptions.BatchWithIndependentOperations, (result2) =>
                         {
                             try
                             {
                                 Static.Dialogs.Alert("BeginSaveChanges starts");
                                 var r = Entities.EndSaveChanges(result2);
                                 Static.Dialogs.Alert("Update done ");
                             }
                             catch (Exception ex2)
                             {
                                 Static.Dialogs.Alert("Error:" + ex2.Message);
                             }
                         }, null);
                     }
                     else
                         Static.Dialogs.Alert("No object");
                 }
                 catch (Exception ex1)
                 {
                     Static.Dialogs.Alert("Error:" + ex1.Message);
                 }
             }, null);
         }
         catch (Exception ex)
         {
             Static.Dialogs.Alert("Error:" + ex.Message);
         }            
     });
我还(在第一个答案中指出)结果是在不同的线程上提供的,因此我添加了一个dispatcher调用来获取结果(注意UI线程调用:
Xamarin.Forms.Device.BeginInvokeMainThread

在应用程序中,回调必须在特定的 线程,必须显式封送End方法的执行, 它处理对所需线程的响应。例如,在 基于Windows Primation Foundation(WPF)的应用程序和 基于Silverlight的应用程序,必须将响应封送回 通过在Dispatcher上使用BeginInvoke方法创建UI线程 反对

注意提到的
End
方法

我添加了以下调试消息:

foreach (var item in Entities.Entities)
{
  System.Diagnostics.Debug.WriteLine(item.Identity + " " + item.State);
}
结果是:

[0:]http://192.168.1.100/InfoDataService/InfoDataService.svc/Info(guid'f1057131-90ee-11d7-9812-0040f6cc1384') Unchanged
[0:]http://192.168.1.100/InfoDataService/InfoDataService.svc/Info(guid'f1057133-90ee-11d7-9812-0040f6cc1384') Unchanged
[0:]http://192.168.1.100/InfoDataService/InfoDataService.svc/Info(guid'f6cfce91-90ef-11d7-9812-0040f6cc1384') Unchanged
[0:]http://192.168.1.100/InfoDataService/InfoDataService.svc/Info(guid'a6c2d822-91a7-11d7-9813-0040f6cc1384') Unchanged
[0:]http://192.168.1.100/InfoDataService/InfoDataService.svc/Info(guid'a6c2d823-91a7-11d7-9813-0040f6cc1384') Unchanged
[0:]http://192.168.1.100/InfoDataService/InfoDataService.svc/Info(guid'a6c2d824-91a7-11d7-9813-0040f6cc1384') Unchanged
[0:]http://192.168.1.100/InfoDataService/InfoDataService.svc/Info(guid'b750e561-91b8-11d7-9813-0040f6cc1384') Unchanged
[0:]http://192.168.1.100/InfoDataService/InfoDataService.svc/Info(guid'b750e562-91b8-11d7-9813-0040f6cc1384') Unchanged
[0:]http://192.168.1.100/InfoDataService/InfoDataService.svc/Info(guid'b750e563-91b8-11d7-9813-0040f6cc1384') Unchanged
[0:]http://192.168.1.100/InfoDataService/InfoDataService.svc/Info(guid'eee2d1f7-17cb-4283-a053-01f6cf7bb2fd') Unchanged
[0:]  Added
[0:]  Added
[0:]  Added
看起来,上下文一直在收集对象,但它只向服务发送第一个新对象,而其他对象一直在堆积

本文强调了
End
方法的线程问题,但回调是这里真正的问题(它没有被触发),因此我们永远不会涉及
End
调用和UI线程


这似乎是一个严重的错误,我不知道该怎么办…

原因是
BeginExecute
将启动另一个线程,即使子线程没有完成,主线程也会返回。然后此代码甚至可能不执行
BeginSaveChanges

因此,要确保所有的子线程完成。主线程需要等待子线程


最简单的方法是
System.Threading.Thread.Sleep(5000)
;或者您可以使用
IAsyncResult.AsyncWaitHandle.WaitOne()

,但是
BeginSaveChanges
函数在线程内部,并且在
BeginExecute
之后不执行,因此它必须在线程中运行。我不明白为什么会失败,我一直在检查。
[0:]http://192.168.1.100/InfoDataService/InfoDataService.svc/Info(guid'f1057131-90ee-11d7-9812-0040f6cc1384') Unchanged
[0:]http://192.168.1.100/InfoDataService/InfoDataService.svc/Info(guid'f1057133-90ee-11d7-9812-0040f6cc1384') Unchanged
[0:]http://192.168.1.100/InfoDataService/InfoDataService.svc/Info(guid'f6cfce91-90ef-11d7-9812-0040f6cc1384') Unchanged
[0:]http://192.168.1.100/InfoDataService/InfoDataService.svc/Info(guid'a6c2d822-91a7-11d7-9813-0040f6cc1384') Unchanged
[0:]http://192.168.1.100/InfoDataService/InfoDataService.svc/Info(guid'a6c2d823-91a7-11d7-9813-0040f6cc1384') Unchanged
[0:]http://192.168.1.100/InfoDataService/InfoDataService.svc/Info(guid'a6c2d824-91a7-11d7-9813-0040f6cc1384') Unchanged
[0:]http://192.168.1.100/InfoDataService/InfoDataService.svc/Info(guid'b750e561-91b8-11d7-9813-0040f6cc1384') Unchanged
[0:]http://192.168.1.100/InfoDataService/InfoDataService.svc/Info(guid'b750e562-91b8-11d7-9813-0040f6cc1384') Unchanged
[0:]http://192.168.1.100/InfoDataService/InfoDataService.svc/Info(guid'b750e563-91b8-11d7-9813-0040f6cc1384') Unchanged
[0:]http://192.168.1.100/InfoDataService/InfoDataService.svc/Info(guid'eee2d1f7-17cb-4283-a053-01f6cf7bb2fd') Unchanged
[0:]  Added
[0:]  Added
[0:]  Added