Click here to Skip to main content
Click here to Skip to main content

ADO.NET Sync Services in N Tier Architecture

, 7 Oct 2008
Rate this:
Please Sign up or sign in to vote.
The article is a follow up article to Introduction to ADO.NET Sync Services and takes a look at how easily we can convert a 2 Tier Sync application to N tier using WCF services.
Prize winner in Competition "Best VB.NET article of October 2008"

The article is a follow up article to Introduction to ADO .NET Sync Services and takes a look at how easily we can convert a two Tier Sync application to N tier using WCF services. The ability to sync up your data without connecting to the main database is a great value and opens the door to many complex architecture implementations. There are many examples on sync services online but most of them are in C#, hence here I am just trying to have very basic examples for the VB community.

Background

In the previous article we saw how easily we can create an occasionally connected smart client application using ADO.NET Sync services, but it may not work in many architects where your database server is not exposed to the clients for security reasons or due to some other restrictions like network restrictions. In those cases you need to sync the client using an intermediate service. The beauty of the Sync Framework is that it makes even that task a lot easier, you can communicate on any channel using any protocol and still sync up your data with the main data source in many configurable ways.

Using the Code

In this example I will continue from where we left in Introduction to ADO Sync Services, and you can download the project from the article and continue the steps below.

In the current project (2 tier example) we have a client connected to the main SQL server and syncing up directly. Both the server sync provider and client sync provider are residing on our client project. In order to make it N tier all we have to do is move the server sync provider out of the client project into a separate service.

Architecture of 2 Tier Sync Applications

Architecture of N Tier Sync Applications

As you can see from the architecture above the Server Sync Provider and Sync Adapter has been moved to separate service and the client Sync Agent talks with Server Sync provider using service proxy using any transport protocol.

Now let's see how we can convert our current project. First add a new project to our solution of type WCF Service Library and name it WcfSyncService1 (ok you don't have to name it WcfSyncService1, you can call it Mickey mouse or whatever you want).

Now on the client project, double-click the LocalDataCache1.sync to open it. This file was generated by a studio when we added the local data cache to our project. Click the Advanced options to expand the options and change the Server project location to WCFSyncService1. In an earlier project both server project location and client project location were SyncService2Tier.

As soon as we click OK, the wizard moves the server sync provider to the WCFSyncService1 project by creating two new files, LocalDataCache1.Server.Sync and LocalDataCache1.Server.SyncContract.vb

To make our life easy, the service end points and the behavior definitions are provided in the LocalDataCache1.Server.SyncContract.vb file. All we have to do is uncomment it, copy it, and move it to appropriate section of the App.config file in our service project. Move the service end points under <services> under <system.serviceModel> and behavior section under <serviceBehaviors> under <behaviors>. Remember to make one change; the base address will be defined as localhost:8080/LocalDataCache1SyncService/ you may want to remove the port number from the URL as many times we have some other services running on port 8080, in which case it will give us an error while running the service. The best option for now is to remove the port number from the URL and leave it as just localhost/LocalDataCache1SyncService/

Now the last manual thing we have to do is to move our custom written code from the client project to our service. We had written our custom conflict handling code under LocalDataCache1ServerSyncProvider in our client sync provider (LocalDataCache1.Sync). View code for LocalDataCach1.sync in the client project and cut past the following code to LocalDataCache1.Server.sync. The studio does not automatically move this code to service as it is custom code written by us.

Partial Class LocalDataCache1ServerSyncProvider

    Private Sub LocalDataCache1ServerSyncProvider_ApplyChangeFailed _
                  (ByVal sender As Object, _
                   ByVal e As Microsoft.Synchronization.Data.ApplyChangeFailedEventArgs) _
                   Handles Me.ApplyChangeFailed

        e.Action = Microsoft.Synchronization.Data.ApplyAction.RetryWithForceWrite

     End Sub

End Class

Let's make our WCF project the start up project and run it. Copy the address for the service by right clicking on the running service.

Add a new service reference to our client project. Now set the remote provider for our server sync provider to our new service proxy. For this, right click the LocalDataCache1.sync on the client project and do View code. Add this definition under OnInitialized.

Me.RemoteProvider = New Microsoft.Synchronization.Data.ServerSyncProviderProxy(
    New ServiceReference1.LocalDataCache1SyncContractClient)

And you are done. Just set your client project as a startup project and run it. Remember to keep your service running while you are running the client project. You have an N tier smart client application which will run in occasionally connected environment and it will sync data on demand using the WCF service.

Points of Interest

This is a real simple example showing how you can utilize services to sync up your client data with master database. I will try to post more articles on sync services using VB.NET and exploring more advanced features and complex scenarios.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Vishal Shukla
Architect L&T Infotech
India India
Vishal has over 17 years of IT experience with over 15 years on Microsoft technologies from QW Basic to .net 4.5. Vishal works as a Lead Architect for the L&T Infotech. He has given various presentaions at code camps, Teched Tweener and various user group events.
Winner of INETA Community Champions award for 2008 Q3.
Microsoft VB .Net MVP 2009-2011

Comments and Discussions

 
QuestionSyncProgressEventArgs, ApplyChangeFailedEventArgs is possilbe use when using wcf? PinmemberPetersy11-Dec-08 6:51 
AnswerRe: SyncProgressEventArgs, ApplyChangeFailedEventArgs is possilbe use when using wcf? PinmemberVishal Shukla18-Dec-08 10:22 
GeneralTime out when try delete PinmemberPetersy5-Dec-08 3:11 
GeneralRe: Time out when try delete PinmemberVishal Shukla7-Dec-08 2:15 
GeneralRe: Time out when try delete PinmemberPetersy9-Dec-08 20:57 
GeneralSynchronization between sql server database PinmemberSabid M29-Nov-08 18:51 
GeneralRe: Synchronization between sql server database PinmemberVishal Shukla1-Dec-08 12:24 
GeneralMy vote of 1 Pinmemberadospace.NET28-Nov-08 5:06 
Very low quality article
GeneralRe: My vote of 1 PinmemberVishal Shukla1-Dec-08 12:19 
GeneralCongratulations !!! Pinmemberashu fouzdar24-Nov-08 23:22 
GeneralSame as videos PinmemberDave Elliott14-Oct-08 2:11 
GeneralRe: Same as videos PinmemberVishal Shukla14-Oct-08 2:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web03 | 2.8.140814.1 | Last Updated 7 Oct 2008
Article Copyright 2008 by Vishal Shukla
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid