1.向 N 層應用程式添加本機資料庫緩衝
Visual Studio 上下文中的“本機資料庫緩衝”是 SQL Server Compact 資料庫,
該資料庫配置為使用 Microsoft Synchronization Services for ADO.NET 與遠端資料庫進行資料同步。
2.向 RefactorNTierWalkthrough 添加本機資料庫緩衝
由於本機資料庫緩衝是一個位於用戶端上的 SQL Server Compact資料庫,
因此將本機資料庫緩衝添加到 RefactorNTierWalkthrough用戶端項目上。
本例將緩衝 Customers 表,因此將本機資料庫緩衝命名為 CustomersCache。
a)在“方案總管”中右擊“RefactorNTierWalkthrough”,再單擊“添加新項”。
b)單擊“本機資料庫緩衝”模板。
c)在“名稱”中鍵入“CustomersCache”。
d)單擊“添加”。
“配置資料同步”對話方塊隨即開啟。
此時的項目結構:
3.在現有資料服務中啟用同步
產生的同步群組件已添加到 DataService 項目中,但還需要通過服務來實現它們。
產生的 SyncContract 包含服務所需的資訊。此資訊在檔案中顯示為注釋。
修改DataService項目的App.config:
View Code
<service name="DataService.Service1"> <host> <baseAddresses> <add baseAddress="http://localhost:8732/Design_Time_Addresses/DataService/Service1/" /> </baseAddresses> </host> <!-- Service Endpoints --> <!-- 除非完全限定,否則地址將與上面提供的基址相關 --> <endpoint address="" binding="wsHttpBinding" contract="DataService.IService1"> <!-- 部署時,應刪除或替換下列標識元素,以反映 用來運行所部署服務的標識。刪除之後,WCF 將 自動推斷相應標識。 --> <identity> <dns value="localhost"/> </identity> </endpoint> <!-- Metadata Endpoints --> <!-- 中繼資料交換終結點供相應的服務用於向用戶端做自我介紹。 --> <!-- 此終結點不使用安全綁定,應在部署前確保其安全或將其刪除--> <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/> <endpoint address ="SyncServer" binding="wsHttpBinding" contract="DataService.ICustomersCacheSyncContract"/> </service>
4.向現有的資料服務添加同步服務作業
修改DataService項目的CustomersCache.Server.SyncContract
View Code
public partial class Service1 : object, ICustomersCacheSyncContract { private CustomersCacheServerSyncProvider _serverSyncProvider; public Service1() { this._serverSyncProvider = new CustomersCacheServerSyncProvider(); } [System.Diagnostics.DebuggerNonUserCodeAttribute()] public virtual SyncContext ApplyChanges(SyncGroupMetadata groupMetadata, DataSet dataSet, SyncSession syncSession) { return this._serverSyncProvider.ApplyChanges(groupMetadata, dataSet, syncSession); } [System.Diagnostics.DebuggerNonUserCodeAttribute()] public virtual SyncContext GetChanges(SyncGroupMetadata groupMetadata, SyncSession syncSession) { return this._serverSyncProvider.GetChanges(groupMetadata, syncSession); } [System.Diagnostics.DebuggerNonUserCodeAttribute()] public virtual SyncSchema GetSchema(Collection<string> tableNames, SyncSession syncSession) { return this._serverSyncProvider.GetSchema(tableNames, syncSession); } [System.Diagnostics.DebuggerNonUserCodeAttribute()] public virtual SyncServerInfo GetServerInfo(SyncSession syncSession) { return this._serverSyncProvider.GetServerInfo(syncSession); } } [ServiceContractAttribute()] public interface ICustomersCacheSyncContract { [OperationContract()] SyncContext ApplyChanges(SyncGroupMetadata groupMetadata, DataSet dataSet, SyncSession syncSession); [OperationContract()] SyncContext GetChanges(SyncGroupMetadata groupMetadata, SyncSession syncSession); [OperationContract()] SyncSchema GetSchema(Collection<string> tableNames, SyncSession syncSession); [OperationContract()] SyncServerInfo GetServerInfo(SyncSession syncSession); }
5.發布服務
6.更新服務引用
7.修改Form1.cs
添加"載入資料"後台代碼
View Code
private void button1_Click(object sender, EventArgs e) { using (var client = new ServiceReference1.Service1Client()) { //擷取本地的customer表 var customersTableAdapter = new LocalNorthwindCustomersTableAdapters.CustomersTableAdapter(); northwindDataSet.Customers.Merge(customersTableAdapter.GetData()); //使用wcf擷取遠端的Order表 northwindDataSet.Orders.Merge(client.GetOrders()); } }
8.運行測試.
9.同步資料
現在展示層已設定就緒,可以從正確的源顯示表,下一步是添加代碼來啟動同步。
將在表單中添加一個按鈕來啟動同步進程。
修改Form1.cs
添加"同步資料"後台代碼:
View Code
private void button2_Click(object sender, EventArgs e) { CustomersCacheSyncAgent syncAgent = new CustomersCacheSyncAgent(); using (ServiceReference1.CustomersCacheSyncContractClient syncClient = new ServiceReference1.CustomersCacheSyncContractClient()) { syncAgent.RemoteProvider = new Microsoft.Synchronization.Data.ServerSyncProviderProxy(syncClient); Microsoft.Synchronization.Data.SyncStatistics syncStats = syncAgent.Synchronize(); northwindDataSet.Customers.Merge(new LocalNorthwindCustomersTableAdapters.CustomersTableAdapter().GetData()); string syncSummary = "Total changes downloaded: " + syncStats.TotalChangesDownloaded.ToString() + Environment.NewLine + "Last successful synchronization: " + syncStats.SyncCompleteTime.ToString(); MessageBox.Show(syncSummary); } }
10.運行測試
11.完成
轉載請註明出處:http://www.cnblogs.com/refactor