Search code examples
.netexceptiongac

Is there a way to programatically "reboot" a .net assembly?


Hypothetical situation: let's say I have a 3rd party .net assembly being used in the ASP.Net web system that I'm working on that just kinda crashes every now and then. When it does so, all calls to it throw exceptions until the system is rebooted. This is, obviously, a little sub-optimal.

Is there a way to reboot just that assembly from the exception handler?

(Addendum: of course, the correct answer here is to get the vendor to fix their bug. However, the vendor in question is somewhat... "reluctant" to answer our emails. So, this reboot the assembly idea came about this morning as a MacGyver-eqsue piece of duct-tape. And then we realized none of us knew how to do it.)


Solution

  • Calling HttpRuntime.UnloadAppDomain() will force the application to terminate itself and restart when the next web request arrives. This results in everything being reloaded from scratch and should eliminate the corrupted assembly data.

    Granted, this will kill any active sessions, but if the application is essentially crashed anyway this is probably acceptable.