调查或比较Azure脱机数据同步中推送和提取的数据

问题描述 投票:0回答:2

我有以下Azure移动客户端

public AzureCloudService()
{
    Client = new MobileServiceClient(AzureUrl, new CustomAzureClientMessageHandler());

}

我将以下消息处理程序附加到该客户端

   public class CustomAzureClientMessageHandler : DelegatingHandler
    {
        protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request,
            CancellationToken cancellationToken)
        {
            // Do any pre-request requirements here
            request.Headers.Add("UserId", Settings.UserId);

            // Request happens here
            var response = base.SendAsync(request, cancellationToken);

            // Do any post-request requirements here
            return response;
        }
    }

我在以下行的帮助下与服务器同步本地数据。

  // Push the Operations Queue to the mobile backed
            await Client.SyncContext.PushAsync();

 // Pull each sync table
            var table = await GetTableAsync<T>();
            await table.PullAsync();

问题是我需要调查/比较同步中推送和拉取的数据。

1)有没有办法查看在同步调用中推送和拉出的数据?可能正在使用我上面提到的消息处理程序?

2)或者是否有任何方法可以在Table Controller中执行相同的操作而不是移动客户端?

当有同步问题时,很难调试东西

c# sqlite azure xamarin azure-mobile-services
2个回答
1
投票

https://blogs.msdn.microsoft.com/azuremobile/2014/04/07/deep-dive-on-the-offline-support-in-the-managed-client-sdk/

在某些情况下,您希望捕获并处理客户端中的同步冲突。您可以通过实现IMobileServiceSyncHandler接口并在初始化上下文时传递它来控制所有同步操作。例如,这是一个跟踪所有正在发生的操作的同步处理程序的实现。

classMySyncHandler : IMobileServiceSyncHandler
{
    MainPage page;

    public MySyncHandler(MainPage page)
    {
        this.page = page;
    }

    publicTask<JObject> ExecuteTableOperationAsync(IMobileServiceTableOperation operation)
    {
        page.AddToDebug("Executing operation '{0}' for table '{1}'", operation.Kind, operation.Table.Name);
        return operation.ExecuteAsync();
    }

    publicTask OnPushCompleteAsync(MobileServicePushCompletionResult result)
    {
        page.AddToDebug("Push result: {0}", result.Status);
        foreach (var error in result.Errors)
        {
            page.AddToDebug("  Push error: {0}", error.Status);
        }

        returnTask.FromResult(0);
    }
}

我们可以通过在同步上下文中将其传递给InitializeAsync的重载来使用此同步处理程序,如下所示:

var store = newMobileServiceSQLiteStore(StoreFileName);
store.DefineTable<TodoItem>();
AddToDebug("Defined table in the store");

var syncHandler = newMySyncHandler(this);
await client.SyncContext.InitializeAsync(store, syncHandler);
AddToDebug("Initialized the sync context");

0
投票

我找到了实现这个目标的解决方案。我从以下示例中得到了它。感谢作者

https://github.com/Azure-Samples/app-service-mobile-dotnet-todo-list-files/blob/master/src/client/MobileAppsFilesSample/Helpers/LoggingHandler.cs#L12

解决方案是

  1. 扩展SQLite存储以启用日志记录
  2. 使用消息处理程序进行记录

/// <summary>
/// Extended SQlite Store which can log operations happen in the SQLite database
/// </summary>
public class MobileServiceSQLiteStoreWithLogging : MobileServiceSQLiteStore
{
    private bool logResults;
    private bool logParameters;

    public MobileServiceSQLiteStoreWithLogging(string fileName, bool logResults = false, bool logParameters = false)
        : base(fileName)
    {
        this.logResults = logResults;
        this.logParameters = logParameters;
    }

    protected override IList<JObject> ExecuteQueryInternal(string tableName, string sql,
        IDictionary<string, object> parameters)
    {
        Debug.WriteLine(sql);

        if (logParameters)
            PrintDictionary(parameters);

        var result = base.ExecuteQueryInternal(tableName, sql, parameters);

        if (logResults && result != null)
        {
            foreach (var token in result)
                Debug.WriteLine(token);
        }

        return result;
    }


    protected override void ExecuteNonQueryInternal(string sql, IDictionary<string, object> parameters)
    {
        Debug.WriteLine(sql);

        if (logParameters)
            PrintDictionary(parameters);

        base.ExecuteNonQueryInternal(sql, parameters);
    }


    private void PrintDictionary(IDictionary<string, object> dictionary)
    {
        if (dictionary == null)
            return;

        foreach (var pair in dictionary)
            Debug.WriteLine("{0}:{1}", pair.Key, pair.Value);
    }
}

/// <summary>
/// Message Handler which enable to pass the customer headers as well as logging the Request, Response etc happen via the Azure Mobile client
/// </summary>
public class CustomAzureClientMessageHandler : DelegatingHandler
{
    private bool logRequestResponseBody;

    public CustomAzureClientMessageHandler(bool logRequestResponseBody = false)
    {
        this.logRequestResponseBody = logRequestResponseBody;
    }

    protected override async Task<HttpResponseMessage> SendAsync(
        HttpRequestMessage request,
        CancellationToken cancellationToken)
    {
        Debug.WriteLine("Request is being sent: {0} {1}", request.Method, request.RequestUri.ToString());

        if (logRequestResponseBody && request.Content != null)
        {
            var requestContent = await request.Content.ReadAsStringAsync();
            Debug.WriteLine(requestContent);
        }


        Debug.WriteLine("HEADERS in the request");

        foreach (var header in request.Headers)
        {
            Debug.WriteLine(string.Format("{0}:{1}", header.Key, string.Join(",", header.Value)));
        }

        var response = await base.SendAsync(request, cancellationToken);

        Debug.WriteLine("Response from server: {0}", response.StatusCode);

        if (logRequestResponseBody)
        {
            var responseContent = await response.Content.ReadAsStringAsync();
            Debug.WriteLine(responseContent);
        }

        return response;
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.