ASP.NET static variables remain when I stop my application!
I have some static classes in my application. When开发者_如何学编程 I run the application, stop debugging and restart I notice that these variables stay in memory! So when the application terminates they are left behind and are reattached to the next instance of the application. This produces really crazy results as you'd expect.
Can you programatically tell a static variable to destroy itself when the application terminates?
Static variables remain as long as the AppDomain that contains them remains active. If you're seeing them persist between debugging sessions, that means the app does not stop running when you stop debugging - it's the same instance both times. Try restarting the web server.
I would add that if the fact that static variables tend to be persistent is causing crazy results, you're probably not using them right, and possibly shouldn't be using static variables at all.
Stopping the debugger doesn't stop IIS or Cassini.
If you're using Cassini, you need to explicitly stop it from the command tray in order to get it to recycle.
Otherwise, you can try modifying web.config to force a recycle.
I filed a bug about this once on Connect; Microsoft said it was "by design."
精彩评论