I am facing an issue while using SQL Server Notifications. I am developing a web application in ASP.net where one of the page needs to be notified about new entries in one of the tables in a SQL Server database. I am using SQL Server Notification services along with Signal R to achieve this functionality.All seems to work fine with my web page getting updates about new data entries.
The problem arises when the page using notification is refreshed. I find the no of notification for single entry in database go up by the number of refreshes. So if I refresh the page thrice, I get 3 notifications for one entry. I am bit concerned if this would be a burden on server when the no of connected users increases. Also if there is an error while processing the request to update the page with new entry, the user gets multiple error messages with same text. I tried debugging my code and found out that the on change event of SqlDependency
object used is fired multiple time with different IDs every time. Below is brief overview of what my code is doing to use notifications -
I am using SQL Server 2012 and
enable_broker
is set for the database.In
global.asax
, I am usingapplication_start
andapplication_stop
events to start and stopSqlDependency
.In page code, I am setting a new
SqlDependency
object on page load using a command object to monitor the exact data field of the table.When
onchange
ofSqlDependency
object fires, I am notifying the UI using Signal R hub class. Then I remove theOnChange
handler of theSqlDependency
object, call forSqlDependency.Stop(connectionstring)
, setSqlDependency
object to nothing, call forSqlDependency.Start(connectionstring)
and finally set up theSqlDependency
object again using the command object for updated data. This whole set to nothing-stop-start-reset object is to continue monitoring the data for changes.
The above steps work fine but when I refresh the page, those are repeated for the number of refreshes. I tried a lot of things by changing code and debugging but nothing seems to resolve the issue. Now I am wondering if it is some setting somewhere that I missed.
Please help me resolve this issue. Also let me know if any other information such as environment, coding details etc are required.
Regards, Tanmay