mts*_*ggs 6 c# sql-server asp.net sqldependency signalr
我们有一个Web应用程序,它包含一个带有UI的3层后端(Controller/Biz/Data).我们的数据层单独负责从数据库实例(SQL)中提取数据,业务层向数据发送消息并创建派生属性,控制器负责将这些更改发送到UI.
我们需要在我们的应用程序中进行实时更新,必须在数据库级别(而不是控制器级别)进行跟踪.
我们选择使用SQL Dependency和SignalR作为我们的解决方案.
我所研究的有关SignalR和SQL依赖关系的所有内容都在数据库级别,其中SQL依赖关系将识别数据层中的所有更改和广播.出于显而易见的原因,此方法将绕过在业务层中创建的派生属性,并为我们提供不同的查找对象.
我能想到的唯一解决方案是使用SQL依赖关系来跟踪更改,将它们转储到某个表/对象中,然后使用轮询从控制器,商业层,数据层和备份中获取.
SqlDependency 在您跟踪的数据库中留下垃圾并且不会自行清理。避免使用它!请改用开源实现 - SqlDependencyEx。配置和使用非常简单:
// See constructor optional parameters to configure it according to your needs
var listener = new SqlDependencyEx(connectionString, "YourDatabase", "YourTable");
// e.Data contains actual changed data in the XML format
listener.TableChanged += (o, e) => Console.WriteLine("Your table was changed!");
// After you call the Start method you will receive table notifications with
// the actual changed data in the XML format
listener.Start();
// ... Your code is here
// Don't forget to stop the listener somewhere!
listener.Stop();
Run Code Online (Sandbox Code Playgroud)
使用上面提到的组件,您甚至可以跟踪实际更改的数据,这些数据可以从事件处理程序的事件参数中获取。希望这可以帮助。
| 归档时间: |
|
| 查看次数: |
651 次 |
| 最近记录: |