Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.


Build Occasionally Connected Systems Using Sync Services for ADO.NET v1.0 : Page 4

When workers aren't always connected, Occasionally Connected Systems (OCS) can help. Find out how Visual Studio 8's Sync Services for ADO.NET enables OCS, synchronizing databases between different devices and computers.




Application Security Testing: An Integral Part of DevOps

Using Sync Services with Windows Communication Foundation
The previous example shows you how to use Sync Services to generate all the synchronization logic on the client side. In the real world, you might want to use a multi-tier architecture and host part of the synchronization logic on another server.

In this section, you'll modify the previous application and host the synchronization logic using a WCF service. First, add a new project to the Solution. Select the WCF Service Library template and name it WcfSyncService.

Back in the SyncServices project, double-click on the LocalAuthors.sync file to bring up the Configure Data Synchronization dialog again. In the Server project location dropdown listbox, select the WcfSyncService project (see Figure 11). This creates the synchronization logic in two different projects: the SyncServices project for the client component, and WcfSyncService for the server component. Click OK.

Figure 11. Select the WcfSyncService Project: Regenerating the synchronization logic for the server for the WCF service.
Figure 12. Three New Files: The WcfSyncService project now has three additional files added.

Notice that, in Solution Explorer, the WcfSyncService project will now have three additional files (see Figure 12).

Double-clicking on the LocalAuthors.Server.sync file will redirect you to the LocalAuthors.sync file in the SyncServices project. The LocalAuthors.Server.SyncContract.cs file contains the service contract for the WCF service. In the LocalAuthors.Server.SyncContract.cs file, change the class name to Service1, as shown in bold below:

namespace WcfSyncService { using System; using System.Data; using System.Collections.ObjectModel; using System.ServiceModel; using Microsoft.Synchronization.Data; public partial class Service1 : object, ILocalAuthorsSyncContract { private LocalAuthorsServerSyncProvider _serverSyncProvider; public Service1() { this._serverSyncProvider = new LocalAuthorsServerSyncProvider(); } ...

In the Service1.cs file, add the partial keyword to the definition of the Service1 class (see Listing 1).

With the changes made to the LocalAuthors.Server.SyncContract.cs and the Service1.cs files, you are essentially combining the two Service1 partial classes into one.

In the LocalAuthors.Server.SyncContract.cs file, locate the endpoint address and copy it into memory:

<endpoint address ="" binding="wsHttpBinding" contract="WcfSyncService.ILocalAuthorsSyncContract"/>

Paste the endpoint address into app.config and delete the <identity> element, as shown in Figure 13. This will define an endpoint for your WCF service.

Figure 13. Define an Endpoint: Paste in the endpoint address into and delete the <identity> element.
Figure 14. Testing the Application: Copying the working URL of the WCF service.

The next step is to set the WcfSyncService project as the startup project. Then press F5 to obtain its working URL. When the WCF Test Client has loaded, right-click on the URL of the service and select Copy Address (see Figure 14).

You can now add a service reference to the SyncServices project so that your application can connect to this service to synchronize its data. Right-click on the References item in the SyncServices project in Solution Explorer and select Add Service Reference.

Next, paste the URL you have copied earlier into the Address textbox and click Go. Use the default name of ServiceReference1 and click OK.

The last step is to right-click on the LocalAuthors.sync file and select View Code. Add the code shown in bold type below:

using Microsoft.Synchronization.Data; namespace SyncServices { public partial class LocalAuthorsSyncAgent { partial void OnInitialized() { this.authors.SyncDirection = Microsoft.Synchronization.Data.SyncDirection.Bidirectional; this.RemoteProvider = new ServerSyncProviderProxy( new ServiceReference1.LocalAuthorsSyncContractClient()); } } }

The additions specify the remote provider of the synchronization services.

That's it! To test the application, set the SyncServices project as the startup project and press F5. You will now be able to synchronize your databases using a WCF service.

Easy as 1-2-3
You have seen the architecture of Sync Services and the various controls that make Sync Services possible. With Visual Studio 2008, using Sync Services is now much easier as most of the code is generated for you. While this article focuses on using Visual Studio 2008 to generate the code, you might want to examine the code generated in more detail so that you have a better understanding of how the various Sync Services objects work.

Wei-Meng Lee is a Microsoft MVP and founder of Developer Learning Solutions, a technology company specializing in hands-on training on the latest Microsoft technologies. He is an established developer and trainer specializing in .NET and wireless technologies. Wei-Meng speaks regularly at international conferences and has authored and coauthored numerous books on .NET, XML, and wireless technologies. He writes extensively on topics ranging from .NET to Mac OS X. He is also the author of the .NET Compact Framework Pocket Guide, ASP.NET 2.0: A Developer's Notebook (both from O'Reilly Media, Inc.), and Programming Sudoku (Apress). Here is Wei-Meng's blog.
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.
Thanks for your registration, follow us on our social networks to keep up-to-date