我目前使用sql依賴關係通知來檢測表中的更改並處理它們。我有一個在通知被調用,而它仍處於有待完成這將導致重複處理Sql依賴 - 在處理當前請求之前收到的通知
private void ProcessData()
{
try
{
m_Guids = new List<Guid>();
using (SqlCommand command = new SqlCommand("SP_XXX_SELECT", m_sqlConn))
{
command.CommandType = CommandType.StoredProcedure;
command.Notification = null;
SqlDependency dependency = new SqlDependency(command);
dependency.OnChange += new OnChangeEventHandler(OnDependencyChange);
SqlDependency.Start(m_ConnectionString, m_QueueName);
if (m_sqlConn.State == ConnectionState.Closed)
{
m_sqlConn.Open();
}
using (SqlDataReader reader = command.ExecuteReader())
{
if (reader.HasRows)
{
while (reader.Read())
{
m_Guids.Add(reader.GetGuid(0));
}
}
}
Console.WriteLine(m_Guids.Count.ToString());
ProcessGuids();
}
}
}
catch (Exception ex)
{
//SendFailureEmail
}
}
private void OnDependencyChange(object sender, SqlNotificationEventArgs e)
{
SqlDependency dependency = sender as SqlDependency;
dependency.OnChange -= OnDependencyChange;
ProcessData();
}
public void OnStart()
{
SqlDependency.Stop(m_ConnectionString, m_QueueName);
SqlDependency.Start(m_ConnectionString, m_QueueName);
m_sqlConn = new SqlConnection(m_ConnectionString);
}
過程數據的方法得到,而其仍然在處理(processGuids)中間應我再次打電話的第一個請求的中間問題處理完所有數據後訂閱事件? 如果在處理完成之前我不訂閱,那麼在此過程中更改的數據會發生什麼變化,我相信在下一次變更發生之前不會收到通知?做這件事的正確方法是什麼,或者我做錯了什麼。 謝謝