WCF Service 10s call for sync

305 views Asked by At

I'm using Microsoft Sync Framework and am Synching every 10 seconds over a WCF service. On some system I have run into weird SemaphoreFullExceptions, so I'm trying to Refactor this Synching Background Thread. Basically my question is: what approach to prefer:

private void SynchronizedWorker()
    {
        // Sync loop
        while (!_shouldStop)
        {
            using (var conn = new SqlConnection())
            {
                SyncOrchestrator orchestrator = new SyncOrchestrator();
                SqlSyncProviderProxy destinationProvider = new SqlSyncProviderProxy("TEST");
                SqlSyncProvider localProvider = new SqlSyncProvider("TEST", conn);
                // Set Providers
                orchestrator.LocalProvider = localProvider;
                orchestrator.RemoteProvider = destinationProvider;
                // Direction
                orchestrator.Direction = SyncDirectionOrder.UploadAndDownload;


                // Sync
                orchestrator.Synchronize();
            }


            // wait for next interval or being woken up
            if (_syncWaitEvent.WaitOne(10000))
            {
                // we have been signaled prior to the timeout expiring
                _syncForce = false;
            }
        }
    }

or this

SqlConnection conn;
    SyncOrchestrator orchestrator = new SyncOrchestrator();
    SqlSyncProviderProxy destinationProvider = new SqlSyncProviderProxy("TEST");
    SqlSyncProvider localProvider = new SqlSyncProvider("TEST", conn);

    private void SynchronizedWorker2()
    {
        conn = new SqlConnection();
        orchestrator = new SyncOrchestrator();
        destinationProvider = new SqlSyncProviderProxy("TEST");
        localProvider = new SqlSyncProvider("TEST", conn);
        // Set Providers
        orchestrator.LocalProvider = localProvider;
        orchestrator.RemoteProvider = destinationProvider;
        // Direction
        orchestrator.Direction = SyncDirectionOrder.UploadAndDownload;

        // Sync loop
        while (!_shouldStop)
        {
            // Sync
            orchestrator.Synchronize();

            // wait for next interval or being woken up
            if (_syncWaitEvent.WaitOne(10000))
            {
                // we have been signaled prior to the timeout expiring
                _syncForce = false;
            }
        }
    }

Creating new objects for each sync for me seems to be the cleaner way to go. But of course it costs slightly more cpu time.

What do you think?

1

There are 1 answers

2
Travis On BEST ANSWER

What I've done is use the same Orchestrator for sync, but In two cases I am disposing the objects (and recreating if needed):

  • An error occured
  • The application is being exited

Think this is the way to go...