Configuring the WCF service library

The first thing you can do is to delete both the Servicei.cs and iservicei.cs files from your CRMLiveServiceLibrary project. These two files are default files generated together with the project, and will not be used.

After that, open the CRMLiveDataCache.SyncContract.cs file. Look for the iCRMLiveDataCacheSyncContract interface definition. You will need to add the XMLSerializerFormat() attribute here (highlighted as follows). This is important because the .NET Compact Framework does not support the WCF default DataContractSerializer when serializing data for transfer over the network. The XMLSerializer, on the other hand is supported by the .NET Compact Framework.

[ServiceContractAttribute() , XmlSerializerFormat ()]

public interface ICRMLiveDataCacheSyncContract {


SyncContext ApplyChanges(SyncGroupMetadata groupMetadata,

DataSet dataSet, SyncSession syncSession);

Serializers are classes that convert an object and its current state into I a stream or buffer of bytes (for storage or transport purposes). I

Next, scroll to the top of this same file. There is a bunch of commented app.config settings that you need to copy and paste into the app.config file in this project. You need to perform the following changes to your app.config file:

1. Copy the commented <service> block into the app.config file, overwriting the existing <service> entry under the <system. serviceModel><services> tag. Don't forget to uncomment this block after that!

2. Copy the commented <behavior> block into the app.config file, overwriting the existing <behavior> entry under the <behaviors><service Behaviors> tag. Uncomment this block of code as well.

3. Change the binding="wsHttpBinding" property to binding="basicHttpB inding" .This is important because the .NET Compact Framework does not support wsHttpBinding.

4. Change the base address from http://localhost:8 08 0 to reflect your actual server name or server IP (for example, http://i92.i68.2.4:8080).

Your app.config file should now look something like this:

<?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> </configSections> <connectionStrings> <add name="CRMLiveServiceLibrary.Properties.Settings. ServerCRMLIVEConnectionString"

connectionString="Data Source=EDZEHOO-PC\SQLEXPRESS;Initial Catalog=CRMLIVE;Persist Security

Download from Wow! eBook <>

Info=True;User ID=sa;Password=admin123" providerName="System.Data.SqlClient" /> </connectionStrings> <system.web>

<compilation debug="true" /> </system.web>

<system.serviceModel> <services> <service name="CRMLiveServiceLibrary. CRMLiveDataCacheSyncService"




<add baseAddress ="http://192.16 8.2.4:80 80

/CRMLiveDataCacheSyncService/"/> </baseAddresses> </host>

<endpoint address ="" binding="basicHttpBinding"



<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /> </service> </services> <behaviors>


<behavior name="CRMLiveServiceLibrary. CRMLiveDataCacheSyncServiceBehavior"> <serviceMetadata httpGetEnabled="True" /> <serviceDebug includeExceptionDetailInFaults="False" /> </behavior>

</serviceBehaviors> </behaviors> </system.serviceModel> </configuration>

Setting filters for the Sync

When you sync down Accounts data from the server to the mobile device, you obviously don't need to sync down every single record from the server. Only the accounts owned by the mobile user need to be synced down. This same rule extends to all the accounts-related tables —Accounts, AccountTasks, AccountHistories, and AccountFiles.

As each account record has an owneriD column, we can easily filter the records using this column. Open the CRMLiveDataCache.Designer.cs file in Visual Studio. This file contains all the DataAdapter classes (one for each table to be precise), which determines how your data is extracted for the sync job.

Let's take a look at how we can implement this filter, starting with the Accounts table. Navigate to the InitializeCommands() method of the AccountsSyncAdapter in the Visual Studio designer, shown as follows :

Forrnl.cs CRMLJVEDataSet.Designer.cs Refe

; App.confir CRMLiveDatatache,Desigrer.cs

C RM Li veServi c eLi b ra ry Acco u ntsSyn cAd a pter


" C RM LiveServi c eLi b ra ry Acco u ntFi I esSy n cAd a pter ""t^ C RM LiveServi c eL i b ra ry Acco untHistoriesSync Ad a pter

C RM LiveServi ceLi b lary Ac c c u ntsSy n Ad a pter

" i C RM LiveServi c eLi b ra ry Acco u ritTa sksSy n c Ad a pter "t$ C RM LiveServi ceLibrary.CRM LiveData C a ch eServerSyn cP rovi d er ' J C RM LiveServi c eLi b ra ry, P rod u ctsSy n c Ad a pter

Microsoft. Synclaronizat

Look for the SelectlncrementallnsertsCommand object. The default SQL for this command object retrieves newly created records from the Accounts table. You will need to make the following changes to the SQL (highlighted as follows). Take note that you can use the @owneriD parameter in your SQL. The actual value will be passed in from the client front-end later.

this.SelectlncrementallnsertsCommand = new System.Data.SqlClient.SqlCommand();

this.SelectlncrementallnsertsCommand.CommandText = @"SELECT [OwneriD], [AccountGUID], [AccountType], [DateCreated], [FirstName], [LastName], [Status], [Reception], [Source], [ResPhoneNo], [MobPhoneNo], [EmailAddress], [Street], [City], [State], [Zipcode], [Country], [Website], [InterestedProds], [LastEditDate], [CreationDate] FROM dbo.Accounts WHERE ([CreationDate] > @sync_last_received_anchor AND [CreationDate] <= @sync_new_received_anchor AND [email protected])";

Now let's move on to a child table—the AccountFiles table. You need to filter these records by the OwneriD field as well, but the AccountFiles table does not contain this field. So how do we get around this?

As the owneriD is located in the Accounts table, you can do an inner join with the Accounts table to create this filter. The highlighted code below illustrates how this can be done:

this.SelectIncrementalInsertsCommand = new System.Data.SqlClient.SqlCommand();

this.SelectIncrementalInsertsCommand.CommandText = "SELECT a.[AccountGUID], a.[AttachmentID], a.[AttachmentName], [AttachmentSize], a.[Attachment], a.[Keywords], a.[LastEditDate], a.[CreationDate] FROM dbo.AccountFiles a, dbo.Accounts b WHERE (a.AccountGUID = b.AccountGUID AND a.[CreationDate] > @sync_last_received_anchor AND

a.[CreationDate] <= @sync_new_received_anchor AND

[email protected])";

You will need to repeat this for the AccountTasks and AccountHistories tables. As for the Products table, you will not need to change the query. This is because every mobile user will need to see the same list of products. No filter is required for this table.

«aS-^ Take note that if you change any settings through the Configure Data ( ) Synchronization Wizard, you will lose all changes you've made to the

CRMLiveDataCache.Designer.cs file.

Was this article helpful?

0 0

Post a comment