0

我的 MS SQL 数据库上有一张表。(2008 R2) 表名:MESSAGES 字段:message_id、message、is_synced

在我的移动智能手机上,我需要开发应用程序来同步我的表记录并在应用程序同步每条记录时更新表。我需要支持对 WCF 的 MIN 调用次数,因此我不能为每条记录生成调用。

如果表中有n条记录,我不调用WCF n次,我想调用一次,以获取所有记录,同步并使用WCF返回所有同步结果。这是正确的方法吗?你能建议更好的实施吗?

4

1 回答 1

1

当事情发生变化时,您可以进行双工通信以将数据从服务中发送到所有智能手机。

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

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

但是,要根据您当前的实现来回答您当前的问题,您可以在启动时或在计时器上轮询服务以获取所有消息的列表

在您的服务器上,您可以在一个简单的集合中拥有这样的东西:

[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
        //..


    }

}

然后在客户端这样的事情会起作用:

    //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
   //...
于 2013-08-23T20:36:34.103 回答