Skip to content
Advertisement

SqlDependency onchange event infinite loop

I have a simple query, and the event fires at the correct time. However, once fired, the property, .HasChanges, of the SqlDependency object is always set as true.

The first time OnChange is fired, the SqlNotificationEventArgs Info property is “Inserted”. The second time the event is fired it’s “Already Changed”.

  • I commented all of my code in the OnChange event out to verify that my code wasn’t causing the change.
  • Servicebroker is enabled in the database

Is there a reason the following code causes an infinite loop of onChange events?

Advertisement

Answer

It appears that the both the OnChange handler and the SqlDependency instance are only good for ONE event. After the event is fired and you unsubscribe the handler, you need to register your handler to a NEW SqlDependency object.

Please see the link here for full details: http://msdn.microsoft.com/en-us/library/a52dhwx7(v=vs.80).aspx

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement