我有一个运行数据库查询的webapp,希望它在表发生变化时进行更新。 我以前曾经做过,但现在可以用,但是由于某种原因现在还不能用。
SQL Server已启用代理,而我正在使用SignalR。我已经关注了许多帮助页面/教程,但无法使它正常工作!
StatRepo.cs
public class StatRepo
{
readonly string _connstring = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
public IEnumerable<Telephony>GetAllQueues()
{
var realtime = new List<Telephony>();
using (var connection = new SqlConnection(_connstring))
{
connection.Open();
using (var command = new SqlCommand(@"SELECT [QueueName], [StatDateTime], [ServiceLevel60], [ServiceLevel60_RAG], [CallsWaiting], [CallsWaiting_RAG], [MaxWaitTime], [MaxWaitTime_RAG], [AvgSpeedAnswer], [AvgSpeedAnswer_RAG], [AgentsLoggedIn], [AgentsLoggedIn_RAG], [AgentsInTalkCalc], [AgentsReady], [AgentsReady_RAG], [CallsOffered], [CallsAnswered] FROM [dbo].[statserver_telephony_LIVE] WHERE [QueueName] = 'Enhance'", connection))
{
command.Notification = null;
var dependency = new SqlDependency(command);
dependency.OnChange += new OnChangeEventHandler(Dependency_OnChange);
if (connection.State == System.Data.ConnectionState.Closed)
{
connection.Open();
}
var reader = command.ExecuteReader();
while (reader.Read())
{
realtime.Add(item: new Telephony
{
QueueName = (string)reader["QueueName"],
StatDateTime = (DateTime)reader["StatDateTime"],
ServiceLevel60 = (decimal)reader["ServiceLevel60"],
ServiceLevel60_RAG = (string)reader["ServiceLevel60_RAG"],
CallsWaiting = (int)reader["CallsWaiting"],
CallsWaiting_RAG = (string)reader["CallsWaiting_RAG"],
MaxWaitTime = (int)reader["MaxWaitTime"],
MaxWaitTime_RAG = (string)reader["MaxWaitTime_RAG"],
AvgSpeedAnswer = (int)reader["AvgSpeedAnswer"],
AvgSpeedAnswer_RAG = (string)reader["AvgSpeedAnswer_RAG"],
AgentsLoggedIn = (int)reader["AgentsLoggedIn"],
AgentsLoggedIn_RAG = (string)reader["AgentsLoggedIn_RAG"],
AgentsReady = (int)reader["AgentsReady"],
AgentsReady_RAG = (string)reader["AgentsReady_RAG"],
AgentsInTalk = (decimal)reader["AgentsInTalkCalc"],
//AgentsInTalk_RAG = (string)reader["AgentsInTalk_RAG"],
CallsOffered = (int)reader["CallsOffered"],
CallsAnswered = (int)reader["CallsAnswered"],
});
}
}
}
return realtime;
}
private void Dependency_OnChange(object sender, SqlNotificationEventArgs e)
{
var dependency = (SqlDependency)sender;
dependency.OnChange -= Dependency_OnChange;
System.Diagnostics.Debug.WriteLine(DateTime.Now + " Type: " + e.Type);
System.Diagnostics.Debug.WriteLine(DateTime.Now + " Info: " + e.Info);
//System.Diagnostics.Debug.WriteLine(DateTime.Now + " Source: " + e.Source);
if (e.Type == SqlNotificationType.Change)
{
System.Diagnostics.Debug.WriteLine("This got hit");
RealTimeHub.SendMessages();
GetAllQueues();
}
else
{
//Do somthing here
//Console.WriteLine(e.Type);
}
}
}
SqlNotificationType始终为“订阅”,它永远不会以“更改”的形式出现,因此它永远不会被点击,因此不会更新前端。
我非常确定SQL查询符合正常工作所需的条件
编辑:这显然是SQL Server问题,因为我尝试了使用不同服务器的项目的早期版本,并且此代码运行良好。我将其修改为指向新服务器,然后再次中断。 我需要检查/更改的服务器上的代理内是否有东西?