我新的Microsoft同步框架,並與微軟下面的示例IM測試(見http://msdn.microsoft.com/en-us/library/ff928758.aspx):微軟同步框架衝突事件不火
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using Microsoft.Synchronization;
using Microsoft.Synchronization.Data;
using Microsoft.Synchronization.Data.SqlServer;
using Microsoft.Synchronization.Data.SqlServerCe;
namespace ExecuteExpressSync
{
class Program
{
static void Main(string[] args)
{
SqlConnection clientConn = new SqlConnection(@"Data Source=.\SQLCLIENT; Initial Catalog=SyncExpressDB; Trusted_Connection=Yes");
SqlConnection serverConn = new SqlConnection("Data Source=localhost\\SQLEXPRESS; Initial Catalog=SyncDB; Integrated Security=True");
// create the sync orhcestrator
SyncOrchestrator syncOrchestrator = new SyncOrchestrator();
// set local provider of orchestrator to a sync provider associated with the
// ProductsScope in the SyncExpressDB express client database
syncOrchestrator.LocalProvider = new SqlSyncProvider("ProductsScope", clientConn);
// set the remote provider of orchestrator to a server sync provider associated with
// the ProductsScope in the SyncDB server database
syncOrchestrator.RemoteProvider = new SqlSyncProvider("ProductsScope", serverConn);
// set the direction of sync session to Upload and Download
syncOrchestrator.Direction = SyncDirectionOrder.UploadAndDownload;
// subscribe for errors that occur when applying changes to the client
((SqlCeSyncProvider)syncOrchestrator.RemoteProvider).ApplyChangeFailed += new EventHandler<DbApplyChangeFailedEventArgs>(Program_ApplyChangeFailed);
((SqlCeSyncProvider)syncOrchestrator.LocalProvider).ApplyChangeFailed += new EventHandler<DbApplyChangeFailedEventArgs>(Program_ApplyChangeFailed);
//
makeConflict(clientConn, "999");
makeConflict(serverConn, "666");
// execute the synchronization process
SyncOperationStatistics syncStats = syncOrchestrator.Synchronize();
// print statistics
Console.WriteLine("Start Time: " + syncStats.SyncStartTime);
Console.WriteLine("Total Changes Uploaded: " + syncStats.UploadChangesTotal);
Console.WriteLine("Total Changes Downloaded: " + syncStats.DownloadChangesTotal);
Console.WriteLine("Download failed: " + syncStats.DownloadChangesFailed);
Console.WriteLine("Upload Changes failed: " + syncStats.UploadChangesFailed);
Console.WriteLine("Complete Time: " + syncStats.SyncEndTime);
Console.WriteLine(String.Empty);
Console.ReadLine();
}
static void Program_ApplyChangeFailed(object sender, DbApplyChangeFailedEventArgs e)
{
// display conflict type
Console.WriteLine(e.Conflict.Type);
// display error message
Console.WriteLine(e.Error);
}
private static void makeConflict(SqlConnection nodeConn, String price)
{
int rowCount = 0;
using (nodeConn)
{
SqlCommand sqlCommand = nodeConn.CreateCommand();
sqlCommand.CommandText = "UPDATE Products SET ListPrice = " + price + " WHERE Name = 'PCClient' ";
nodeConn.Open();
rowCount = sqlCommand.ExecuteNonQuery();
nodeConn.Close();
}
}
}
}
我有兩個SQL-Server實例(SQLCLIENT和SQLEXPRESS )在我的本地PC上測試環境。我的問題是ApplyChangeFailed事件不會觸發,儘管我通過調用makeConflict()來創建衝突,makeConflict()一次爲產品表中的一行執行UPDATE,一次爲服務器和客戶端執行UPDATE。結果是服務器或客戶端都會贏,具體取決於SyncDirectionOrder屬性。
我在做什麼錯了?
在調用同步之前,您是否可以確認正在提交的更新正在提交?或者檢查_tracking表是否實際更新? – JuneT
是的,更新將在調用同步之前提交。 _tracking表中的「last_change_」時間戳已被更新。如果我在Management Studio中手動更改值或調用makeConflict(),也沒有區別。行爲是一樣的。當將SyncDirectionOrder設置爲UploadAndDownload時,服務器獲勝,通過DownloadAndUpload,客戶端獲勝。 – user2516796