易失性 IEnlistmentNotification 和 TransactionScope.AsyncFlowEnabled = true

Dan*_*ach 5 .net c# transactionscope task-parallel-library async-await

除了 .NET 4.5.1 之外,TransactionScope 上还有一个新选项,可以使用异步流。这允许编写以下客户端代码

using(var txt = new TransactionScope(..., TransactionScopeAsyncFlowOption.Enabled)
{
   await sender.SendAsync();
}
Run Code Online (Sandbox Code Playgroud)

到目前为止,一切都很好。但是当我需要实现一个易失性的 IEnlistmentNotification 时,我很难做到这一点。让我们想象一下以下场景,假设:我的底层基础设施从下到上完全异步

public class MessageSender : ISendMessages
{
    public async Task SendAsync(TransportMessage message, SendOptions options)
    {
        await sender.SendAsync(message);
    }
}
Run Code Online (Sandbox Code Playgroud)

所以我想要实现的是引入一个像这样的易失性IEnlistmentNotification:

internal class SendResourceManager : IEnlistmentNotification
{
    private readonly Func<Task> onCommit;

    public SendResourceManager(Func<Task> onCommit)
    {
        this.onCommit = onCommit;
    }

    public void Prepare(PreparingEnlistment preparingEnlistment)
    {
        preparingEnlistment.Prepared();
    }

    public void Commit(Enlistment enlistment)
    {
        await this.onCommit();
        enlistment.Done();
    }

    public void Rollback(Enlistment enlistment)
    {
        enlistment.Done();
    }

    public void InDoubt(Enlistment enlistment)
    {
        enlistment.Done();
    }
}
Run Code Online (Sandbox Code Playgroud)

和新的发件人

public class MessageSender : ISendMessages
{
    public async Task SendAsync(TransportMessage message, SendOptions options)
    {
        // Dirty: Let's assume Transaction.Current is never null
        Transaction.Current.EnlistVolatile(new SendResourceManager(async () => { await sender.SendAsync(message) }));
    }
}
Run Code Online (Sandbox Code Playgroud)

注意:当然这段代码不能编译。这需要我将提交方法声明为 async void。这太棒了。

所以我的问题是:如何编写一个可以在内部等待异步操作的登记?

Yuv*_*kov 5

只要EnlistVolatile不是 CPU 密集型耗时操作,您就可以使用Task以下方法创建基于精简的包装器:EnlistVolatileTask.FromResult

public static class TranscationExtensions
{
    public static Task EnlistVolatileAsync(this Transaction transaction, 
                                           IEnlistmentNotification 
                                           enlistmentNotification, 
                                           EnlistmentOptions enlistmentOptions)
    {
        return Task.FromResult(transaction.EnlistVolatile
                                           (enlistmentNotification, 
                                            enlistmentOptions));
    }
}
Run Code Online (Sandbox Code Playgroud)

然后在你的方法中使用它:

public class MessageSender : ISendMessages
{
    public Task SendAsync(TransportMessage message, SendOptions options)
    {
        return Transaction.Current.EnlistVolatileAsync
               (new SendResourceManager(async () => 
                                       { await sender.SendAsync(message) }));
    }
}
Run Code Online (Sandbox Code Playgroud)

可以在您的调用堆栈中等待更高的位置:

MessageSender sender = new MessageSender();
await sender.SendAsync(message, options);
Run Code Online (Sandbox Code Playgroud)