⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 clientsyncagent.cs

📁 微软的行业应用解决方案示例
💻 CS
字号:
using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using Microsoft.Synchronization;
using Microsoft.Synchronization.Data;
using Microsoft.Synchronization.Data.SqlServerCe;
using HardwareDistributor.Business;
using System.Data.SqlServerCe;
using System.Threading;

namespace HardwareDistributor.Synchronization
{
    /// <summary>
    /// drives synchronization by looping through the tables to be synchronized 
    /// and calling the client and server sync providers to apply changes on the tables
    /// </summary>
    sealed class ClientSyncAgent : SyncAgent
    {
        private readonly static ClientSyncAgent _instance = new ClientSyncAgent();

        internal static ClientSyncAgent Instance
        {
            get { return _instance; }
        }

        public event EventHandler BakgroundSyncBegin;
        public event EventHandler BakgroundSyncEnd;

        public new void Synchronize()
        {
            RaiseBeginEvents();

            
            try
            {
                base.Synchronize();
            }
            finally
            {
                //ensure that the complementary events to the start events
                //are always raised, irrespective of the success or failure of 
                //the synchronization process.
                RaiseEndEvents();
            }

        }

        /// <summary>
        /// raises the events if using a background thread
        /// </summary>
        private void RaiseEndEvents()
        {
            if ( Thread.CurrentThread.IsBackground && null != BakgroundSyncEnd)
            {
                try
                {

                    BakgroundSyncEnd(this, new EventArgs());
                }
                catch
                {
                    /*
                  * eat up any errors. These are externally attached 
                  * events that should not interfere in the higher priority 
                  * Synchronization activity
                  * 
                  */
                }
            }
        }

        /// <summary>
        /// raises the events if using a background thread
        /// </summary>
        private void RaiseBeginEvents()
        {
            if (Thread.CurrentThread.IsBackground && null != BakgroundSyncBegin)
            {

                try
                {
                    BakgroundSyncBegin(this, new EventArgs());
                }
                catch
                {
                    /*
                     * eat up any errors. These are externally attached 
                     * events that should not interfere in the higher priority 
                     * Synchronization activity
                     * 
                     */
                }
            }


        }

        /// <summary>
        /// hook up the client and server providers to be called. Also mark the tables to be 
        /// synchronized.
        /// </summary>
        private ClientSyncAgent()
        {

            //hook up client provider to call for client side synchronization
            string connectionString = GlobalCache.Instance.ConnectionString;
            this.LocalProvider = new SqlCeClientSyncProvider(connectionString);

            //configure conflict resolution so that server always wins
            SqlCeClientSyncProvider localCEProvider = ((SqlCeClientSyncProvider)this.LocalProvider);
            localCEProvider.ConflictResolver.ClientDeleteServerUpdateAction = ResolveAction.ServerWins;
            localCEProvider.ConflictResolver.ClientInsertServerInsertAction = ResolveAction.ServerWins;
            localCEProvider.ConflictResolver.ClientUpdateServerDeleteAction = ResolveAction.ServerWins;
            localCEProvider.ConflictResolver.ClientUpdateServerUpdateAction = ResolveAction.ServerWins;
            localCEProvider.ConflictResolver.StoreErrorAction = ResolveAction.ServerWins;


            //this provider will be used internally to make the server side syncing calls
            this.RemoteProvider = new ProxyServerSyncProvider(GlobalCache.Instance.InternetUrl);

            //setup tables to be synchronized
            SyncGroup uberGroup = new SyncGroup("UberGroup");
            
            //download-only tables with incremental updates 
            this.Configuration.SyncTables.Add(
                SyncUtility.GetDownloadOnlySyncTable("Customers", uberGroup));

            this.Configuration.SyncTables.Add(
                SyncUtility.GetDownloadOnlySyncTable("DistributionCenters", uberGroup));

            this.Configuration.SyncTables.Add(
                SyncUtility.GetDownloadOnlySyncTable("Employees", uberGroup));

            this.Configuration.SyncTables.Add(
                SyncUtility.GetDownloadOnlySyncTable("OrderState", uberGroup));

            this.Configuration.SyncTables.Add(
                SyncUtility.GetDownloadOnlySyncTable("Roles", uberGroup));

            this.Configuration.SyncTables.Add(
                SyncUtility.GetDownloadOnlySyncTable("Routes", uberGroup));
                
            this.Configuration.SyncTables.Add(
                SyncUtility.GetDownloadOnlySyncTable("Trucks", uberGroup));


			// bi-directional tables

            //Orders
            SyncTable ordersSyncTable = new SyncTable("Orders");
            ordersSyncTable.CreationOption = TableCreationOption.UseExistingTableOrFail;
            ordersSyncTable.SyncDirection = SyncDirection.Bidirectional;
            ordersSyncTable.SyncGroup = uberGroup;
            this.Configuration.SyncTables.Add(ordersSyncTable);

            //OrderDetails
            SyncTable orderdetailsSyncTable = new SyncTable("OrderDetails");
            orderdetailsSyncTable.CreationOption = TableCreationOption.UseExistingTableOrFail;
            orderdetailsSyncTable.SyncDirection = SyncDirection.Bidirectional;
            orderdetailsSyncTable.SyncGroup = uberGroup;
            this.Configuration.SyncTables.Add(orderdetailsSyncTable);

            //Inventory
            SyncTable inventorySyncTable = new SyncTable("Inventory");
            inventorySyncTable.CreationOption = TableCreationOption.UseExistingTableOrFail;
            inventorySyncTable.SyncDirection = SyncDirection.Bidirectional;
            inventorySyncTable.SyncGroup = uberGroup;
            this.Configuration.SyncTables.Add(inventorySyncTable);


        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -