Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keep an Application Running even if an unhandled Exception occurs

Tags:

c#

.net

What ?
I am developing a Console Application that needs to keep running 24/7 No Matter What
Is there any way to stop a Multi-Threaded Application from getting blown up by some unhandled exception happening in "some thread somewhere" ?

Why ?
Please refrain from giving lessons like "you should manage all your exceptions", "this should never happen" etc. I have my reasons : We are in test deployment and we need to keep this running, log exceptions, and restart all threads again. If anything unplanned happens and causes an unhandled exception to be thrown, it needs to be detected and some method called to restart all threads(atomicity is impossible due due the tier design)

This being said, I am aware it might no be possible to restart an application from "within" if it has blown because of and UnhandledException (which I already implemented).

So far, I have used Quartz.net's FileScan Job and a Flag File to detect stuff like that and restart the application from outwards. But this sounds hacky to me. I would like to find something cleaner and less quick and dirty.

DownVoting / Sniping Warning : I KNOW this might NOT be possible "as is'".
Please be creative/helpful rather than abruptly critic and think of this more as an "Open question"

like image 651
Mehdi LAMRANI Avatar asked Jan 11 '12 17:01

Mehdi LAMRANI


1 Answers

If it is going to run 24/7 anyway, why not just write it as a Windows service and take advantage of the recovery options built right into windows?

Configuring Recovery Services

This approach has the additional advantage of being able to survive a machine reboot, and it will log failures/restarts in the system event logs.

like image 169
JohnFx Avatar answered Oct 20 '22 21:10

JohnFx