SqlDependency.Start - Getting KeyNotFoundException

  • Thread starter Thread starter Seen The Bean
  • Start date Start date
S

Seen The Bean

I'm working heavily with query notifications in SQL Server 2005 and I
get the following exception:

System.Collections.Generic.KeyNotFoundException: The given key was
not
present in the dictionary.

This was working and then it stopped. At first, the exception was
intermittent, but now it's permanent. The exception is happening deep
inside of a System.Data.dll in a class called
SqlDependencyProcessDispatcher in a method defined as follows:

private bool Start(string connectionString, out string server, out
DbConnectionPoolIdentity identity, out string user, out string
database, ref string queueService, string appDomainKey,
SqlDependencyPerAppDomainDispatcher dispatcher, out bool
errorOccurred, out bool appDomainStart, bool useDefaults)

It looks like the method is checking a dictionary for particular key;
then, if it's found, it retrieves the item from the dictionary.
Looking at Reflector it appears that this lookup may not be
threadsafe. It has a few code blocks inside structured like this:

if (!
this._sqlDependencyPerAppDomainDispatchers.ContainsKey(appDomainKey))
{
lock (this._sqlDependencyPerAppDomainDispatchers)
{

this._sqlDependencyPerAppDomainDispatchers[appDomainKey] = dispatcher;
}
}

These should probably be using
sqlDependencyPerAppDomainDispatchers.TryGetValue instead or should
move the lock to the outside of the if statement.

No one on the Internet seems to have ever encountered this.

Two partial stack traces appear below. They're partial in that I've
ommitted a
long set of calls from our interal app framework that are orthogonal
to the
problem.

The NT user making these calls is a local administrator on the
database
server. The calls work a lot of the time, but break and seemingly
random
moments.

We use a preconfigured SQL Server Service Broker Queue named
QueryChangeMessages and a service called QueryChangeNotifications.

Please help.

One stack trace looks like this:

[KeyNotFoundException: The given key was not present in the
dictionary.]
SqlDependencyProcessDispatcher.Start(String connectionString,
String queue, String appDomainKey, SqlDependencyPerAppDomainDispatcher
dispatcher) +0
System.Data.SqlClient.SqlDependency.Start(String connectionString,
String queue, Boolean useDefaults) +672
System.Data.SqlClient.SqlDependency.Start(String connectionString,
String queue) +7

Nvidia.PolicyInjection.CallHandlers.SqlDependencyConnection.StartInternal()
+103

[DataException: An error occurred while calling
ConfigurationManagement.Start for connection string named
ConfigurationManagement on SQL Server Service Broker Queue
QueryNotificationQueue.]
....

Another stack trace looks like this:

Server stack trace:
at System.ThrowHelper.ThrowKeyNotFoundException()
at System.Collections.Generic.Dictionary`2.get_Item(TKey key)
at
SqlDependencyProcessDispatcher.SqlConnectionContainer.ProcessNotificationResults(SqlDataReader
reader)
at
SqlDependencyProcessDispatcher.SqlConnectionContainer.SynchronouslyQueryServiceBrokerQueue()
at
SqlDependencyProcessDispatcher.SqlConnectionContainer..ctor(SqlConnectionContainerHashHelper
hashHelper, String appDomainKey, Boolean useDefaults)
at SqlDependencyProcessDispatcher.Start(String connectionString,
String&
server, DbConnectionPoolIdentity& identity, String& user, String&
database,
String& queueService, String appDomainKey,
SqlDependencyPerAppDomainDispatcher dispatcher, Boolean&
errorOccurred,
Boolean& appDomainStart, Boolean useDefaults)
at SqlDependencyProcessDispatcher.Start(String connectionString,
String
queue, String appDomainKey, SqlDependencyPerAppDomainDispatcher
dispatcher)
at
System.Runtime.Remoting.Messaging.StackBuilderSink._PrivateProcessMessage(IntPtr
md, Object[] args, Object server, Int32 methodPtr, Boolean
fExecuteInContext,
Object[]& outArgs)
at
System.Runtime.Remoting.Messaging.StackBuilderSink.PrivateProcessMessage(RuntimeMethodHandle
md, Object[] args, Object server, Int32 methodPtr, Boolean
fExecuteInContext,
Object[]& outArgs)
at
System.Runtime.Remoting.Messaging.StackBuilderSink.SyncProcessMessage(IMessage
msg, Int32 methodPtr, Boolean fExecuteInContext)

Exception rethrown at [0]:
at
System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage
reqMsg, IMessage retMsg)
at
System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData&
msgData, Int32 type)
at SqlDependencyProcessDispatcher.Start(String connectionString,
String
queue, String appDomainKey, SqlDependencyPerAppDomainDispatcher
dispatcher)
at System.Data.SqlClient.SqlDependency.Start(String
connectionString,
String queue, Boolean useDefaults)
at System.Data.SqlClient.SqlDependency.Start(String
connectionString,
String queue)
....
 
Back
Top