在没有TransactionScopeAsyncFlowOption.Enabled的情况下启用异步TransactionScope [英] Enable Async TransactionScope without TransactionScopeAsyncFlowOption.Enabled

查看:120
本文介绍了在没有TransactionScopeAsyncFlowOption.Enabled的情况下启用异步TransactionScope的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是使用事务作用域的异步缓存和数据库更新.我不能使用v.4.5.1中引入的TransactionScopeAsyncFlowOption.Enabled,因为我使用的Apache Ignite.Net缓存不支持它.我试图通过捕获当前的Synchronization Context,然后显式地使用Synchronization Context Send方法来完成事务来找到解决方法,但这不起作用,因为我仍然收到错误Transaction scope must be disposed on same thread it was created

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

关于如何实现Async Update的任何建议. Apache Ignite支持的建议之一是使用类似以下内容的东西:

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(),但这将使异步代码同步,因此不是最佳选择之一

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);        
    }
}

推荐答案

经过对博客和文章的大量搜索之后,我发现了Stephen Toub撰写的以下博客,有助于在完全相同的线程上实现异步方法的延续,从而避免了交易范围问题.现在,我不需要TransactionScopeAsyncFlowOption.Enabled即可在TransactionScope

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 the TransactionScope

https://blogs.msdn.microsoft.com/pfxteam/2012/01/20/await-synchronizationcontext-and-console-apps/

void Main()
{
    // Modified Async Scheduler for Continuations to work on Exactly same thread
    // Required in the case same Thread is required for Task Continuation post await
    Run(async () => await DemoAsync());

    "Main Complete".Dump();
}

static async Task DemoAsync()
{
    // Transcation Scope test (shall dispose 
    using (var ts = new TransactionScope())
    {            
        await Cache + Database Async update
        ts.Complete();
        "Transaction Scope Complete".Dump();
    }   
}

// Run Method to utilize the Single Thread Synchronization context, thus ensuring we can
// Control the threads / Synchronization context post await, cotinuation run of specific set of threads

public static void Run(Func<Task> func)
{
    // Fetch Current Synchronization context
    var prevCtx = SynchronizationContext.Current;

    try
    {
        // Create SingleThreadSynchronizationContext
        var syncCtx = new SingleThreadSynchronizationContext();

        // Set SingleThreadSynchronizationContext
        SynchronizationContext.SetSynchronizationContext(syncCtx);

        // Execute Func<Task> to fetch the task to be executed
        var t = func();

        // On Continuation complete the SingleThreadSynchronizationContext
        t.ContinueWith(
            delegate { syncCtx.Complete(); }, TaskScheduler.Default);

        // Ensure that SingleThreadSynchronizationContext run on a single thread
        // Execute a Task and its continuation on same thread
        syncCtx.RunOnCurrentThread();

        // Fetch Result if any
        t.GetAwaiter().GetResult();
    }
    // Reset the Previous Synchronization Context
    finally { SynchronizationContext.SetSynchronizationContext(prevCtx); }
}

// Overriden Synchronization context, using Blocking Collection Consumer / Producer model
// Ensure that same Synchronization context / Thread / set of threads are maintained
// In this case we main a single thread for continuation post await

private sealed class SingleThreadSynchronizationContext : SynchronizationContext
{
    // BlockingCollection Consumer Producer Model
    private readonly BlockingCollection<KeyValuePair<SendOrPostCallback, object>>
      m_queue = new BlockingCollection<KeyValuePair<SendOrPostCallback, object>>();

    // Override Post, which is called during Async continuation
    // Send is for Synchronous continuation
    public override void Post(SendOrPostCallback d, object state)
    {
        m_queue.Add(
            new KeyValuePair<SendOrPostCallback, object>(d, state));
    }

    // RunOnCurrentThread, does the job if fetching object from BlockingCollection and execute it
    public void RunOnCurrentThread()
    {
        KeyValuePair<SendOrPostCallback, object> workItem;
        while (m_queue.TryTake(out workItem, Timeout.Infinite))
            workItem.Key(workItem.Value);
    }

    // Compete the SynchronizationContext
    public void Complete() { m_queue.CompleteAdding(); }
}

这篇关于在没有TransactionScopeAsyncFlowOption.Enabled的情况下启用异步TransactionScope的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆