Today is June 3rd 2008 and I am writing this blog post to demonstrate a step by step guide to developing an ADO.NET Sync Services Sample Application that is using Service Based Architecture. Though I planned to first write a post on ADO.NET Sync Services as a whole from architecture perspective and then provide a step by step instructions for the sample code, but now, when I have completed the step by step instructions before the architecture document, I am posting the same.
In almost every practical case of disconnected applications architecture, the server side central database is exposed via http end points like web services. Moreover with the release of technologies like ADO.NET Data Services, it is a matter of few clicks to expose the entire data model to the external world using web services.
So I thought of writing down the steps that I took in Visual Studio 2008 to create a client windows application which will be talking to the central database server via a http WCF Service. If you are interested in the sample code as well, please feel free to get in touch with me.
using Microsoft.Synchronization; using Microsoft.Synchronization.Data; using System.Data; using System.Collections.ObjectModel; And the class declaration looks similar to this: public partial class RemoteProviderClass:ServerSyncProvider
using Microsoft.Synchronization; using Microsoft.Synchronization.Data; using System.Data; using System.Collections.ObjectModel;
And the class declaration looks similar to this: public partial class RemoteProviderClass:ServerSyncProvider
public override SyncSchema GetSchema(Collection<string> tableNames, SyncSession syncSession) { string[] tables = new string[tableNames.Count]; tableNames.CopyTo(tables, 0); return this._serverSyncProvider.GetSchema(tables, syncSession); }
partial void OnInitialized() { this.RemoteProvider = new ProxyForWebServiceCall(); this.Employee.SyncDirection = SyncDirection.Snapshot; }
Now we will create a UI on the windows form and invoke the synchronization process on timer tick event. Calling synchronization is straightforward but calling inside a timer that is invoked every 1 second is a little bit tricky. Practically, web service might return response in 1 second, and also till web service returns back, the UI thread will be unresponsive, so we will invoke the synchronization code asynchronously and only when one web service call has returned. I will illustrate this in following steps and also show the sample code to make things more comprehensive.
public partial class Form1 : Form { bool IsContinue = true; private delegate SyncStatistics SynchronizeMethod(); LocalDataCache1SyncAgent syncAgent = new LocalDataCache1SyncAgent();
if (!IsContinue) return; else IsContinue = false;
syncAgent.Employee.SyncDirection = Microsoft.Synchronization.Data.SyncDirection.Bidirectional;
SynchronizeMethod syncMethod = new SynchronizeMethod(syncAgent.Synchronize); IAsyncResult result = call.BeginInvoke(new AsyncCallback(MyCallBack), null);
Microsoft.Synchronization.Data.SyncStatistics syncStats = syncMethod.EndInvoke(result); if (syncStats.TotalChangesDownloaded > 0) Form1_Load(null, null);
private void MyCallBack(IAsyncResult result) { IsContinue = true; }
Compile and run the application by setting windows application as startup project. Make changes from windows application and you can see them at SQL server and similarly make changes in SQL Server and they will be visible in Windows Application datagrid. Make sure that you do not change primary key field. This is just a start, go ahead and implement conflict resolution and try to make changes from multiple machines at same time.
I would love to see your comments/suggestions/corrections if any.
Rahul Gangwar