Search code examples
c#nservicebustopshelf

Why is not my windows service stopped on fatal error?


As we have moved from NSB5 to NSB6 we also looked into removing NServiceBus.Host and instead use Topshelf. When we did, our service no longer shows that it has stopped when we receive a critical failure.

As an example, when we have trouble to reach the database for any reason I want the service to end and in Services Manager it should indicate not running. Though, it still says running but service is actually stopped. Therefore no recovery is run either.

This was working as we were using NServiceBus.Host.


Solution

  • I was looking in the wrong direction, towards Topshelf. The answer lies in how to configure NServiceBus to take care of critical errors.

       EndpointConfiguration.DefineCriticalErrorAction(OnCriticalError);
    

    and

        private async Task OnCriticalError(ICriticalErrorContext context)
        {
            await context.Stop().ConfigureAwait(false);
        }
    

    This worked for me.