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
Do you know of any Sync Service examples which access non Microsoft Web services where all you start with is a WSDL file which defines the SOAP Web services for insert/update/deletes.
Regards,
Lewis.
Hi Lewis,
SmugMug.com is one real life example.
I am not sure about any sample code (and I haven't tried either) but yes, it is absolutely possible to have the scenario acheved having non-MS based services on server side. All we need to do is to imitate types like SyncContext, SyncSchema etc. on server side service (whatever technology it is) and make it the remote provider at client side.
You might need to write code from scratch to support the operations that server side component does. The point where you leave Microsoft tools, you will have to implement functionalities on your own that designer did.
In the case of replacing server side with non MS based technologies, you are actually implementing the other half of sync services. client side will keep functioning as expected.
The other way to do this is to use server side as well on MS based technology, like embdding server side code in .NET dll, and the calling that DLL from Non-MS based service. But I am not sure about scenario which would require this model.
Rahul's
SelectConflictUpdatedRowsCommand and Change Tracking אני עובד כרגע על איזה פרויקט המשתמש ב Sync Framework
hi
im using remote web server how do i include the remote connection to my smart client
please send me the sample code at fastian_Developer@yahoo.com
thanks
Hi, Mr. Raul, your tutorial matches with my assignment. may I ask a question whether I use mySql, can it be synchronised with sqlce by web service (localdatacache)?
Thank you,
agnes