We have some code in a job filter that tries to acquire a distributed lock. When it fails, it needs to cancel the PerformingContext
. The code looks as follows:
try
{
return filterContext.Connection.AcquireDistributedLock(resource, timeout);
}
catch (Exception)
{
filterContext.Canceled = true;
return true;
}
What I (and FxCop) don’t like is the catch-all-exceptions. I would like to have a more specific exception to catch. In our environment, a SqlServerDistributedLockException
is thrown but
- This class is internal.
- Under a different configuration it might be a different exception (non SQL Server specific), I assume.
Would it make sense to expect a specific exception type from AcquireDistributedLock
? Either a (public) Hangfire-custom one, or InvalidOperationException
? Or alternatively, to have a TryAcquireDistributedLock
with a boolean return value.