I have a virtual path provider. Problem is its caching my files. Whenever I manually edit one of the aspx files it references the VPP doesn't pull in the new file, it continues to reuse the old file until I restart the site.
I've even over-rode the GetCacheDependency() in my VirtualPathProvider class:
public override CacheDependency GetCacheDependency(string virtualPath, IEnumerable virtualPathDependencies, DateTime utcStart)
{
return null;
}
Ideas?
Returning a null is essentially telling ASP.NET that you do not have any dependency - hence ASP.NET will not reload the item.
What you need is to return a valid dependency e.g.
public override CacheDependency GetCacheDependency(string virtualPath, IEnumerable virtualPathDependencies, DateTime utcStart)
{
return new CacheDependency(getPhysicalFileName(virtualPath));
}
A more correct approach is to make sure that you only handle your own cache dependencies (this is a schematic example) :
public override CacheDependency GetCacheDependency(string virtualPath, IEnumerable virtualPathDependencies, DateTime utcStart)
{
if (isMyVirtualPath(virtualPath))
return new CacheDependency(getPhysicalFileName(virtualPath));
else
return new Previous.GetCacheDependency(virtualPath, virtualPathDependencies, utcStart);
}
The correct way to disable caching is this:
public override CacheDependency GetCacheDependency(string virtualPath, IEnumerable virtualPathDependencies, DateTime utcStart)
{
if (_IsLayoutFile(virtualPath))
{
return null;
}
return Previous.GetCacheDependency(virtualPath, virtualPathDependencies, utcStart);
}
public override String GetFileHash(String virtualPath, IEnumerable virtualPathDependencies)
{
if (_IsLayoutFile(virtualPath))
{
return Guid.NewGuid().ToString();
}
return Previous.GetFileHash(virtualPath, virtualPathDependencies);
}
I don't believe this is what the original poster asked. He wants to disable the caching entirely, not implement it in a better way, although your post is helpful for the latter.
A great many people are using VirtualPathProvider to pull data from a database instead of a file system. I don't see how creating a file system dependency would be a useful way to determine when it's time to refresh the file.
How would you FORCE it to never use caching and always retrieve the latest version of the file? That is the question.
public override CacheDependency GetCacheDependency(string virtualPath, IEnumerable virtualPathDependencies, DateTime utcStart)
{
return IsVirtualPath(virtualPath) ? new CacheDependency(HttpContext.Current.Server.MapPath("~/Resource.xml"))
: Previous.GetCacheDependency(virtualPath, virtualPathDependencies, utcStart);
}
The solution that worked for me as desired was:
- GetCacheDependency: return null;
- GetFileHash: return Guid.NewGuid().ToString();
However, with this solution results in hanging the server (Cassini, IIS 6, IIS 7, IIS 8). The hanging only lasts for a few minutes then the results are delivered.
I've also included a test for virtual path/file with the same results. I messed with browser timeouts.
Can anyone help?