I have a class that stores registration information in a database (in the NServiceBus message method Handle).
In most cases, logging can be performed in a separate thread (and transaction) from the main process. But all this needs to be done on the same background thread (which means that they need to be done in order, just do not synchronize with the main process).
However, as soon as it starts external binding to the actual data from NServiceBus, it should be in the same thread.
Here is a sample code:
public class MyExample
{
public void LogSomeStuff(Stuff stuff)
{
using (MoveOutsideTransaction())
{
dataAccess.SaveChanges();
}
}
public void LogSomeOtherStuff(Stuff stuff)
{
using (MoveOutsideTransaction())
{
dataAccess.SaveChanges();
}
}
private IDisposable MoveOutsideTransaction()
{
if (loggingOutsideTransaction)
return new TransactionScope(TransactionScopeOption.Suppress);
return null;
}
}
I am wondering if there is a way to use my transaction conditionally to conditionally move the work to another thread. (But only when it suppresses a transaction.)
source