2

The ASP.NET Web App's Application_End method is not called when a deployment is deleted or when the actual production Web Role is restarted.

I need to perform clean up operations (like deleting dynamic Service Bus subscriptions) when the application is unloaded.

Any ideas?

krisdyson
  • 3,217
  • 7
  • 43
  • 86

2 Answers2

0

I don't know why Application_End isn't being called (or doesn't look like it's being called) on your Web Role, but you' should use Application_End for cleaning up your instance. This could mean your code runs at several occasions (each recycle for example) while this isn't necessary.

Cleanup operations should go in the WebRole.cs in the OnStop method. But keep into account, you only have a limited time for your cleanup operations.

Sandrino Di Mattia
  • 24,739
  • 2
  • 60
  • 65
  • 1
    But I have Service Bus subscription components inside my Web app which I must call Dispose. The "WebRole" object operates in a different process so cannot clean up on there. – krisdyson Nov 09 '12 at 08:06
  • And it definitely isn't being called as logging reveals that it isn't. – krisdyson Nov 09 '12 at 08:06
0

I ran into the same problem and I set up a less-than-ideal workaround Inspired by this thread.

In the WebRole OnStop handler, call this to recycle all application pools on the role VM:

var p = Process.Start("cmd.exe", Environment.ExpandEnvironmentVariables(@"/C %windir%\system32\inetsrv\appcmd list apppool /xml | %windir%\system32\inetsrv\appcmd.exe recycle apppool /in"));
p.WaitForExit();

(You'll need to make sure your web role runs with elevated permissions by adding <Runtime executionContext="elevated" /> to the csdef file directly under the WebRole node)

Note: Be sure to test your shutdown code both with role reboots and with scale-down operations; I've found that things like database connections may not work during scale-downs.

Community
  • 1
  • 1
Mike Asdf
  • 2,309
  • 26
  • 34