1

In ASP.NET/IIS, when a deployment happens or the web.config changes (or a few other scenarios), ASP.NET will spin up a new app domain for the site. The new app domain receives requests, while the old app domain is given some time to spin down (finish it's current requests) before being destroyed. I'd like to be able to detect whether my code is running as the "current" app domain or whether it has been replaced. Is there an in-code way to do this?

alex
  • 6,818
  • 9
  • 52
  • 103
ChaseMedallion
  • 20,860
  • 17
  • 88
  • 152

1 Answers1

0

There is no bullet proof way to determine that within the system, so you need to build your own mechanisms. For example, even if you manage to determine that you are the latest app domain, that is still not an indication that you are the app handling requests, for example IIS can and will recycle the entire worker process at times (such as when a global configuration change is done, or when it fails to ping back the process activation service, etc), and in those cases there might actually be two instances of w3wp.exe (called overlapped recycling) at which point your app domain logic would fail. Of course when you need to scale to more machines the problem further becomes a distributed system (and not even counting Web Gardens which would complicate further).

So depending on how sensitive your difference in behavior is, you'll likely be much better of using other solutions, especially if you have external state already, such as a SQL database, or such, then you probably would be better off moving that leader election there, or use better frameworks equipped with that logic.

Carlos Aguilar Mares
  • 13,411
  • 2
  • 39
  • 36