Programmatically Recycling Your ASP.NET Application

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.

view source
print?
01.Public Function RecycleApplication() As Boolean
02.    Dim Success As Boolean = True
03.
04.    'Method #1
05.    '   It requires high security permissions, so it may not
06.    '   work in your environment
07.    Try
08.        HttpRuntime.UnloadAppDomain()
09.    Catch ex As Exception
10.        Success = False
11.    End Try
12.
13.    If Not Success Then
14.        'Method #2
15.        '   By 'touching' the Web.config file, the application
16.        '   is forced to recycle
17.        Try
18.            Dim WebConfigPath As String = HttpContext.Current.Request.PhysicalApplicationPath + "\\Web.config"
19.            IO.File.SetLastWriteTimeUtc(WebConfigPath, DateTime.UtcNow)
20.        Catch ex As Exception
21.            Success = False
22.        End Try
23.    End If
24.
25.    Return Success
26.
27.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.

2 Responses to “Programmatically Recycling Your ASP.NET Application”

  1. linda Says:

    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

加支付宝好友偷能量挖...


评论(0)网络
阅读(98)喜欢(0)Asp.Net/C#/WCF