I'm getting an error trying to enqueue a job inside a lock statement.
lock (lockObj)
{
BackgroundJob.Enqueue(() => MyJob());
}
It complains to distributed transactions being disabled. But I don't want my locks influence on job processing. If I enable MSDTC, will it hold the lock until job processing done? It's the opposite of what I need.
It is not convenient to move BackgroundJob.Enqueue()
calls outside of the lock block because it could be inside another method which makes it hard to extract (real cases much more difficult):
lock (lockObj)
{
MainWork();
AnotherMethod();
MoreWork();
}
private void AnotherMethod()
{
BackgroundJob.Enqueue(() => MyJob());
SomeWork();
}
Could anyone please suggest refactoring/solution I need here?
Revealed that the issue was not because of lock but because of using TransactionScope. So the next code did the trick:
private void AnotherMethod()
{
using (var ts = new TransactionScope(TransactionScopeOption.Suppress))
{
BackgroundJob.Enqueue(() => MyJob());
ts.Complete();
}
SomeWork();
}