Cache_dependency_used_more_that_once Exception when adding to .NET cache

  • Thread starter Thread starter mahesh.prasad
  • Start date Start date
M

mahesh.prasad

Hi,

I'm trying to add an object to the .NET cache using a CacheDependency
and I get the following exception :

System.InvalidOperationException: Cache_dependency_used_more_that_once
at System.Web.Caching.CacheEntry.MonitorDependencyChanges()
at System.Web.Caching.CacheSingle.UpdateCache(CacheKey cacheKey,
CacheEntry newEntry, Boolean replace, CacheItemRemovedReason
removedReason, Object& valueOld)
at System.Web.Caching.CacheMultiple.UpdateCache(CacheKey cacheKey,
CacheEntry newEntry, Boolean replace, CacheItemRemovedReason
removedReason, Object& valueOld)
at System.Web.Caching.CacheInternal.DoInsert(Boolean isPublic,
String key, Object value, CacheDependency dependencies, DateTime
utcAbsoluteExpiration, TimeSpan slidingExpiration, CacheItemPriority
priority, CacheItemRemovedCallback onRemoveCallback, Boolean replace)
at System.Web.Caching.Cache.Insert(String key, Object value,
CacheDependency dependencies, DateTime absoluteExpiration, TimeSpan
slidingExpiration, CacheItemPriority priority,
CacheItemRemovedCallback onRemoveCallback)

I've not seen this error before...and I couldn't find reference to
this error message either on Google or MSDN.
Does anyone know what could cause the
"Cache_dependency_used_more_that_once" exception to happen.

Thanks
Mahesh
 
Decompiled:

internal void MonitorDependencyChanges()
{
CacheDependency dependency1 = this._dependency;
if ((dependency1 != null) && (this.State ==
CacheEntry.EntryState.AddedToCache))
{
if (!dependency1.Use())
{
throw new
InvalidOperationException(SR.GetString("Cache_dependency_used_more_that_once"));
}
dependency1.SetCacheDependencyChanged(this);
}
}

In other words, it is trying to use the dependency but it is alread in use
(probably from another part of your code is my guess).
Peter
 
Hi Peter,

Thanks for the reply! I was doing the following :

_cache.Insert("dep","dependency");

string[] depKey = {"dep"};
CacheDependency cdep = new CacheDependency(null,depKey);

_cache.Insert("1","one",cdep,...);
_cache.Insert("2","two",cdep,..);

I thought we could use the same CacheDependency object for more than
one keys; seems like we cannot (btw it would be interesting to know
why it was designed this way)
The code worked after I changed to this...

_cache.Insert("dep","dependency");
string[] depKey = {"dep"};

CacheDependency cdep1 = new CacheDependency(null,depKey);
_cache.Insert("1","one",cdep1,...);

CacheDependency cdep2 = new CacheDependency(null,depKey);
_cache.Insert("2","two",cdep2,..);

Thanks again for your help.

-Mahesh
 
Back
Top