C# 调查或比较Azure脱机数据同步中推拉的数据
我有以下Azure移动客户端C# 调查或比较Azure脱机数据同步中推拉的数据,c#,sqlite,azure,xamarin,azure-mobile-services,C#,Sqlite,Azure,Xamarin,Azure Mobile Services,我有以下Azure移动客户端 public AzureCloudService() { Client = new MobileServiceClient(AzureUrl, new CustomAzureClientMessageHandler()); } 我将以下消息处理程序附加到该客户端 public class CustomAzureClientMessageHandler : DelegatingHandler { protected overr
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;
}
}
公共类CustomAzureClientMessageHandler:DelegatingHandler
{
受保护的覆盖任务SendAsync(HttpRequestMessage请求,
取消令牌(取消令牌)
{
//这里有任何预先要求吗
request.Headers.Add(“UserId”,Settings.UserId);
//请求发生在这里
var response=base.sendaync(请求、取消令牌);
//这里有任何职位要求吗
返回响应;
}
}
我通过以下几行代码将本地数据与服务器同步
// Push the Operations Queue to the mobile backed
await Client.SyncContext.PushAsync();
// Pull each sync table
var table = await GetTableAsync<T>();
await table.PullAsync();
//将操作队列推送到移动备份
等待Client.SyncContext.PushAsync();
//拉动每个同步表
var table=await GetTableAsync();
wait table.PullAsync();
问题是我需要调查/比较同步推拉的数据
1) 有没有办法查看在同步调用中推拉哪些数据?可能正在使用我上面提到的消息处理程序
2) 或者是否有任何方法可以在移动客户端的TableController中执行相同的操作
当同步出现问题时,调试东西确实很困难
在某些情况下,您希望捕获并处理客户端中的同步冲突。通过实现IMobileServiceSyncHandler接口并在初始化上下文时传递它,可以控制所有同步操作。例如,这是一个同步处理程序的实现,它跟踪正在发生的所有操作
classMySyncHandler:imobileservicesynchHandler
{
主页;
公共MySynchHandler(主页)
{
this.page=page;
}
publicTask ExecuteTableOperationAsync(IMobileServiceTableOperation)
{
AddToDebug(“为表“{1}”执行操作“{0}”,operation.Kind,operation.table.Name);
返回操作。ExecuteAsync();
}
publicTask OnPushCompleteTasync(移动服务PushCompletionResult)
{
AddToDebug(“推送结果:{0}”,result.Status);
foreach(result.Errors中的变量错误)
{
AddToDebug(“推送错误:{0}”,error.Status);
}
returnTask.FromResult(0);
}
}
我们可以通过将此同步处理程序传递给同步上下文中InitializeAsync的重载来使用它,如下所示:
var-store=newmobileseservicesqlitestore(StoreFileName);
store.DefineTable();
AddToDebug(“存储中定义的表”);
var syncHandler=newMySyncHandler(此);
等待client.SyncContext.InitializeAsync(存储,syncHandler);
AddToDebug(“已初始化同步上下文”);
我找到了实现这一点的解决方案。我从以下示例中获得了它。感谢作者
解决办法是
/// <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;
}
}
//
///扩展的SQlite存储,可以记录SQlite数据库中发生的操作
///
带有日志记录的公共类MobileServiceSQLiteStore:MobileServiceSQLiteStore
{
私人bool日志结果;
私有布尔对数参数;
public MobileServiceSqliteStorehithLogging(字符串文件名,bool logResults=false,bool logParameters=false)
:base(文件名)
{
this.logResults=logResults;
this.logParameters=logParameters;
}
受保护的覆盖IList ExecuteQueryInternal(字符串表名、字符串sql、,
(字典参数)
{
Debug.WriteLine(sql);
if(日志参数)
打印字典(参数);
var result=base.ExecuteQueryInternal(表名、sql、参数);
if(logResults&&result!=null)
{
foreach(结果中的var标记)
Debug.WriteLine(令牌);
}
返回结果;
}
受保护的覆盖无效ExecuteOnQueryInternal(字符串sql、IDictionary参数)
{
Debug.WriteLine(sql);
if(日志参数)
打印字典(参数);
base.ExecuteNonQueryInternal(sql,参数);
}
专用void打印字典(IDictionary字典)
{
if(dictionary==null)
返回;
foreach(字典中的var对)
Debug.WriteLine(“{0}:{1}”,pair.Key,pair.Value);
}
}
///
///消息处理程序,允许通过Azure移动客户端传递客户头以及记录请求、响应等
///
公共类CustomAzureClientMessageHandler:DelegatingHandler
{
私有bool日志库;
公共CustomAzureClientMessageHandler(bool LogRequestResponseBy=false)
{
this.logRequestResponseBody=logRequestResponseBody;
}
受保护的覆盖异步任务SendAsync(
HttpRequestMessage请求,
取消令牌(取消令牌)
{
WriteLine(“正在发送请求:{0}{1}”,Request.Method,Request.RequestUri.ToString());
if(logRequestResponseBody&&request.Content!=null)
{
var requestContent=await request.Content.ReadAsStringAsync();
Debug.WriteLine(请求内容);
}
WriteLine(“请求中的头”);
foreach(request.Headers中的var头)
{
Debug.WriteLine(string.Format(“{0}:{1}”,header.Key,string.Join(“,”,header.Value));
}
var response=await base.sendaync(请求、取消令牌);
WriteLine(“来自服务器的响应:{0}”,Response.StatusCode);
if(logRequestResponseBody)
{
var responseContent=await response.Content.ReadAsStringAsync();
调试.Wri
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");
/// <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;
}
}