Kristina88
Kristina88

Reputation: 57

Synchronizing SQL table via WCF service

I have one table on my MS SQL db. (2008 R2) table name: MESSAGES fields: message_id, message, is_synced

On my mobile smart phone i need to develop application that will sync my table records and update table when application synced the each record. I need to support MIN numbers of calls to WCF so i must not generate call for each record.

If there will be n records in table, i dont to call WCF n times, i want to call one time, to get all records, sync and return using WCF all the synced results. Is that the right way to go? Can you suggest better implementation?

Upvotes: 0

Views: 540

Answers (1)

Louie Bacaj
Louie Bacaj

Reputation: 1417

You can do Duplex communication to send the data to all smart phones out from the service when things changed.

http://www.codeproject.com/Articles/491844/A-Beginners-Guide-to-Duplex-WCF

http://msdn.microsoft.com/en-us/library/ms731064.aspx

However to answer your current question given your current implementation you could poll the service for a List of all messages at start or on a timer

On your Server you can have something like this in a simple collection:

[ServiceContract(Namespace = "Contracts.IDatabaseResponder")]
public interface IDatabaseResponder
{
    //You could use an object rather than a string but then you have to mark the object
    [OperationContract]
    List<String> GetMessages();
    [OperationContract]
    void SyncMessagesBack(List<String> messages);

}



[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall, ConcurrencyMode = ConcurrencyMode.Multiple)]
public class DatabaseResponder : IDatabaseResponder
{
    List<String> _DatabaseMessageList;

    public List<String> GetMessages()
    {
        //Code here to go to SQL and grab all of the needed Messages
        //..


        return _DatabaseMessageList;
    }

    public void SyncMessagesBack(List<String> messages)
    {
        //Code here to go to SQL and update messages you want to update
        //..


    }

}

Then On the client side something like this would work:

    //Can use plain old list or ObservableCollection
    private IList<String> _DatabaseMessagesItems = new ObservableCollection<String>();
    private DatabaseResponderClient _Proxy;
    DispatcherTimer dispatcherTimer;
    List<String> LocalListOfMessages;

    public Constructor()
    {

          _Proxy = new DatabaseResponderClient();
          _Proxy.InnerChannel.Faulted += new EventHandler(InnerChannel_Faulted);


       try
       {
                _DatabaseMessagesItems = _Proxy.GetMessages();
       }
       catch (Exception ex)
       {
                MessageBox.Show(ex.Message);

                throw;
       }

       dispatcherTimer = new DispatcherTimer();
       dispatcherTimer.Tick += new EventHandler(dispatcherTimerTick);
       dispatcherTimer.Interval = new TimeSpan(0, 0, 60);
       dispatcherTimer.Start();

       dispatcherTimerTick();
    }


    private void dispatcherTimerTick(object sender, EventArgs e)
    {

        try
        {
             //Push back to the service any new or changed list of messages you need to push
             _Proxy.SyncMessagesBack(LocalListOfMessages);
        }
        catch (Exception ex)
        {
            //Handel error
        }
    }

   //Code to keep track of new messages add them etc
   //...

Upvotes: 1

Related Questions