Is ServiceBase.OnStop called when a .Net 2.0 service fails?

When a .Net service fails, is the ServiceBase.OnStop method called? Will the service be marked as stopped regardless of whether the OnStop method is called?

+1


source to share


3 answers


use try-catch and call OnStop yourself; don't rely on "automatic stop", even if it was "guaranteed", it is not a good idea yet. Services should be as reliable as possible.



+1


source


If by failures you mean there is an unhandled exception in your service then no response will be OnStop will not be thrown. In general, the service will be marked as stopped. But Stephen is right, every root method must have a try-catch block surrounding any code that might throw an exception. Your service should never have an unhandled exception.



0


source


OnStop () is only called when the service actually stops. This does not include, for example, sytem powerdown, as there is a separate override method (called OnShutdown()

) for that.

Everything you put in OnStop()

should most likely be in one separate method that can be called from OnStop()

bothOnShutdown()

As far as catching an odd unexpected exception is concerned, I would suggest wrapping the call ServiceBase.Run()

in try / catch with a log in catch claus. This pretty much guarantees you some kind of logging in case of exceptions.

0


source







All Articles