0

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?

Kristina88
  • 57
  • 1
  • 7
  • ***SQL*** is just the *Structured Query Language* - a query language used by **many database systems**, but not a database product itself. We really need to know what **concrete database system** (and which version) you're using (please update tags accordingly).... – marc_s Aug 23 '13 at 16:36
  • It's not clear what you are asking here. "Is this the right way to go" - well, it's not clear what you are trying to do or why you are doing it. – Kirk Broadhurst Aug 23 '13 at 21:24

1 Answers1

1

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
   //...
Louie Bacaj
  • 1,417
  • 13
  • 18
  • the table on sql2008 db is dynamic means it gets new records every few seconds. on the asp.net site aspx page i need to print "exactly" the results that was not yet synced. while on mobile phone i need to have to sync as many results and always report to sql2008 table which records were already synced. so timing is every thing in this case - i cant allow situation that record that was already synced on mobile phone will printed as not synced on the web site. I have never used Duplex - is that accurate to use duplex in my case? – Kristina88 Aug 23 '13 at 22:10
  • Yes duplex should work. Dual http bind is what you will need. Take a look at examples on the web with this type of implementation of a WCF service. – Louie Bacaj Aug 23 '13 at 22:34
  • my client side-the smartphone app will be for android. http://stackoverflow.com/questions/17607237/android-ios-client-consumes-wcf-duplex-service . i see there is no support for duplex wcf there – Kristina88 Aug 24 '13 at 10:48
  • http://stackoverflow.com/questions/10561054/wcf-duplex-service-and-android-client-any-posibilities – Kristina88 Aug 24 '13 at 10:49
  • Yes but the code I put up can be used to sync without duplex. Also you weren't clear from the beginning about all the platforms. Make your service duplex then use the code above for the android and the callback duplex communication for your ASP site. – Louie Bacaj Aug 24 '13 at 13:37