Skip to content

Commit

Permalink
Changed unhandled exception behavior to not call Stop so that restart…
Browse files Browse the repository at this point in the history
… behavior is triggered by the SCM
  • Loading branch information
phatboyg committed Nov 8, 2013
1 parent 5ace9e4 commit b433429
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/Topshelf/Runtime/Windows/WindowsServiceHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public class WindowsServiceHost :
readonly HostSettings _settings;
int _deadThread;
bool _disposed;
Exception _unhandledException;

public WindowsServiceHost(HostEnvironment environment, HostSettings settings, ServiceHandle serviceHandle)
{
Expand Down Expand Up @@ -150,6 +151,13 @@ protected override void OnStop()
ExitCode = (int)TopshelfExitCode.StopServiceFailed;
throw;
}

if (_unhandledException != null)
{
ExitCode = (int)TopshelfExitCode.UnhandledServiceException;
_log.Info("[Topshelf] Unhandled exception detected, rethrowing to cause application to restart.");
throw new InvalidOperationException("An unhandled exception was detected", _unhandledException);
}
}

protected override void OnPause()
Expand Down Expand Up @@ -243,8 +251,14 @@ protected override void Dispose(bool disposing)
void CatchUnhandledException(object sender, UnhandledExceptionEventArgs e)
{
_log.Error("The service threw an unhandled exception", (Exception)e.ExceptionObject);

// // IF not terminating, then no reason to stop the service?
// if (!e.IsTerminating)
// return;
// This needs to be a configuration option to avoid breaking compatibility

ExitCode = (int)TopshelfExitCode.UnhandledServiceException;
_unhandledException = e.ExceptionObject as Exception;

Stop();

Expand Down

0 comments on commit b433429

Please sign in to comment.