Following is Async Cache and Database update using Transaction Scope. I cannot use TransactionScopeAsyncFlowOption.Enabled
introduced in the v 4.5.1, since the Apache Ignite.Net Cache I am using doesn't support it. I have tried finding a workaround by capturing the current Synchronization Context
and then explicitly using Synchronization Context Send
method to complete the transaction, but this doesn't work as I still get an error Transaction scope must be disposed on same thread it was created
Any suggestion how to go about achieving the Async Update
. one of the suggestion by Apache Ignite support is to use something like:
Task.WhenAll(cacheUpdate, databaseUpdate).Wait()
, but that would make Async code Sync, therefore not one of the best option
public async Task Update()
{
// Capture Current Synchronization Context
var sc = SynchronizationContext.Current;
TransactionOptions tranOptions = new TransactionOptions();
tranOptions.IsolationLevel = System.Transactions.IsolationLevel.RepeatableRead;
using (var ts = new TransactionScope())
{
// Do Cache Update Operation as Async
Task cacheUpdate = // Update Cache Async
// Do Database Update Operation as Async
Task databaseUpdate = // Update Database Async
await Task.WhenAll(cacheUpdate, databaseUpdate);
sc.Send(new SendOrPostCallback(
o =>
{
ts.Complete();
}), sc);
}
}
After fair amount of search across blogs and article, I have found the following blog by Stephen Toub, helps in achieving the Continuation of Async method on exactly same thread, thus avoiding the Transaction Scope issue. Now I don't need
TransactionScopeAsyncFlowOption.Enabled
to get the Async methods run in theTransactionScope
https://blogs.msdn.microsoft.com/pfxteam/2012/01/20/await-synchronizationcontext-and-console-apps/