I am getting the following error when creating a new CacheDependency
object:
Failed to start monitoring changes to '\\server\share\dir49'
This error happens after 48 successful instantiations of
CacheDependency using the same fileserver.
I have an ASP.NET application that needs to display filecounts for 300
different directories on my fileserver. I have a loop where I get each
directory and determine the number of files. In order to make this more
efficient, I want to use the cache object to hold the filecount and
make this dependent on the directory which holds the files. When a file
is added or dropped, the cached item is removed, and I can rebuild it
on my next attempt at reading it.
This works well for the first 48 iterations in the loop. On the 49th
iteration I get the error above. Every directory is on the same drive
and they have identical security settings. If I process the directories
in a different order, it still fails on the 49th. It does not fail if
the files are on the local filesystem.
What is causing this? If I try to instantiate the CacheDependancy
objects again (on the next page request), they will succeed (not raise
any errors) but then not work, e.g. not clear the cache when the
directory changes. The first 48 continue to work as expected.
Example code:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Try
Dim strRoot As String = "\\server\share" ' must have 50+
subdirectories to test this error
Dim strPath As String
Dim intCount As Integer = 0
For Each strPath In
System.IO.Directory.GetDirectories(strRoot)
Response.Write(intCount & " :" &
GetCachedFileCount(strPath) & "<br>")
intCount += 1
Next
Catch ex As Exception
Response.Write(ex.Message)
End Try
End Sub
Private Function GetCachedFileCount(ByVal FullPath As String) As
Integer
Dim intFileCount As Integer
Dim dep As System.Web.Caching.CacheDependency
If Cache.Item(FullPath) Is Nothing AndAlso intCacheCount < 20
Then
If System.IO.Directory.Exists(FullPath) Then
intFileCount =
System.IO.Directory.GetFiles(FullPath).Length
Try
Response.Write(FullPath & "<br>")
dep = New
System.Web.Caching.CacheDependency(FullPath)
Cache.Insert(FullPath, intFileCount, dep)
Catch ex As Exception
Response.Write("Failed: " & ex.Message & "<br>")
End Try
Else
intFileCount = 0
End If
Else
intFileCount = Cache.Get(FullPath)
End If
Return intFileCount
End Function