locked
N-tier Architecture synchronisation, WCF RRS feed

  • Question

  • I'm going to create simple n-tier application to synchronize client DB(MS SQL Server Compact) with server DB throught WCF-service

     

    On client-side I have proxy(generated by svcutil) for my WCF-service. This proxy is put to ServerSyncProviderProxy as constructor's param.

    When I call method Synchronize I have exception "Exception has been thrown by the target of an invocation"

     

    Help me to solve this problem please

    • Moved by Hengzhe Li Friday, April 22, 2011 8:01 AM (From:SyncFx - Microsoft Sync Framework Database Providers [ReadOnly])
    Monday, October 20, 2008 9:51 AM

All replies

  • There may be several reason for invocation error.  It will help if you provide interface contract methods and config section from your config file.

     

    Thanks.

     

    Monday, October 20, 2008 2:25 PM
  • May be my code helps to see the reason of error

     

    Server part:

     

    [ServiceContract]

    public interface IServiceForSync

    {

    [OperationContract()]

    SyncContext ApplyChanges(SyncGroupMetadata groupMetadata, DataSet dataSet, SyncSession syncSession);

    [OperationContract()]

    SyncContext GetChanges(SyncGroupMetadata groupMetadata, SyncSession syncSession);

    [OperationContract()]

    SyncSchema GetSchema(string[] tableNames, SyncSession syncSession);

    [OperationContract()]

    SyncServerInfo GetServerInfo(SyncSession syncSession);

    }

     

     

    public class ServiceForSync : IServiceForSync

    {

    private DbServerSyncProvider _serverSyncProvider;

    public ServiceForSync()

    {

    //this._serverSyncProvider = new SampleServerSyncProvider();

    CreateServerProvider();

    }

    private void CreateServerProvider()

    {

    // assuem default sql server instance

    string sqlserver = Environment.MachineName;

    SqlConnection conn;

    SqlSyncAdapterBuilder builder;

    conn = new SqlConnection(@"Data Source=SLACK\SQLEXPRESS;Initial Catalog=MyDB;User ID=sa;Password=palm_sa");

    conn.Open();

    _serverSyncProvider = new DbServerSyncProvider();

    _serverSyncProvider.Connection = conn;

    builder = new SqlSyncAdapterBuilder();

    builder.Connection = conn;

    builder.SyncDirection = SyncDirection.Bidirectional;

    builder.TableName = "MyTable";

    builder.CreationTrackingColumn = "CreationDate";

    builder.DeletionTrackingColumn = "DeletionDate";

    builder.UpdateTrackingColumn = "LastEditDate";

    builder.TombstoneTableName = "MyTable_Tombstone";

    _serverSyncProvider.SyncAdapters.Add(builder.ToSyncAdapter());

    SqlCommand selectNewAnchorCmd = new SqlCommand();

    selectNewAnchorCmd.CommandText = "SELECT @" + SyncSession.SyncNewReceivedAnchor + " = GETUTCDATE()";

    SqlParameter newRecAnchor = new SqlParameter();

    newRecAnchor.ParameterName = "@" + SyncSession.SyncNewReceivedAnchor;

    newRecAnchor.DbType = System.Data.DbType.DateTime;

    newRecAnchor.Direction = System.Data.ParameterDirection.Output;

    selectNewAnchorCmd.Parameters.Add(newRecAnchor);

    _serverSyncProvider.SelectNewAnchorCommand = selectNewAnchorCmd;

    }

     

    [System.Diagnostics.DebuggerNonUserCodeAttribute()]

    public virtual SyncContext ApplyChanges(SyncGroupMetadata groupMetadata, DataSet dataSet, SyncSession syncSession)

    {

    return this._serverSyncProvider.ApplyChanges(groupMetadata, dataSet, syncSession);

    }

    [System.Diagnostics.DebuggerNonUserCodeAttribute()]

    public virtual SyncContext GetChanges(SyncGroupMetadata groupMetadata, SyncSession syncSession)

    {

    return this._serverSyncProvider.GetChanges(groupMetadata, syncSession);

    }

    [System.Diagnostics.DebuggerNonUserCodeAttribute()]

    public virtual SyncSchema GetSchema(string[] tableNames, SyncSession syncSession)

    {

    Collection<string> tables = new Collection<string>();

    for (int i = 0; i < tableNames.Length; i++) tables.Add(tableNamesIdea);

    return this._serverSyncProvider.GetSchema(tables, syncSession);

    }

    [System.Diagnostics.DebuggerNonUserCodeAttribute()]

    public virtual SyncServerInfo GetServerInfo(SyncSession syncSession)

    {

    return this._serverSyncProvider.GetServerInfo(syncSession);

    }

    }

     

    client part:

    public class Program

    {

    public static void Main(string[] args)

    {

    SampleStats sampleStats = new SampleStats();

    SampleSyncAgent sampleSyncAgent = new SampleSyncAgent();

    SyncStatistics syncStatistics = sampleSyncAgent.Synchronize();

    sampleStats.DisplayStats(syncStatistics, "subsequent");

    }

    public class SampleSyncAgent : SyncAgent

    {

    public SampleSyncAgent()

    {

    this.LocalProvider = new SampleClientSyncProvider();

    //this.RemoteProvider = new SampleServerSyncProvider();

    ServiceReference.ServiceForSyncClient serviceProxy = new ServiceReference.ServiceForSyncClient("TCP_EndPoint");

    serviceProxy.Open();

    this.RemoteProvider = new ServerSyncProviderProxy(serviceProxy);

    SyncTable customerSyncTable = new SyncTable("MyTable");

    customerSyncTable.CreationOption = TableCreationOption.DropExistingOrCreateNewTable;

    customerSyncTable.SyncDirection = SyncDirection.Bidirectional;

    this.Configuration.SyncTables.Add(customerSyncTable);

    }

    }

    public class SampleClientSyncProvider : SqlCeClientSyncProvider

    {

    public SampleClientSyncProvider()

    {

    this.ConnectionString = @"Data Source=D:\SDF\MyDB.sdf;Max Database Size=2047";

    }

    }

    public class SampleStats

    {

    public void DisplayStats(SyncStatistics syncStatistics, string syncType)

    {

    Console.WriteLine(String.Empty);

    if (syncType == "initial")

    {

    Console.WriteLine("****** Initial Synchronization ******");

    }

    else if (syncType == "subsequent")

    {

    Console.WriteLine("***** Subsequent Synchronization ****");

    }

    Console.WriteLine("Start Time: " + syncStatistics.SyncStartTime);

    Console.WriteLine("Total Changes: " + syncStatistics.TotalChangesDownloaded);

    Console.WriteLine("Complete Time: " + syncStatistics.SyncCompleteTime);

    Console.WriteLine(String.Empty);

    }

    }

    }

    public class SampleServerSyncProvider : DbServerSyncProvider

    {

    public SampleServerSyncProvider()

    {

    SqlConnection serverConn = new SqlConnection(@"Data Source=SLACK\SQLEXPRESS;Initial Catalog=MyDB;User ID=sa;Password=palm_sa");

    this.Connection = serverConn;

    // selectNewAnchorCmd command.

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

    this.SelectNewAnchorCommand.CommandText = "Select @sync_new_received_anchor = GETUTCDATE()";

    this.SelectNewAnchorCommand.CommandType = System.Data.CommandType.Text;

    System.Data.SqlClient.SqlParameter selectnewanchorcommand_sync_new_received_anchorParameter = new System.Data.SqlClient.SqlParameter("@sync_new_received_anchor", System.Data.SqlDbType.DateTime);

    selectnewanchorcommand_sync_new_received_anchorParameter.Direction = System.Data.ParameterDirection.Output;

    this.SelectNewAnchorCommand.Parameters.Add(selectnewanchorcommand_sync_new_received_anchorParameter);

    SqlSyncAdapterBuilder customerBuilder = new SqlSyncAdapterBuilder(serverConn);

    customerBuilder.TableName = "MyTable";

    customerBuilder.TombstoneTableName = "MyTable_Tombstone";

    customerBuilder.SyncDirection = SyncDirection.Bidirectional;

    customerBuilder.CreationTrackingColumn = "CreationDate";

    customerBuilder.UpdateTrackingColumn = "LastEditDate";

    customerBuilder.DeletionTrackingColumn = "DeletionDate";

    SyncAdapter customerSyncAdapter = customerBuilder.ToSyncAdapter();

    customerSyncAdapter.TableName = "MyTable";

    this.SyncAdapters.Add(customerSyncAdapter);

    }

    }

    Monday, October 20, 2008 3:37 PM