none
Sync Service round 2. RRS feed

  • Question

  • I posted on the subject before, Hilary Cotter answered and provided initial springboard. I hoped for more, perhaps a demo project or at least coherent blocks of code. Instead I had to start wading thru some disjoined description. Predictably I ended up seeing inconsistencies. This is one of them. I created a WCF Service Library project and enclosed the code from the page above in a class. I provided references as required. The problem is I see Cursor and MessageBox objects that don't belong in a Service. They must be a part of a Windows.Forms project. When I tried to p the same code in a Window.Form.Form I could not reference  System.ServiceModel under any circumstances. I need help. Also by the way of appeal, perhaps someone does have a developed project they would share? Thanks.

    This is the code:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Microsoft.Synchronization;
    using Microsoft.Synchronization.Data;

    namespace SqlSyncForm
    {
        class Class1
        {
            private static void Sync ( )
            {
                Cursor.Current = Cursors.WaitCursor;

                // Create WCF Service proxy
                System.ServiceModel.BasicHttpBinding basicBinding =
                    new System.ServiceModel.BasicHttpBinding ( );
                System.ServiceModel.EndpointAddress endPoint =
                    new System.ServiceModel.EndpointAddress (
                     "http://my_server:8081/NorthwindDataCacheSyncService/" );
                NorthwindDataCacheSyncContractClient proxy =
                    new NorthwindDataCacheSyncContractClient ( basicBinding,
                                                             endPoint );
                // Wrap in a ServerSyncProviderProxy
                Microsoft.Synchronization.Data.ServerSyncProviderProxy syncProxy =
                    new Microsoft.Synchronization.Data.ServerSyncProviderProxy (
                        proxy );

                // Create a SyncAgent
                NorthwindDataCacheSyncAgent syncAgent =
                    new NorthwindDataCacheSyncAgent ( );
                syncAgent.RemoteProvider = syncProxy;

                // Set sync to be bidirectional
                syncAgent.Customers.SyncDirection =
                    Microsoft.Synchronization.Data.SyncDirection.Bidirectional;

                // Synchronize
                Microsoft.Synchronization.Data.SyncStatistics syncStats =
                    syncAgent.Synchronize ( );

                // Show synchronization statistics
                MessageBox.Show ( "Changes downloaded: " +
                    syncStats.TotalChangesDownloaded.ToString ( )
                    + "\r\nChanges uploaded: " +
                    syncStats.TotalChangesUploaded.ToString ( ) );

                Cursor.Current = Cursors.Default;
            }

        }
    }

    At this stage it is far from being compileable code.


    AlexB - Win_7 Pro64, SqlSer64 WinSer64
    Tuesday, May 25, 2010 9:54 PM

Answers

  • regarding the proxy: the proxy represents the remote provider. your remote provider will have settings that is specific to what is being synched (what tables, the corresponding queries, etc...). so to answer your question, you need a separate proxy for every unique sync scenario you may have. (e.g., one synching 5 tables, another sync synching 20 tables)

    regarding the Cursor and MessageBox: again, as i have mentioned, it is most likely that the code was ripped off a client application. And in its current form even while its inside the WCF Service library, you still have to exposed a service contract to invoke it.

    to further qualify your requirements, if your two servers are on the same network, what do you need WCF for? are you trying to build the sync as a service that can be invoked by clients applications over WCF? or are you trying to build it as a Windows service that syncs the two DBs?

    Note that Sync Fx doesnt support the "continous sync" similar to SQL Replication. Sync Fx has to be explicitly invoked everytime a sync is needed. (you can use timers if you want it automatically).

    you can find sample sync apps at this site: http://code.msdn.microsoft.com/sync

    Wednesday, May 26, 2010 8:27 PM
    Moderator
  • Hi, just happened to come across this thread. I found the following sample quite useful: http://code.msdn.microsoft.com/Release/ProjectReleases.aspx?ProjectName=sync&ReleaseId=3762 for WCF. Although it says peer-to-peer and can be used for this, you can use it for client server by having all peers connect and synchronize only with the server.

     

    It does need some re-organising to be used in production (IMO anyway). Firstly the service is hosted in a console - hosting in IIS or a Windows Service is probably better. Secondly the (Windows Forms) client directly references the service project as a regular reference and uses channel factory. I prefer either adding a service reference or using svcutil to generate the service proxy code on the client and having the connection configured in the config file. If you want to do that with this project, some code needs to be moved across from the service project to the client project and some of the code files split up. Thirdly I prefer the provisioning code to be moved to a separate application altogether - at least for provisioning the server anyway.

     

    As an aside (and I haven't had chance to read your initial post) it is of course possible to host a WCF service in a Windows Forms application - although you were saying there was no reference to System.ServiceModel so perhaps this was not the case for the sample you were talking about.

     

    Thursday, May 27, 2010 9:23 AM

All replies

  • hi alex,

    can you elaborate further what is it that you want to achieve?

    do you want to enable synchronization between client and server over WCF? or do you want to build a WCF service that initiates a sync?

    the code you posted above is most likely from a client application. however, even if you omit the Cursor and MessageBox, and even if its inside a WCF Service Library, how do you intend to instantiate this class? is it going to be instantiated by another piece of code in your WCF service? contract?

    Wednesday, May 26, 2010 10:49 AM
    Moderator
  • Hi June,

    Thank you for the answer. I think first time I ran into a person who knows the subject and does not speak in generalities. Answering you questions I want to say that they are somewhat above my head. I understand the sujject viscerally. I am very versed in .NET 4.0 and earlier verssions. I handle Sql Serve professionally, I've built class libraries but I have no idea how I thould approach what I want to achieve. I don't have a road map. If I had prior experience with the methods involved I would not have posted here. I would appreciate if you explain to me where I should go.

    My task is to synchronize two Sql Servers on LAN (one machine - one server, another machine - another server). Databases are equal. The data change will be mostly on one side therefore most of the databases will be synchronized unidirectionally most of the time, although one of them will be bidirectionally synchronized.

    I would consider one of the machines as a server and the other as client.

    Here is a specific question. If I go with that code, then the Proxy;

    NorthwindDataCacheSyncContractClient proxy =
                    new NorthwindDataCacheSyncContractClient ( basicBinding,
                                                             endPoint );
    should be defined for every DB or only one of them?

    You see I am in the dark completely.

    So, give me a plan:)

    Thanks.

     


    AlexB - Win_7 Pro64, SqlSer64 WinSer64
    Wednesday, May 26, 2010 5:02 PM
  • Also, June, could you asnwer my OP question directly? Why did they put Cursor and MessageBox into a Service Class Library? WIthout any using statemetns the code is misleading and not really helpful.
    AlexB - Win_7 Pro64, SqlSer64 WinSer64
    Wednesday, May 26, 2010 5:10 PM
  • regarding the proxy: the proxy represents the remote provider. your remote provider will have settings that is specific to what is being synched (what tables, the corresponding queries, etc...). so to answer your question, you need a separate proxy for every unique sync scenario you may have. (e.g., one synching 5 tables, another sync synching 20 tables)

    regarding the Cursor and MessageBox: again, as i have mentioned, it is most likely that the code was ripped off a client application. And in its current form even while its inside the WCF Service library, you still have to exposed a service contract to invoke it.

    to further qualify your requirements, if your two servers are on the same network, what do you need WCF for? are you trying to build the sync as a service that can be invoked by clients applications over WCF? or are you trying to build it as a Windows service that syncs the two DBs?

    Note that Sync Fx doesnt support the "continous sync" similar to SQL Replication. Sync Fx has to be explicitly invoked everytime a sync is needed. (you can use timers if you want it automatically).

    you can find sample sync apps at this site: http://code.msdn.microsoft.com/sync

    Wednesday, May 26, 2010 8:27 PM
    Moderator
  • Hi, just happened to come across this thread. I found the following sample quite useful: http://code.msdn.microsoft.com/Release/ProjectReleases.aspx?ProjectName=sync&ReleaseId=3762 for WCF. Although it says peer-to-peer and can be used for this, you can use it for client server by having all peers connect and synchronize only with the server.

     

    It does need some re-organising to be used in production (IMO anyway). Firstly the service is hosted in a console - hosting in IIS or a Windows Service is probably better. Secondly the (Windows Forms) client directly references the service project as a regular reference and uses channel factory. I prefer either adding a service reference or using svcutil to generate the service proxy code on the client and having the connection configured in the config file. If you want to do that with this project, some code needs to be moved across from the service project to the client project and some of the code files split up. Thirdly I prefer the provisioning code to be moved to a separate application altogether - at least for provisioning the server anyway.

     

    As an aside (and I haven't had chance to read your initial post) it is of course possible to host a WCF service in a Windows Forms application - although you were saying there was no reference to System.ServiceModel so perhaps this was not the case for the sample you were talking about.

     

    Thursday, May 27, 2010 9:23 AM
  • June hi,

    Thanks for that. A wealth of information. The timer probably will be fine. I am more comfortable with .NET coding than Sql Replication,  the later does not really appeal to me. My understanding is beginning to develop like an old photographic plate:)

    regarding the Cursor and MessageBox: again, as i have mentioned, it is most likely that the code was ripped off a client application. And in its current form even while its inside the WCF Service library, you still have to exposed a service contract to invoke it.

    This is what I suspected.

    Thank you very much for the samples. I am marking your post as answer but may still come abck fo more questions, so stand by please:)

    Many thanks.


    AlexB - Win_7 Pro64, SqlSer64 WinSer64
    Thursday, May 27, 2010 1:03 PM
  • Thank you Brendon. It is another piece of good news and very useful reference. I may as weel do peer-topeer.
    AlexB - Win_7 Pro64, SqlSer64 WinSer64
    Thursday, May 27, 2010 1:07 PM
  • June, could ypu please comment on relative merits of various Sync variants, e.g. Sync with metadata storage service vs with Change Units?
    AlexB - Win_7 Pro64, SqlSer64 WinSer64
    Thursday, May 27, 2010 1:17 PM