Monday, February 27, 2006

Automatically restarting the Application

The Microsoft Money FAQ site is running ASP.NET 2.0, and is developed on my home machine before being manually copied across to the live site.

One curious thing that has started happening to the site, and I believe it is new to ASP.NET 2.0 in the way that it compiles code, is that I sometimes see errors like this:

Exception information:
Exception type: System.IO.FileNotFoundException
Exception message: Could not load file or assembly 'App_Web_muq0ffjm, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.


Over the last week, this error has been logged about 1000 times :-( And they always seem to start in the middle of the night, just when I won't notice....

The most annoying fact for me is that users see just error, even though the error is in the compilation of one minor component which could be safely ignored. I haven't found a solution to this apparent compilation problem, but I have now installed a workaround, so hopefully it should appear a lot less.

The basic premise is this - the Global.asax file has the following code:
Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
' Code that runs on application startup
Application.Item("appErrors") = 0
End Sub

Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
' Code that runs when an unhandled error occurs
Application.Lock()
Application("appErrors") = CInt(Application("appErrors")) + 1
Application.UnLock()

If CInt(Application("appErrors")) > 10 Then
' restart application
rewriteConfig()
End If
End Sub
The idea is that I have a threshold (I use a setting from web.config, but have used '10' in the example above) when I reach a certain number of errors, the restart application code fires.

The code to restart the application rewrites the web.config file, which forces the application to reload. This code doesn't have to be used in the same application as the one it is restarting, which is good for additional security.
Private Sub rewriteConfig()
Try
Dim cfg As System.Configuration.Configuration
cfg = WebConfigurationManager.OpenWebConfiguration("~")

Dim compilation As CompilationSection
compilation = CType(cfg.GetSection("system.web/compilation"), CompilationSection)
If Not (compilation Is Nothing) Then

compilation.Debug = Not compilation.Debug
compilation.Debug = False
config.Save()
End If
Catch ex As Exception
End Try
End Sub
Only time will tell me whether this will work, but hopefully less errors will now be seen.

If you're having similar problems, then see if this will help you. You may need to use impersonation if your application can't write directly to the web.config file.

It would be nice to use Health Monitoring to do this for me, as that seems to me to be a 'cleaner' way of doing things, but at least this is one step on the way to reducing the occurrence of the error.

1 comment:

Anonymous said...

Update: 6th March

Well, despite my best efforts, this didn't actually fix the underlying problem with the exceptions.

It does now automatically recycle the application (which is good), but the Exceptions still occur :-(

Need to continue to investigate and try and see if I can force the application to recompile itself completely.