【问题标题】:Subscribe to row changes in a database and push if ther is any changes to client订阅数据库中的行更改,如果客户端有任何更改,则推送
【发布时间】:2013-01-31 10:24:04
【问题描述】:

我首先使用 EF 代码在 asp.net mvc 中构建在线拍卖应用程序,目前我每 5 秒通过 ajax 轮询一次更新最新的投标金额。有没有其他方法可以达到同样的效果

例如,订阅一个数据行,该数据行会针对任何最新的出价金额更新 UI 元素。 示例:

Bid bid = _bidService.GetLatestBid(auctionId)
bid.Subscribe();    
uielement.Update(bid.amount),

我可以使用 SQLDependency 并且可以是 dotnet 响应式扩展吗?任何机构有任何示例代码或解决方案?

【问题讨论】:

    标签: asp.net-mvc system.reactive sqldependency subscribe


    【解决方案1】:

    首先在您的数据库上启用sql dependency

    接下来,从 SqlCommand 创建 SqlDependency 并使用 Observable.FromEventPattern 将事件转换为 observable。

    示例代码:

    public class SqlDependencyObservable : ObservableBase<SqlNotificationEventArgs>
    {
        private readonly SqlCommand _command;
        public SqlDependencyObservable(SqlCommand command)
        {
            _command = command;
        }
    
        protected override IDisposable SubscribeCore(IObserver<SqlNotificationEventArgs> observer)
        {
            SqlDependency dependency = new SqlDependency(_command);
    
            return Observable.FromEventPattern<OnChangeEventHandler, SqlNotificationEventArgs>
                (addHandler: handler => dependency.OnChange += handler, removeHandler: handler => dependency.OnChange -= handler)
                .Select(i => i.EventArgs)
                .Subscribe(observer);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-10-15
      • 2020-11-13
      • 1970-01-01
      • 2021-08-15
      • 2017-02-18
      • 1970-01-01
      • 2015-12-07
      • 1970-01-01
      相关资源
      最近更新 更多