Determine if SqlDependency.Start(connectionstring) has been called in current project?

3k views Asked by At

Our applications use a lot of shared components. Some of them have no need for caching, for example, Windows Services which process unmailed emails. You'd never cache that result set...

Problem is, since our shared data layer has been modified to use SqlCacheDependency, our services which don't start SqlDependency fail on database calls where the data layer requests a SqlCacheDependency object.

Which leads to the question - is there a way for our data classes to test to see if the broker service is listening (ie: has SqlDependency.Start(connectionString) been called)?

The SqlDependency object itself has no Enabled or similar property. Is there any way short of forcing the calling app to tell the data layer that SqlCaching is in use for the data layer to determine the state?

3

There are 3 answers

0
The Evil Greebo On BEST ANSWER

Pretty much the answer is no. We ended up adding a config variable that if false or not present, causes the request to use SqlCacheDependency to be skipped.

1
user2418968 On

SELECT * FROM sys.service_queues WHERE name LIKE 'SqlQueryNotificationService-%'

returns a 'SqlQueryNotificationService-[some guid]'

And if you look deep in the Non-public members of the SqlDependency _serverUserHash while debugging in the IDE, you'll find a collection that contains that same entry. If Microsoft would be so kind as to expose that, then yes.

0
Juan Carlos Puerto On

In my case I have a class library which is used by some web applications. So I have no App.config. I also use the SqlCacheDependency in a static event. So I'm using a static boolean like:

if (!isCachingEnabled)
  isCachingEnabled = SqlDependency.Start(builder.ProviderConnectionString);

So far is working but I'm open to suggestions when using Class Libraries.