Tuesday, January 22, 2008

Microsoft SQL Server Compact Edition 3.5 and Microsoft Synchronization Services for ADO.NET

Here is a short research on the “Microsoft Sync Framework”, more specifically on Microsoft SQL Server Compact Edition 3.5 and Microsoft Synchronization Services for ADO.NET v2.0 CTP1. While evaluating “Visual Studio 2008 Professional Edition” I found “Steve Lasker's Web Log” to be an excellent resource. Steve has 2 nice short screencasts at channel9.msdn.com:

1. Enabling the cached client with Microsoft SQL Server Compact Edition and the Visual Studio “Orcas” SyncDesigner

Followed by

2. Moving to N Tier when Caching with SQL Server Compact Edition and the Visual Studio “Orcas” SyncDesigner

To follow these examples, you can use the tools listed above with “Microsoft SQL Server 2005 Express Edition”, and the “Northwind sample database for SQL Server”. Notice however that these demonstrations were recorded using a Visual Studio 2008 Beta release (Orcas) and some classes used in the examples changed their names in the final Visual Studio 2008 that was released in November 2007. On the other hand, the code shown in the recordings is VB.NET, and I prefer using C# instead.

I am going to follow Steve’s tutorial by documenting it in a step by step fashion, but I will use C# instead of VB.NET and show the code according to the new Visual Studio 2008 Professional Edition IDE. So here we go:

1. Launch Microsoft Visual Studio 2008 and create a new Windows Forms Application project. File -> New -> Project… Let’s name it “SyncServices”, select your folder location and press the OK button:



(Note: to enlarge these images just click them, and then click your browser's back button to return).

2. In the Solution Explorer, right-click the SyncServices project name, then Add -> New Item...



3. From the Templates select Local Database Cache, name it “NorthwindCache.sync” and press the Add button:



4. You will arrive to the Configure Data Synchronization window similar to the one shown below. In this example for the Databases Connections, I began by the Server Connection and pressing the New.. button I changed my Data source to Microsoft SQL Server (SqlClient) and selected my Server name. In my case I used Windows Authentication, then connected to the Northwind sample database and tested connectivity before going any further. You will notice that Visual Studio 2008 will help you to create the SQL Server CE data file NORTHWND.sdf for the Client Connection.



5. Press the + Add button to select the tables that we want to cache locally. Below is the Configure Tables for Offline Use window. Select Customers, Employees and Shippers. Your screen should look like the one below. Press the OK button to continue.



6. As you press the OK button once again, you will see a quick progress message box indicating that Visual Studio 2008 is Synchronizing the local cache database (populating the NORTHWND.sdf local database with data from the remote tables).



7. Then you will arrive to the Data Source Configuration Wizard window below. Select all tables as the database objects for our data set: Customers, Employees and Shippers. Following Steve’s presentation, let’s change the DataSet name to NorthwindDataSet and press the Finish button.



8. Within the Solution Explorer, click the NorthwindDataSet.xsd to verify that Customers, Employees and Shippers are part of the data set in the Designer View:



9. Now in the Visual Studio 2008 menu, you can click on View -> Server Explorer and after expanding your remote tables on the server database, drag and drop the Order Details and Orders tables into the Design View. You should see how SQL Server infers the PK <= FK relationship by linking both remote tables in the diagram:

10. Close the NorthwindDataSet.xsd Design View and click the Yes button in the dialog box to save the changes you have made:



11. In the Solution Explorer right-click the Form1.cs and then click the View Designer in the context menu. In the Visual Studio 2008 menu click Data -> Show Data Sources and expand Orders within the NorthwindDataSet:



12. Click the Orders pull-down menu and select the Details view:



13. Click the OrderID pull-down menu and select the Label view:



14. Click the CustomerID pull-down menu and select the ComboBox view:



Do the same for the EmployeeID and ShipVia columns.

15. Drag the Orders table into Form1. Then drag tables Customers, Employees and Shippers into the Combo Boxes for Customer ID, Employee ID and Ship Via respectively:



16. Now you can Press F5 to Start Debugging the program. It should run showing locally cached data from NORTHWND.sdf:



17. End the program run by closing the Form1 window. Before we add the button to synchronize the data between the remote tables and the local cache, we will copy the sample code required to synchronize into the clipboard. So, in the Solution Explorer double-click the NorthwindCache.sync to bring up the Configure Data Synchronization , then click the Show Code Example… link, and then click the Copy Code to the Clipboard button as shown below:



18. Now with the code in the clipboard, close the “Code Example: Initiating Data Synchronization” window and close or cancel the Configure Data Synchronization window (do not press the OK button). Then add a button into Form1 and change its Text to “Synchronize” in the Properties panel:



19. Now with the code in the clipboard, close the “Code Example: Initiating Data Synchronization” window and close or cancel the Configure Data Synchronization window (do not press the OK button). Then add a button into Form1 and change its Text to “Synchronize” in the Properties panel. Double click the button and paste the clipboard code into the button1_Click() object handler function. The code should look as follows:



20. Now as stated by the code highlighted in blue, we need to refill the synchronized data. The easiest way is to copy the block shown inside the red rectangle into the place where the arrow points. After you do it, the final code will very much look like this:



21. Now you can Press F5 again to run the program. This time you can go to the remote database on SQL Server an change a piece of data. For example, go to the Customers table and find “Tom Spezialitäten”, change it to “Thomas Spezialitäten” then back at this SyncServices press the Synchronize button and browse the record to verify that changes are reflected.

This ends the equivalent of Steve's first video and sets us ready to follow the second one.

22. If everything went fine until this point, now you are ready to Go N Tier with WCF by splitting out the server components to the server side, configuring a Server Proxy on the client side and a Service on the Server Side as shown in Steve's presentation:



23. So let's add a new Project by right-clicking the SyncServices Solution, then Add -> New Project...



24. Then choose the WCF Service Library template from the Visual C# WCF Project types, and change the Name to WcfSyncService as shown below, and press the OK button:



25. Now in the Solution Explorer click the NorthwindCache.sync item to open the Configure Data Synchronization window, then click the Advanced button, change the Server project location to WfcSyncService and press the OK button:



26. Now just to simplify Steve's demo, within the Solution Explorer open NorthwindCache.Server.SyncContract.cs and change the name of the NorthwindCacheSyncService class and it constructor both to Service1, in the places shown enclosed by the red rectangles below:



27. So the code will look as follows after this change:



28. Before you build, you must also open Service1.cs and make the Service1 class partial as shown below, so it can glue together with the previous code:



29. WCF requires the configuration information to define the endpoint of the INorthwindCacheSyncContract interface in the App.config file using XML. We can get it by opening the NorthwindCache.Server.SyncContract.cs and copying the piece of code shown enclosed within the red rectangle below into the clipboard:



30. Then open App.config within the WcfSyncService project. As shown below, delete the piece of code enclosed inside the red rectangle and paste from the clipboard the endpoint of the INorthwindCacheSyncContract interface, inserting it where the arrow points:



31. After the edition is completed, the XML portion of the App.config should look as follows:



32. Now, in order to find out the correct Service Reference for our SyncServices client, right-click the WcfSyncService project within the Solution Explorer and set it up as the Startup Project:



33. Press F5 to start the program again. Wait for the WCF Test Client window until the Service1 is added successfully, right-click the http://... , copy the referenced address into the clipboard and close the window:



34. Go back to the Solution Explorer, right-click the Sync Service project and make it back again the default Startup Project. Then right-click References and Add Service Reference...



35. The Add Service Reference window will open. In the Address field paste the contents of the clipboard. Then change the Namespace field to ServiceReference, then click the Go button. After Service1 is found, expand and click below it the INorthwindCacheSyncContract and IService1 interfaces to verify their respective implemented operations in the right panel. Press the OK button when you are done.



36. Now we have to tell the client to use the proxy. So within the SyncServices project, right-click NorthwindCache.sync and select View Code, so the NorthwindCache.cs will open. At the top of this file add the directive:

using Microsoft.Synchronization.Data;
Then within the OnInitialized() method, add the following line:

this.RemoteProvider = new ServerSyncProviderProxy(new ServiceReference.NorthwindCacheSyncContractClient());
At this point the code should look as follows (additions pointed out inside red rectangles):



37. Test the program again, the same way you did in step 21 and verify the results.

That's it!

I hope that you have found this step by step guide useful. I'll be more than glad to read your comments, so please post back...