【问题标题】:Synchronizing SQL table via WCF service通过 WCF 服务同步 SQL 表
【发布时间】:2013-08-24 04:42:31
【问题描述】:

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

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

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

【问题讨论】:

  • SQL 只是 结构化查询语言 - 许多数据库系统使用的查询语言b>,但不是数据库产品本身。我们真的需要知道您正在使用什么具体的数据库系统(以及哪个版本)(请相应地更新标签)......
  • 不清楚你在这里问什么。 “这是正确的方法吗?” - 嗯,不清楚你要做什么或为什么要这样做。

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


【解决方案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
   //...

【讨论】:

  • sql2008 db 上的表是动态的,意味着它每隔几秒就会获取新记录。在 asp.net 站点 aspx 页面上,我需要“准确”打印尚未同步的结果。在手机上,我需要同步尽可能多的结果,并始终向 sql2008 表报告哪些记录已经同步。所以在这种情况下,时间就是一切 - 我不能允许已经在手机上同步的记录将打印为未在网站上同步的情况。我从未使用过 Duplex - 在我的情况下使用 duplex 是否准确?
  • 是的,双工应该可以工作。双http绑定是你需要的。看看网络上使用这种类型的 WCF 服务实现的示例。
  • 我的客户端 - 智能手机应用程序将适用于 android。 stackoverflow.com/questions/17607237/… 。我看到那里不支持双工 wcf
  • 是的,但我提供的代码可用于在没有双工的情况下进行同步。此外,您从一开始就不清楚所有平台。使您的服务双工,然后将上面的代码用于 android 和您的 ASP 站点的回调双工通信。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-05
  • 1970-01-01
  • 1970-01-01
  • 2010-12-12
相关资源
最近更新 更多