【发布时间】:2019-03-25 12:52:35
【问题描述】:
我在我的应用程序中使用 SignalR 并响应我使用 Sql Dependency 的数据库更改
SqlDependency.Start(con);
但我收到以下错误:
此版本的 SQL Server 不支持语句“RECEIVE MSG”
据我了解,Azure SQL 数据库不支持 Service Broker。
除了迁移到 Azure VM 之外,还有什么解决方案吗?
具有 SQL 依赖关系的代码示例:
public class NotificationComponent
{
public void RegisterNotification(DateTime currentTime)
{
string conStr = ConfigurationManager.ConnectionStrings["sqlConString"].ConnectionString;
string sqlCommand = @"SELECT [ContactID],[ContactName],[ContactNo] from [dbo].[Contacts] where [AddedOn] > @AddedOn";
using (SqlConnection con = new SqlConnection(conStr))
{
SqlCommand cmd = new SqlCommand(sqlCommand, con);
cmd.Parameters.AddWithValue("@AddedOn", currentTime);
if (con.State != System.Data.ConnectionState.Open)
{
con.Open();
}
cmd.Notification = null;
SqlDependency sqlDep = new SqlDependency(cmd);
sqlDep.OnChange += sqlDep_OnChange;
using (SqlDataReader reader = cmd.ExecuteReader())
{
}
}
}
void sqlDep_OnChange(object sender, SqlNotificationEventArgs e)
{
if (e.Type == SqlNotificationType.Change)
{
SqlDependency sqlDep = sender as SqlDependency;
sqlDep.OnChange -= sqlDep_OnChange;
var notificationHub = GlobalHost.ConnectionManager.GetHubContext<NotificationHub>();
notificationHub.Clients.All.notify("added");
RegisterNotification(DateTime.Now);
}
}
public List<Contact> GetContacts(DateTime afterDate)
{
using (MyPushNotificationEntities dc = new MyPushNotificationEntities())
{
return dc.Contacts.Where(a => a.AddedOn > afterDate).OrderByDescending(a => a.AddedOn).ToList();
}
}
}
【问题讨论】:
-
Azure SQL Database Managed Instances支持Service Broker
-
是什么导致了您想要响应的数据库更改? (即,你能向上移动因果链吗。)
-
您是在谈论数据更改(插入/删除/更新记录)还是架构更改?
-
@AlbertoMorillo 关于插入/删除/更新
-
您的最终解决方案是什么?我不认为数据库驱动的方法适合今天的事件驱动架构。我希望您需要设置事件机制并订阅特定事件。
标签: c# asp.net azure signalr azure-sql-database