tags:

views:

175

answers:

6

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.

A: 

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
Yes, I need to do this programatically from within the web application though.
Nathan Ridley
You can do a WMI call to recycle, permissions might not allow it.
jvanderh
A: 

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
Need to do it programatically.
Nathan Ridley
+1  A: 

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
+1  A: 

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

Here we go:

HttpRuntime.UnloadAppDomain();
Nathan Ridley
Please note this technique only works if your app is running under full trust. For a fallback mechanism see my answer.
RichardOD
+2  A: 

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
Ah nice, cheers for the link.
Nathan Ridley