We have a requirement to provide an API endpoint which reports the health of various external dependencies. One of these is an Azure Service Bus. By health we simply need to know if the service is available and responding to connections.
Our application already starts up a service bus endpoint on startup and uses this to publish messages to its queue. However, it looks like the only way I can test this endpoint's health would be to actually publish a message to the queue and check for errors. I'd rather not do this because having to clean up these message later feels like overkill.
My other idea was to use a dedicated class to create a new endpoint and start it. Then stop it again if there are no errors, as below. And do this each time I need to check the health.
// Build the service bus configuration - connection string etc.
var configuration = _configurationBuilder.Configure(_settings);
IEndpointInstance serviceBusEndpoint = null;
try
{
serviceBusEndpoint = await Endpoint.Start(configuration);
return true;
}
catch
{
return false;
}
finally
{
if (serviceBusEndpoint != null)
{
await serviceBusEndpoint.Stop();
}
}
However, I suspect this may be a less efficient approach. Is there a better/correct way to achieve this aim?