Occasionally an ASP.NET application needs to be recycled, particularly during testing, usually due to poor coding. An application can be recycled in IIS, but if you don’t have access to IIS, you can programmatically recycle your ASP.NET application.
What is a recycle?
An application recycle releases all the resources and memory associated with an application, and restarts the application with a clean slate. This process prevents memory-hog applications from taking up all a server’s resources with memory leaks and poor resource allocation.
Why recycle?
An application, when coded correctly, should never need to be manually recycled; however, we don’t live in a perfect world, and sometimes poorly coded applications need to be manually recycled until a developer can fix the problem. In my experience, the two most common reasons for needing a recycle is if the SQL connection pool is filled (due to a connection leak) or if I accidentally wrote an infinite loop.
Method #1
The best and most straightforward method for application recycling is by using HttpRuntime.UnloadAppDomain(), which terminates the application and leaves it dead — the application will start up again once it received a request. This method is especially useful for applications that are rarely used — an application can be terminated (so that it won’t take up memory) until it’s needed in the future. The only downside to this method is that it requires high security permissions (PermissionState.Unrestricted) — if you don’t have full control of your environment, then you’ll need to consider method #2.
Method #2
This method is more indirect, but will induce an application recycle. By ‘touching’ the Web.config file, IIS forces a recycle. Any changes to the Web.config file, even to the ‘date last written’, will cause the recycle process. This method is a bit more hack-ish, but it’s the best alternative to method #1.
Below is a function that recycles an application. It will return true if a recycle was successful, and false if an error occurred while attempting a recycle.
Public Function RecycleApplication() As Boolean
Dim Success As Boolean = True
'Method #1
' It requires high security permissions, so it may not
' work in your environment
Try
HttpRuntime.UnloadAppDomain()
Catch ex As Exception
Success = False
End Try
If Not Success Then
'Method #2
' By 'touching' the Web.config file, the application
' is forced to recycle
Try
Dim WebConfigPath As String = HttpContext.Current.Request.PhysicalApplicationPath + "\\Web.config"
IO.File.SetLastWriteTimeUtc(WebConfigPath, DateTime.UtcNow)
Catch ex As Exception
Success = False
End Try
End If
Return Success
End Function
One last point
Please remember that manually recycling an application isn’t something that you should ever routinely have to do — it’s a quick-fix that can keep applications afloat until you can actually fix whatever problem is occurring.
January 30th, 2008 at 4:52 pm
Does this work for .net 2.0 applications or .net 2.0 on a combiniation 1.1 and 2.0 server in IIS 6.0
Our experience has been that touching the web.config forces a recycle in 1.1 but not in 2.0
[Reply]
DotNetYuppie reply on January 31, 2008:
[Reply]