How To Create A Sync Service
In this walk-through you will create a simple Sync service.
Pre-requisites
You must have the following products installed on your computer to complete this walk-through.
- Visual Studio 2008 SP1 or Visual Studio 2010 with C# language components
- If you are using x86 Windows computer, install only x86 version (SyncSDK-v2.1-x86-ENU.msi) of Sync Framework 2.1 SDK (click to download from Microsoft Download Center).
- If you are using x64 Windows computer, install the following:
- x64 version (SyncSDK-v2.1-x64-ENU.msi) of Sync Framework 2.1 SDK (Microsoft Download Center).
- The following two x86 redistribution packages of Sync Framework 2.1 from Sync Framework 2.1 Redistributable Packages (Microsoft Download Center).
-
-
- Synchronization-v2.1-x86-ENU.msi
- DatabaseProviders-v3.1-x86-ENU.msi
-
In this walkthrough, you will be hosting a sync service in the Visual Studio Developer Server, which requires 32-bit components of Sync Framework 2.1 to run the service successfully. If you are actually deploying and hosting the service to IIS or Windows Azure, you just need 64-bit components.
It is not possible have full 32-bit Sync Framework 2.1 SDK installed side-by-side with the 64-bit SDK; therefore you will have to install one version of SDK (64-bit) completely and only selected components of other version (32-bit) of SDK.
Task 1: Generating code for sync service
In this task you will create code files for the service by using the SyncSvcUtil.exe tool.
- Open a Command Prompt window with administrative rights, and switch to C:\Program Files\Microsoft SDKs\Microsoft Sync Framework\4.0\Samples\Config folder.
- If your computer has the x64 version of Windows, use C:\Program Files(x86) folder wherever C:\Program Files is mentioned in this tutorial to access Sync Framework 4.0 October 2010 CTP components.
- To create code files for the service by using the SyncSvcUtil.exe tool, type the following command at the command prompt and then press ENTER.
..\..\bin\SyncSvcUtil /mode:codegen /target:server /scopeconfig:listdbconfig.xml
The tool creates three files: DefaultScopeEntities.cs, DefaultScopeSyncService.svc, DefaultScopeSyncService.svc.cs in the current folder (C:\Program Files\Microsoft SDKs\Microsoft Sync Framework\4.0\samples\Config). You will use these files later in Task 2 when you create a Visual Studio project. Here is the sample output from the previous command:
Reading specified config file... Generating DbSyncScopeDescription for scope DefaultScope... Generating files... SyncSvcUtil completed with no errors.
You can also use the SyncSvcUtilHelper, a UI tool built on top of SyncSvcUtil command-line tool, to generate server side code. See Generating Code for a Server, Generic Client, or Isolated Store Client in the online help for more details.
Task 2: Creating a Visual Studio project
In this task, you will create a Visual Studio project for the sync service.
- Open Microsoft Visual Studio 2008 with Administrator permissions: Click Start, point to All Programs, point to Microsoft Visual Studio 2008, right-click Microsoft Visual Studio 2008, and then click Run as Administrator.
- If you are using Microsoft Visual Studio 2010, use steps similar to preceding steps to launch Visual Studio 2010.
- If the User Account Control dialog appears, click Continue.
- From the File menu, click New and then click Project.
- In the New Project dialog, expand Visual C# in the project types list and select ASP.NET Web Application.
- Enter the Name “ListService”. Click OK to create the project.
- In the Solution Explorer, right-click the ListService project, point to Add, and then click Add Existing Item.
- Navigate to C:\Program Files\Microsoft SDKs\Microsoft Sync Framework\4.0\samples\Config and select the 3 files (DefaultScopeEntities.cs, DefaultScopeSyncService.svc, DefaultScopeSyncService.svc.cs), and then click Add to add these files to the project.
- Examine the Solution Explorer and make sure that the files appear in the project tree.
- In the Solution Explorer, right-click the ListService project and point to Add, and then click Add Reference.
- In the Add Reference dialog box, click the Browse tab, navigate to C:\Program Files\Microsoft SDKs\Microsoft Sync Framework\4.0\server\Microsoft.Synchronization.Services.dll, and then click OK.
Keep the Visual Studio window open. You will use this in the next task to configure the service.
Task 3: Configuring the sync service
In this task, you will configure the service and build it.
In the Solution Explorer, expand the files under DefaultScopeSyncService.svc and then double-click DefaultScopeSyncService.svc.cs to open the file.
Uncomment the following lines in the InitializeService method.
config.ServerConnectionString = "connection string here"; config.SetEnableScope("scope name goes here");
Replace connection string here with the following:
Data Source=(local);Initial Catalog=listDb;Integrated Security=true
In the previous statement, replace the server name with your SQL Server’s instance name, if you are not using the default instance. For example: if your SQL Server instance is called MYSQLINSTANCE, then replace (local) with .\MYSQLINSTANCE.
Replace “scope name goes here” with DefaultScope.
Add the following lines of code to the InitializeService method.
// configure filter parameters used by the service config.AddFilterParameterConfiguration("userid", "User", "@ID", typeof(System.Guid)); config.AddFilterParameterConfiguration("userid", "Item", "@UserID", typeof(System.Guid)); config.AddFilterParameterConfiguration("userid", "List", "@UserID", typeof(System.Guid)); config.AddFilterParameterConfiguration("userid", "TagItemMapping", "@UserID", typeof(System.Guid)); // enable Diagnostic Dashboard feature for the service config.EnableDiagnosticPage = true; // enable verbose errors config.UseVerboseErrors = true;
The table name (ex: User) and the parameter name (ex. @ID) correspond to the definitions in the listdbconfig.xml file that was used for provisioning the database.
From the Build menu, click Build Solution. This will build the solution.
In the Solution Explorer, right-click DefaultScopeSyncService.svc and then click Set As Start Page.
Press F5 to execute the project, it will open a Web browser and navigate to DefaultSyncScopeService.svc. Perform the following operations.
In the address bar of Web browser, add $syncScopes to the end of .svc address, and press ENTER to see all the scopes available from the sync service.
For example: http://localhost:<port>/DefaultScopeSyncService.svc/$syncscopes.
Use the following syntax to see the metadata for defaultscope, which is the name of scope supported by the service:
http://localhost:<port>/DefaultScopeSyncService.svc
Refresh (press F5) the web page if you see a blank page.
Use the following syntax to see diagnostics information for the service:
http:/localhost:<port>/DefaultScopeSyncService.svc/$diag.
If Visual Studio shows a Debugging Not Enabled prompt, click OK in the dialog box. This will modify the Web.config to enable debugging.