I have a Windows Service that is supposed to wait for database changes.
The database table it looks at looks like this :
Id [Guid]
Name [text]
MatchId [Guid] => this is a foreign key
MatchDateTime [DateTime]
I would like my service to detect whenever someone 'cleared' the 'MatchId' field (assigned NULL to it),
and then for all the rows that have 'NULL' in their 'MatchId' field - perform some algorithm that finds a match for them and assigns the 'MatchId' field.
Problem is - if I use the 'SqlDependency' class, and wait for it to trigger on the 'MatchId' field - I will only get notified when new rows are affected, but any existing rows with 'MatchId = NULL' - I will not know about.
This means that if the Windows Service loads, and there are already 20 rows in the table with 'MatchId = NULL', and for the next 5 hours no one will 'clear' the 'MatchId' row - this means that I will not know about the existing 20 rows and will not handle them.
One solution might be that before I setup the 'SqlDependency' - I should perform a quick 'Select' to see if there are any existing rows with 'MatchId = NULL', and if so - handle them, and only then setup the dependency.
The problem is - in the short interval between finishing to handle those rows, and setting up the SqlDependency - there might occur an event where someone changed a row to 'MatchId = NULL', and then again - I wouldn't catch it in the SqlDependency.
Any idea how this can be solved ?
(If the worst comes to the worst, I will just not use SqlDependency, and I will just have to poll the database every XXX minutes ...)