|
|||||||||
|
The Lowly SqlNotificationRequest
SqlDependency is a high level implementation of the SqlNotificationRequest class. With SqlNotificationRequest, you are required to create your own Service Broker Services and Queues in SQL Server as well as your own listener to grab the notification. You may choose to use this lower level class for more granular control over the notification architecture. Another benefit is that you can create your own messages that are returned along with the notification, similar to the SqlNotificationInfo values.
Here are the key parts you need for doing notification with the SqlNotificationRequest class. Create a queue and a then a service that hooks back to the queue in SQL Server using T-SQL. You can do this directly in SQL Server or just execute this code from your .NET application. The latter method gives you even more control and you can additionally remove them if you like. I have named mine myNotifQueue and myNotifService.
Note that the schema is case sensitive. You won't get an error message if you mis-case it, but you won't get your notification either.Starting with what you now know about using SqlDependency, make a few changes to that process.
The next example takes the RobustDependency class and replaces the GetData method with one that uses a SqlNotificationRequest instead. Additionally, because you won't be using the SqlDependency OnChange event, you write your own method inside the TableProxy class to call when the notification is retrieved. Here is the modified GetData method. If nothing happens within the timeout period, a notification is returned with an EventArg indicating that a timeout occurred.
The StartListener method sets up a new thread and then fires up the Listen method on that thread.Listen creates a new T-SQL WaitFor command to query the queue you created for this job. Note that you have set the timeout of the command to 15 seconds more than the timeout of the QueryNotificationRequest. Here's the code to set up the listener.
Listing 1 shows the rest of the class, including the new DataChanged method in the TableProxy class that replaces the event handler.
|
|||||||||
|