Bare with me on this question. We are using NServiceBus 5.2.3.
We are running services using the NServiceBus.Host. We are doing some safety checks in our code to make sure we have appropriate permissions to network folders and that databases exist and can be connected and such. We always do these safety checks in a class that implements “IWantToRunWhenTheBusStartsAndStops.Start”. I think we do this here because the first guy did it there and no one every questioned it. And it works… most of the time.
But the issue we are having and have finally traced down to this is the Handlers will start grabbing from the Queue as soon as the Bus is initialized. Which seems to happen at the same time our safety checks are still running. So a Handler will try and access a database to store something before we have confirmed that database is up and running. Because the safety check fails we end up shutting our service down with an error saying unable to reach database but we’ve already got a message in the error queue because it tried and couldn’t connect to the database.
The basic question is where do we put our safety checks in the startup lifecycle so that our safety check code runs before the handlers start processing messages that may already be waiting?
After looking into this a bit I’ve started doing our safety checks in a class that implements “INeedInitialization.Customize” but it feels like the wrong spot to be doing this. It seems to be working for now but I was wondering if there was somewhere more appropriate? Again we are using NServiceBus 5.2.3.