.aspx page loads slowly every once in awhile

liunx

Guest
Hey, guys, I'm hoping one of you can help me out here. I basically wrote my own version of master pages in 2.0 for asp.net 1.1. It works awesomely - usually right on par with html, but every so often it will take about 8 extra seconds to load. Now I know that if you modify web.config or any dlls, your application restarts and needs to be recompiled, but that isn't the case here. Here's what I know:

-this infrequent wait usually happens about once a day and after it occurs, it's fast again

-seems like it doesn't happen if you're accessing the page a lot (and I cleared my browser cache each time, so I know this isn't an image loading issue).

I'm going to disable session state and viewstate for the page since I don't need them and they could be causing the problem, but I'm wondering if you guys have any other ideas. Do you think it could be something with the HttpApplication instances that handle requests running out? I don't know much about how that works, but I know there's a pool generated of HttpApplication instances that can each handle multiple requests, and I know that the more requests a server is getting, the more HttpApplication instances it will generate. At least I think that's how it works. Do they get cleared if it isn't accessed for awhile?

I also looked in the temporary internet files folder and noticed xml files for some of my controls that had <filedep> tags that I'm assuming mean "file dependency." Usually something that is cached with a file dependency is only removed from the cache if that file changes, but I'm wondering if .net might have a timer or something for its internal cache.I think it may be very likely that your last thought, the cached resources, could be what is causing it. Caching is not usually meant to be permanent and so it usually times out and re-establishes itself.

You may have ruled this out, but another possiblity is that the applicaiton state timed out, your application has terminated, and resources reclaimed. Later you access your application and the pause is the time it takes for the application to restart.thanks for the reply - I did some research and it turns out that most shared hosting servers don't set idletimeout to infinite in machine.config (the default when I Download ed the framework), so this causes the application to recycle itself if it doesn't get a request in a specified amount of time (usually 20 minutes). I found this:

<!-- m --><a class="postlink" href="http://authors.aspalliance.com/paulwilson/articles/?id=12">http://authors.aspalliance.com/paulwils ... les/?id=12</a><!-- m -->

What it does is set a timer in the global.asax file that will fire a method every 15 minutes (you can change this value though) which will automatically request your page and prevent the recycle process from ever happening if you aren't getting a lot of requests. It goes through each folder and even loads the usercontrols into a page object in memory to ensure that your app stays fast.

Just finished testing it too - works like a charm. Anyone doing .net development for smaller scale stuff (i.e. sites that don't get a lot of hits) should definitely get this. All you need to do is add the following class to your project and make global.asax inherit from it (it auto-detects your host name, so you don't have to set anything):


Imports System
Imports System.IO
Imports System.Net
Imports System.Web
Imports System.Web.UI

Namespace Wilson.WebCompile
Public Class GlobalBase
Inherits System.Web.HttpApplication

Private Shared _needsCompile As Boolean = True
Private Shared _applicationPath As String = ""
Private Shared _physicalPath As String = ""
Private Shared _applicationURL As String = ""

Private Shared _thread As System.Threading.Thread = Nothing
Private Shared _timer As System.Timers.Timer = Nothing

Public Event Elapsed As EventHandler

' Override and Indicate Time in Minutes to Force the Keep-Alive
Protected Overridable ReadOnly Property KeepAliveMinutes() As Integer
Get
Return 1
End Get
End Property

' Override and Indicate Files to Skip with Semi-Colon Delimiter
Protected Overridable ReadOnly Property SkipFiles as string
Get
Return ""
End Get
End Property

' Override and Indicate Folders to Skip with Semi-Colon Delimiter
Protected Overridable ReadOnly Property SkipFolders() As String
Get
Return ""
End Get
End Property

Public Overrides Sub Init()
If GlobalBase._needsCompile Then
GlobalBase._needsCompile = False

_applicationPath = HttpContext.Current.Request.ApplicationPath
If Not _applicationPath.EndsWith("/") Then _applicationPath += "/"

Dim server As String = HttpContext.Current.Request.ServerVariables("SERVER_NAME")
Dim https As Boolean = (HttpContext.Current.Request.ServerVariables("HTTPS") <> "off")
_applicationURL = IIf(https, "https://", "http://") + server + _applicationPath

_physicalPath = HttpContext.Current.Request.PhysicalApplicationPath
_thread = New System.Threading.Thread(New System.Threading.ThreadStart(AddressOf CompileApp))
_thread.Start()

If Me.KeepAliveMinutes > 0 Then
_timer = New System.Timers.Timer(60000 * Me.KeepAliveMinutes)
AddHandler _timer.Elapsed, AddressOf KeepAlive
_timer.Start()
End If
End If
End Sub

Private Sub KeepAlive(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs)
_timer.Enabled = False
Dim url As String = _applicationURL
Dim response As WebResponse = Nothing
Try
RaiseEvent Elapsed(Me, e)
response = HttpWebRequest.Create(url).GetResponse()
Finally
_timer.Enabled = True
If Not response Is Nothing Then response.Close()
System.Diagnostics.Debug.WriteLine(url, "Timer")
End Try
End Sub

Private Sub CompileApp()
CompileFolder(_physicalPath)
End Sub

Private Sub CompileFolder(ByVal Folder As String)
Dim _file As String
For Each _file In Directory.GetFiles(Folder, "*.as?x")
CompileFile(_file)
Next

Dim _folder As String
For Each _folder In Directory.GetDirectories(Folder)
Dim skipFolder As Boolean = False
Dim item As String
For Each item In Me.SkipFolders.Split(";")
If item <> "" And _folder.ToUpper().EndsWith(item.ToUpper()) Then
skipFolder = True
Exit For
End If
Next
If Not skipFolder Then
CompileFolder(_folder)
End If
Next
End Sub

Private Sub CompileFile(ByVal File As String)
Dim skipFile As Boolean = False
Dim item As String
For Each item In Me.SkipFiles.Split(";")
If item <> "" And File.ToUpper().EndsWith(item.ToUpper()) Then
skipFile = True
Exit For
End If
Next

If Not skipFile Then
Dim path As String = File.Remove(0, _physicalPath.Length)
If File.ToLower().EndsWith(".ascx") Then
Dim virtualPath As String = _applicationPath + path.Replace("\", "/")
Dim controlLoader As Page = New Page
Try
controlLoader.LoadControl(virtualPath)
Finally
System.Diagnostics.Debug.WriteLine(virtualPath, "Control")
End Try
ElseIf Not File.ToLower().EndsWith(".asax") Then
Dim url As String = _applicationURL + path.Replace("\", "/")
Dim response As WebResponse = Nothing
Try
response = HttpWebRequest.Create(url).GetResponse()
Finally
If Not response Is Nothing Then response.Close()
End Try
System.Diagnostics.Debug.WriteLine(url, "Page")
End If
End If
End Sub

End Class
End Namespace
 
Back
Top