通过WCF服务同步SQL表

时间:2013-08-23 16:34:32

标签: wcf web-services sql-server-2008 wcf-data-services

我的MS SQL数据库上有一个表。 (2008 R2) 表名:MESSAGES fields:message_id,message,is_synced

在我的移动智能手机上,我需要开发能够同步我的桌面记录的应用程序 并在应用程序同步每条记录时更新表。 我需要支持对WCF的MIN号码,所以我不能为每条记录生成呼叫。

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

1 个答案:

答案 0 :(得分:1)

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

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

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

但是,根据您当前的实施情况回答当前的问题,您可以轮询服务 a开始时或计时器上所有消息的列表

在您的服务器上,您可以在一个简单的集合中使用这样的内容:

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