首页 > 解决方案 > Azure 移动应用推送操作离线-在线同步失败

问题描述

我有一个简单的 Xamarin 表单,它具有在线-离线支持。当我运行该应用程序时,我收到此错误。

    {Microsoft.WindowsAzure.MobileServices.Sync.MobileServicePushFailedException: Push operation has failed. See the PushResult for details. ---> Microsoft.WindowsAzure.MobileServices.Sync.MobileServiceLocalStoreException: Failed to read the item from local store. ---> System.InvalidOperationException: Table with name 'Coffees' is not defined.
  at Microsoft.WindowsAzure.MobileServices.SQLiteStore.MobileServiceSQLiteStore.GetTable (System.String tableName) [0x0001b] in <36e1ccee72264daa8b2297dac889883c>:0 
  at Microsoft.WindowsAzure.MobileServices.SQLiteStore.MobileServiceSQLiteStore.ExecuteQueryInternal (System.String tableName, System.String sql, System.Collections.Generic.IDictionary`2[TKey,TValue] parameters) [0x00000] in <36e1ccee72264daa8b2297dac889883c>:0 
  at Microsoft.WindowsAzure.MobileServices.SQLiteStore.MobileServiceSQLiteStore+<>c__DisplayClass13_0.<LookupAsync>b__0 (System.Threading.Tasks.Task t) [0x00000] in <36e1ccee72264daa8b2297dac889883c>:0 
  at System.Threading.Tasks.ContinuationResultTaskFromTask`1[TResult].InnerInvoke () [0x00024] in <58604b4522f748968296166e317b04b4>:0 
  at System.Threading.Tasks.Task.Execute () [0x00000] in <58604b4522f748968296166e317b04b4>:0 
--- End of stack trace from previous location where exception was thrown ---

  at Microsoft.WindowsAzure.MobileServices.Sync.PushAction+<>c__DisplayClass12_0.<LoadOperationItem>b__0 () [0x0008e] in <14b3925606b5475b8c7d9d3d44eaca5c>:0 
  at Microsoft.WindowsAzure.MobileServices.Sync.PushAction.TryStoreOperation (System.Func`1[TResult] action, Microsoft.WindowsAzure.MobileServices.Sync.OperationBatch batch, System.String error) [0x00063] in <14b3925606b5475b8c7d9d3d44eaca5c>:0 
   --- End of inner exception stack trace ---
  at Microsoft.WindowsAzure.MobileServices.Sync.PushAction.TryStoreOperation (System.Func`1[TResult] action, Microsoft.WindowsAzure.MobileServices.Sync.OperationBatch batch, System.String error) [0x00085] in <14b3925606b5475b8c7d9d3d44eaca5c>:0 
  at Microsoft.WindowsAzure.MobileServices.Sync.PushAction.LoadOperationItem (Microsoft.WindowsAzure.MobileServices.Sync.MobileServiceTableOperation operation, Microsoft.WindowsAzure.MobileServices.Sync.OperationBatch batch) [0x000cc] in <14b3925606b5475b8c7d9d3d44eaca5c>:0 
  at Microsoft.WindowsAzure.MobileServices.Sync.PushAction.ExecuteOperationAsync (Microsoft.WindowsAzure.MobileServices.Sync.MobileServiceTableOperation operation, Microsoft.WindowsAzure.MobileServices.Sync.OperationBatch batch) [0x0018c] in <14b3925606b5475b8c7d9d3d44eaca5c>:0 
  at Microsoft.WindowsAzure.MobileServices.Sync.PushAction.ExecuteAllOperationsAsync (Microsoft.WindowsAzure.MobileServices.Sync.OperationBatch batch) [0x00199] in <14b3925606b5475b8c7d9d3d44eaca5c>:0 
  at Microsoft.WindowsAzure.MobileServices.Sync.PushAction.ExecuteBatchAsync (Microsoft.WindowsAzure.MobileServices.Sync.OperationBatch batch, System.Collections.Generic.List`1[T] syncErrors) [0x000ba] in <14b3925606b5475b8c7d9d3d44eaca5c>:0 
   --- End of inner exception stack trace ---
  at Microsoft.WindowsAzure.MobileServices.Sync.MobileServiceSyncContext.ExecuteSyncAction (Microsoft.WindowsAzure.MobileServices.Sync.SyncAction action) [0x00090] in <14b3925606b5475b8c7d9d3d44eaca5c>:0 
  at Microsoft.WindowsAzure.MobileServices.Sync.MobileServiceSyncContext.PushAsync (System.Threading.CancellationToken cancellationToken, Microsoft.WindowsAzure.MobileServices.Sync.MobileServiceTableKind tableKind, System.String[] tableNames) [0x0013b] in <14b3925606b5475b8c7d9d3d44eaca5c>:0 
  at CoffeeApp.Services.APIClients.SalonsAPIClient.SyncSalons () [0x00194] in E:\Jan-May\CoffeeApp\CoffeeApp\CoffeeApp\Services\Server.cs:25 
  at CoffeeApp.Services.APIClients.SalonsAPIClient.AddSalon (CoffeeApp.Models.Coffee s) [0x000fa] in E:\Jan-May\CoffeeApp\CoffeeApp\CoffeeApp\Services\Server.cs:42 
  at CoffeeApp.Views.AddNewCoffee.ToolSave_Clicked (System.Object sender, System.EventArgs e) [0x000c3] in E:\Jan-May\CoffeeApp\CoffeeApp\CoffeeApp\Views\AddNewCoffee.xaml.cs:39 }

这是我的代码。

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using CoffeeApp.Models;
using Microsoft.WindowsAzure.MobileServices;
using Plugin.Connectivity;

namespace CoffeeApp.Services.APIClients
{
    public class SalonsAPIClient
    {
        private async Task SyncSalons()
        {
            await ApiClient.Initialise();
            try
            {
                if (!CrossConnectivity.Current.IsConnected)
                    return;
                await ApiClient.client.SyncContext.PushAsync();
                await ApiClient.coffeeTable.PullAsync("allCoffee", ApiClient.coffeeTable.CreateQuery());
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }

        public async Task<List<Coffee>> GetSalons()
        {
            await ApiClient.Initialise();
            await SyncSalons();
            var data = await ApiClient.coffeeTable.OrderBy(s => s.CoffeName)
                .ToListAsync();
            return data;
        }

        public async Task<Coffee> AddSalon(Coffee s)
        {
            await ApiClient.Initialise();
            await ApiClient.coffeeTable.InsertAsync(s);
            await SyncSalons();
            return null;
        }
    }
}

这是我的 API 类

using System.Threading.Tasks;
using CoffeeApp.Models;
using Microsoft.WindowsAzure.MobileServices;
using Microsoft.WindowsAzure.MobileServices.SQLiteStore;
using Microsoft.WindowsAzure.MobileServices.Sync;

namespace CoffeeApp.Services.APIClients
{
    public static class ApiClient
    {
        public static MobileServiceClient client;
        public static IMobileServiceSyncTable<Coffee> coffeeTable;
        public static IMobileServiceSyncTable<Users> usersTable;
        public static IMobileServiceSyncTable<Account> accountTable;
        public static IMobileServiceSyncTable<Orders> ordersTable;

        public static async Task Initialise()
        {
            if (client?.SyncContext?.IsInitialized ?? false)
                return;
            var appUrl = "{My Azure URL}";

            client = new MobileServiceClient(appUrl);
            var fileName = "Example.db";
            var store = new MobileServiceSQLiteStore(fileName);
            store.DefineTable<Account>();
            store.DefineTable<Coffee>();
            store.DefineTable<Orders>();
            store.DefineTable<Users>();


            await client.SyncContext.InitializeAsync(store);
            coffeeTable = client.GetSyncTable<Coffee>();
            accountTable = client.GetSyncTable<Account>();
            usersTable = client.GetSyncTable<Users>();
            ordersTable = client.GetSyncTable<Orders>();

        }
    }
}

当我运行该应用程序时,这就是我的 Xaml.cs 文件 Xamarin 表单后面堆积代码的地方。

async void RefreshList()
        {
            try
            {
                SalonsAPIClient salonsAPIClient = new SalonsAPIClient();

               // LVItems.ItemsSource = await App.CoffeeServices().GetCoffee();
                LVItems.ItemsSource = await salonsAPIClient.GetSalons();

            }
            catch (Exception ex) { await DisplayAlert("Err", $"{ex.ToString()}", "OK"); }

        }

这是开始抛出异常的代码行

  LVItems.ItemsSource = await salonsAPIClient.GetSalons();

任何帮助将不胜感激。谢谢。

标签: c#azuremobilexamarin.forms

解决方案


推荐阅读