我有一个使用 Microsoft Sync Framework 的项目。除同步方向外,一切正常。我根据用户的选择设置同步协调器的同步方向是上传还是下载。
编辑:
我上传了一份文件以更好地解释情况:
代码:
private void RunSynch()
{
SqlConnection sqlServer = new SqlConnection(GetServerConnectionString());
SqlConnection sqlClient = new SqlConnection(GetClientConnectionString());
SyncOrchestrator syncOrchestrator = new SyncOrchestrator();
syncOrchestrator.LocalProvider = new SqlSyncProvider(strScopeName, sqlClient);
syncOrchestrator.RemoteProvider = new SqlSyncProvider(strScopeName, sqlServer);
switch(iButtonState) // int constant
{
case STATE_UPLOAD:
syncOrchestrator.Direction = SyncDirectionOrder.Upload;
break;
case STATE_DOWNLOAD:
syncOrchestrator.Direction = SyncDirectionOrder.Download;
break;
}
((SqlSyncProvider)syncOrchestrator.RemoteProvider).ApplyChangeFailed += new EventHandler<DbApplyChangeFailedEventArgs>(RemoteProvider_ApplyChangeFailed);
((SqlSyncProvider)syncOrchestrator.LocalProvider).ApplyChangeFailed += new EventHandler<DbApplyChangeFailedEventArgs>(LocalProvider_ApplyChangeFailed);
syncStats = syncOrchestrator.Synchronize();
}
private void RemoteProvider_ApplyChangeFailed(object sender, DbApplyChangeFailedEventArgs e)
{
if (rdoConflict.SelectedIndex == 0)// server wins option
{
e.Action = ApplyAction.Continue;
}
else
{
e.Action = ApplyAction.RetryWithForceWrite;
}
}
private void LocalProvider_ApplyChangeFailed(object sender, DbApplyChangeFailedEventArgs e)
{
if (rdoConflict.SelectedIndex == 0) // server wins option
{
e.Action = ApplyAction.RetryWithForceWrite;
}
else
{
e.Action = ApplyAction.Continue;
}
}
这里的问题是服务器数据库会被客户端更改更新,即使同步方向只是下载。
固定的:
我现在终于可以正常工作了。同步框架完全没有问题,但它是分配方向的开关部分,我调试并发现程序没有通过开关盒,这就是为什么方向总是双向的。