I have a .NET window Service which extend a custom base class, this serves as base for many windows services i have written so far, now i want to do add some monitor (hearbeat) on this base class, on the initialized of the base class i start a thread that will notify a database and sleep for 40 sec, this is not perfect but serves its purpouse so far. I have registered an event for the AppDomain.CurrentDomain.DomainUnload, to stop the heartbeat threads, but when i stop the Window Service, my database updates are stopped but the logs that i have place in the event registered at AppDomain.CurrentDomain.DomainUnload are not appearing in the Event Viewer, so i dont have a clear indication if the event is getting fired or not.
I cannot place my heartbeat initialization into the OnStart / OnStop of my base classes, since it will require to recompile all my windows service to make sure all of them all call base.OnStart / base.OnStop.
Any idea why the AppDomain.CurrentDomain.DomainUnload is not getting fired
ps.
I tried also with AppDomain.CurrentDomain.ProcessExit with the same result as above, no logs in the EventViewer .
Is the “CurrentDomain” the default app domain? I would guess so, unless you create custom domains. If so, then note that DomainUnload is not raised for the default domain.
As for
ProcessExit, I’m not sure, but note that a Windows Service’s lifetime is not necessarily bound to the process’ lifetime that hosts it. If a process hosts more than one Windows Service it will only exit, when all services are stopped.