4

Basically I want the effect that would occur if I were to edit the web.config file. The application basically completely unloads itself and starts again, thus re-firing Application_Start and also ditching any dynamically created Types created by the now-defunct AppDomain.

EDIT

I need to do this in my C# code inside my web application. I know it can be done; I did it ages ago but have since lost the code and forgotten how I did it.

Nathan Ridley
  • 33,766
  • 35
  • 123
  • 197

6 Answers6

7

For full trust you can use HttpRuntime.UnloadAppDomain(). If you aren't running in full trust you can modify the last write time on the web.config file. Rick Strahl has wrapped these two approaches up in a nice class.

RichardOD
  • 28,883
  • 9
  • 61
  • 81
1

You can "touch" the web.config file (i.e. rewrite it to disk unchanged), or any file in the bin directory to recycle the application. Of course this means the identity under which your application is running needs appropriate permissions.

Joe
  • 122,218
  • 32
  • 205
  • 338
1

Lately I seem to be answering my own questions a lot :P

Here we go:

HttpRuntime.UnloadAppDomain();
Nathan Ridley
  • 33,766
  • 35
  • 123
  • 197
  • Please note this technique only works if your app is running under full trust. For a fallback mechanism see my answer. – RichardOD Jul 04 '09 at 11:34
1

If all the options above fail, you can also create an endless recursive function as a final resort. The resulting stackoverflow exception will force a reload of the application. (don't do this when you have the visual studio debugger attached)

Johan Gorter
  • 1,253
  • 10
  • 13
0

In IIS you can recycle the worker processes. You don't need to restart IIS.

http://www.microsoft.com/technet/prodtechnol/WindowsServer2003/Library/IIS/24e3c22e-79a9-4f07-a407-dbd0e7f35432.mspx?mfr=true

jvanderh
  • 2,925
  • 4
  • 24
  • 28
0

If you have created a separate application pool for your application, you can reset the Application Pool.

In general, it's always a good idea to have separate app pool's for each application.

SolutionYogi
  • 31,807
  • 12
  • 70
  • 78