3

I'm using VirtualPathProvider to return virtual pages from a SQL Server table. This is working pk and the code in my VirtualPathProvider class file is below.

The problem I'm having is that when I make a change the virtual page data (title or page text) held in the database, this change isn't shown on the page that's output as the original page has been cached.

I've read a few articles about adding GetCacheDependancy to the VirtualPathProvider class and I've tried implementing a couple of examples but the original page is still cached and displaying.

I've also tried adding some code to the page load of the virtual pages (Response.AddCacheItemDependency("Pages")) and editing the global.asax:

Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
        ' Code that runs on application startup
        HttpContext.Current.Cache.Insert("Pages", DateTime.Now, Nothing, _
             System.DateTime.MaxValue, System.TimeSpan.Zero, _
             System.Web.Caching.CacheItemPriority.NotRemovable, _
             Nothing)

to prevent the caching. but nothings working.

So what I'm after is some changes to by VirtualPathProvider class file to prevent these caching problems. Thanks for any help you can offer!

Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.HtmlControls
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.Hosting
Imports System.Web.UI.MobileControls
Imports System.Collections.Generic

Public Class DbVirtualPathProvider
    Inherits VirtualPathProvider
    Public Shared Sub AppInitialize()
        Dim db As New DbVirtualPathProvider()
        HostingEnvironment.RegisterVirtualPathProvider(db)
    End Sub

Public Overrides Function FileExists(ByVal virtualPath As String) As Boolean
    Dim strConn As String = ConfigurationManager.ConnectionStrings("LIQUIDConnectionString").ConnectionString
    Dim cnn As New SqlConnection(strConn)
    cnn.Open()
    Dim cmd As New SqlCommand()
    cmd.Connection = cnn
    cmd.CommandText = "select count(*) from tbl_VirtualFiles where virtualpath='" & virtualPath & "'"
    Dim retval As Object = cmd.ExecuteScalar()
    cnn.Close()
    Dim i As Integer = Convert.ToInt32(retval)
    If i <= 0 Then
        'important as if no virtual file it looks for physical file
        Return Previous.FileExists(virtualPath)
    Else
        Return True
    End If
End Function

Public Overrides Function GetFile(ByVal virtualPath As String) As VirtualFile
    Dim file As New DbVirtualFile(virtualPath)
    If file.WebFormContent Is Nothing Then
        Return Previous.GetFile(virtualPath)
    Else
        Return file
    End If
End Function

End Class
braX
  • 11,506
  • 5
  • 20
  • 33
Steve
  • 35
  • 5

1 Answers1

0

I think you need to override VirtualPathProvider.GetCacheDependency method for your virtual path provider in the same manner as it is done for FileExists and GetFile methods. So it could be done e.g. in this way:

Public Overrides Function GetCacheDependency(virtualPath As String, virtualPathDependencies As IEnumerable, utcStart As DateTime) As CacheDependency
    If IsVirtualPathForDatabase(virtualPath) Then
        Return New CacheDependency(...)
    Else
        Return New Previous.GetCacheDependency(virtualPath, virtualPathDependencies, utcStart)
    End If
End Function

where is fake IsVirtualPathForDatabase method should determine whether the virtual path is related to your db or not. If yes, you could create your own CacheDependency for this (I leave a place for it ...), otherwise you could use Previous.GetCacheDependency for all physical .aspx pages.

Oleks
  • 31,955
  • 11
  • 77
  • 132
  • 3
    The answer needs more details. For instance how to initialize CacheDependency. I've tried but without success. – Tim Murphy Sep 22 '14 at 20:01