Asp.net mvc SqlDependency无法运行mvc应用程序。击中一次
我正在关注这个博客,以获取一条信号器推送消息,发送给连接的客户端。 我的调试器从未命中onchange事件 my Global.asax.cs:Asp.net mvc SqlDependency无法运行mvc应用程序。击中一次,asp.net-mvc,signalr,sqldependency,Asp.net Mvc,Signalr,Sqldependency,我正在关注这个博客,以获取一条信号器推送消息,发送给连接的客户端。 我的调试器从未命中onchange事件 my Global.asax.cs: string connString = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString; protected void Application_Start() { // basic stuff SqlDependency.St
string connString = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
protected void Application_Start()
{
// basic stuff
SqlDependency.Start(connString);
var repo = new Repositories.MarkerRepository();
repo.GetAllMarkers(); // to register the dependency
}
My MarkerRepository.cs:
readonly string _connString = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
private MarkersHub _mh = new MarkersHub(); // my signalr hub class
public IEnumerable<House> GetAllMarkers()
{
var markers = new List<House>();
using (var connection = new SqlConnection(_connString))
{
connection.Open();
using (var command = new SqlCommand(@"SELECT * FROM [dbo].Houses", connection))
{
command.Notification = null;
var dependency = new SqlDependency(command);
dependency.OnChange += new OnChangeEventHandler(dependency_OnChange);
if (connection.State == ConnectionState.Closed)
connection.Open();
var reader = command.ExecuteReader();
while (reader.Read())
{
markers.Add(item: new House {
ID = (int)reader["ID"],
Name = (string)reader["Name"],
Code = reader["Code"] != DBNull.Value ? (string)reader["Code"] : "",
Latitude = Convert.ToDouble(reader["Latitude"]),
Longitude = Convert.ToDouble(reader["Longitude"])
});
}
}
}
return markers;
}
private void dependency_OnChange(object sender, SqlNotificationEventArgs e)
{
if (e.Type == SqlNotificationType.Change)
{
_mh.SendMarkers();
}
}
仍然不返回任何行。不是在我的db或master上。所以我认为博客文章中有一个错误,重新订阅了on change事件?此示例在onchange事件中取消注册,并调用注册新事件的方法。有人能验证我的假设吗?这些是我事件中
SqlNotificationEventArgs e
的值,并告诉我要依赖的查询无效
SqlNotificationEventArgs.Type
-->SqlNotificationType.Subscribe
SqlNotificationEventArgs.Info
-->SqlNotificationInfo.Invalid
SqlNotificationEventArgs.Source
-->SqlNotificationSource.Statement
该语句不能使用星号()或表名。用于指定列的语法
source不确定,但在这一行
var command=new SqlCommand(@“SELECT*FROM[dbo].house”,connection)
您应该把@
放在那里吗?因为您只是查询数据,而不是创建、更新或删除数据。这可能与您的问题无关,因此我很抱歉无法提供更多帮助。您的dependency\u onchange方法在我看来是正确的,它几乎和我在项目中的做法一模一样。@Dennis 1679它不是多行字符串,因此不需要@。但问题是当启动依赖项更改时。SqlNotificationEventArgs.Type
的类型为SqlNotificationType.Subscribe
,根据msdn文档,这是一个错误。我得挖得更深一些。
select * from sys.dm_qn_subscriptions