C# Microsoft同步框架冲突事件未触发

C# Microsoft同步框架冲突事件未触发,c#,microsoft-sync-framework,C#,Microsoft Sync Framework,我是Microsoft Sync Framework和im测试的新手,使用Microsoft提供的以下示例(请参阅): 使用系统; 使用System.Collections.Generic; 使用System.Linq; 使用系统文本; 使用System.Data.SqlClient; 使用Microsoft.Synchronization; 使用Microsoft.Synchronization.Data; 使用Microsoft.Synchronization.Data.SqlServer;

我是Microsoft Sync Framework和im测试的新手,使用Microsoft提供的以下示例(请参阅):

使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Data.SqlClient;
使用Microsoft.Synchronization;
使用Microsoft.Synchronization.Data;
使用Microsoft.Synchronization.Data.SqlServer;
使用Microsoft.Synchronization.Data.SqlServerCe;
命名空间ExecuteExpressSync
{
班级计划
{
静态void Main(字符串[]参数)
{
SqlConnection clientConn=newsqlconnection(@“数据源=。\SQLCLIENT;初始目录=SyncExpressDB;可信的_连接=Yes”);
SqlConnection serverConn=newsqlconnection(“数据源=localhost\\SQLEXPRESS;初始目录=SyncDB;集成安全性=True”);
//创建sync或hcestrator
SyncOrchestrator SyncOrchestrator=新的SyncOrchestrator();
//将orchestrator的本地提供程序设置为与关联的同步提供程序
//SyncExpressDB express客户端数据库中的ProductsScope
syncOrchestrator.LocalProvider=新的SqlSyncProvider(“ProductsScope”,clientConn);
//将orchestrator的远程提供程序设置为与关联的服务器同步提供程序
//SyncDB服务器数据库中的ProductsScope
syncOrchestrator.RemoteProvider=新的SqlSyncProvider(“ProductsScope”,serverConn);
//将同步会话的方向设置为上载和下载
syncOrchestrator.Direction=SyncDirectionOrder.UploadAndDownload;
//订阅将更改应用到客户端时发生的错误
((SqlCeSyncProvider)syncOrchestrator.RemoteProvider).ApplyChangeFailed+=新事件处理程序(程序\u ApplyChangeFailed);
((SqlCeSyncProvider)syncOrchestrator.LocalProvider).ApplyChangeFailed+=新事件处理程序(程序\u ApplyChangeFailed);
// 
制造冲突(clientConn,“999”);
makeConflict(serverConn,“666”);
//执行同步过程
SyncOperationStatistics syncStats=syncOrchestrator.Synchronize();
//打印统计数据
Console.WriteLine(“开始时间:+syncStats.SyncStartTime”);
Console.WriteLine(“上传的总更改数:+syncStats.UploadChangesTotal”);
Console.WriteLine(“下载的总更改数:+syncStats.DownloadChangesTotal”);
Console.WriteLine(“下载失败:+syncStats.DownloadChanges失败”);
Console.WriteLine(“上载更改失败:+syncStats.UploadChanges失败”);
Console.WriteLine(“完成时间:+syncStats.SyncEndTime”);
Console.WriteLine(String.Empty);
Console.ReadLine();
}
静态无效程序\u ApplyChangeFailed(对象发送方,DbApplyChangeFailedEventArgs e)
{
//显示冲突类型
Console.WriteLine(如Conflict.Type);
//显示错误消息
控制台写入线(如错误);
}
私有静态void makeConflict(SqlConnection nodecon,字符串价格)
{
int rowCount=0;
使用(nodecon)
{
SqlCommand SqlCommand=nodecon.CreateCommand();
sqlCommand.CommandText=“更新产品集ListPrice=“+price+”其中Name='PCClient';
nodecon.Open();
rowCount=sqlCommand.ExecuteNonQuery();
nodecon.Close();
}
}
}
}
我在本地pc上有两个SQL Server实例(SQLCLIENT和SQLEXPRESS)用于测试环境。我的问题是,尽管我通过调用makeConflict()创建了一个冲突,但applychangefiled事件并没有触发,它对products表中的一行执行更新,一次更新服务器和客户端。结果是服务器或客户端获胜,具体取决于SyncDirectionOrder属性

我做错了什么?

更改此选项:

((SqlCeSyncProvider)同步协调器

为此:

((SqlSyncProvider)syncOrchestrator


您使用的是SqlSyncProvider,而不是SqlCeSyncProvider,以避免进一步混淆。以下是实际代码。问题仍然存在

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
            ((SqlSyncProvider)syncOrchestrator.RemoteProvider).ApplyChangeFailed += new EventHandler<DbApplyChangeFailedEventArgs>(Program_ApplyChangeFailed);
            ((SqlSyncProvider)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();

            }
        }

    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Data.SqlClient;
使用Microsoft.Synchronization;
使用Microsoft.Synchronization.Data;
使用Microsoft.Synchronization.Data.SqlServer;
使用Microsoft.Synchronization.Data.SqlServerCe;
命名空间ExecuteExpressSync
{
班级计划
{
静态void Main(字符串[]参数)
{
SqlConnection clientConn=newsqlconnection(@“数据源=。\SQLCLIENT;初始目录=SyncExpressDB;可信的_连接=Yes”);
SqlConnection serverConn=newsqlconnection(“数据源=localhost\\SQLEXPRESS;初始目录=SyncDB;集成安全性=True”);
//创建sync或hcestrator
SyncOrchestrator SyncOrchestrator=新的SyncOrchestrator();
//将orchestrator的本地提供程序设置为与关联的同步提供程序
//SyncExpressDB express客户端数据库中的ProductsScope
syncOrchestrator.LocalProvider=新的SqlSyncProvider(“ProductsScope”,clientConn);
//将orchestrator的远程提供程序设置为与关联的服务器同步提供程序
//SyncDB服务器数据库中的ProductsScope
syncOrchestrator.RemoteProvider=新的SqlSyncProvider(“ProductsScope”,serverConn);
//将同步会话的方向设置为上载和下载
syncOrchestrator.Direction=SyncDirectionOrder.UploadAndDownload;
//订阅将更改应用到客户端时发生的错误
((SqlSyncProvider)syncOrchestrator.RemoteProvider).ApplyChangeFailed+=新事件处理程序(Progr)
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
            ((SqlSyncProvider)syncOrchestrator.RemoteProvider).ApplyChangeFailed += new EventHandler<DbApplyChangeFailedEventArgs>(Program_ApplyChangeFailed);
            ((SqlSyncProvider)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();

            }
        }

    }
}