DeC*_*Caf 5 .net remoting appdomain transactionscope system.transactions
考虑以下小程序,它只是创建一个TransactionScope、打印Transaction.Current、调用另一个 AppDomain 中的方法(需要一段时间执行),然后Transaction.Current在返回时打印。
using System;
using System.Linq;
using System.Runtime.Remoting.Lifetime;
using System.Threading;
using System.Transactions;
namespace TransactionScopeFlowTest
{
class Program
{
static void Main(string[] args)
{
// These times are just to generate the error faster. Normally the initial lease is 5 minutes, meaning the method call
// would have to take 5 minutes to occur, so we speed it up here for demonstration purposes.
LifetimeServices.LeaseManagerPollTime = TimeSpan.FromSeconds(1);
LifetimeServices.LeaseTime = TimeSpan.FromSeconds(1);
LifetimeServices.RenewOnCallTime = TimeSpan.FromSeconds(1);
AppDomain domain = AppDomain.CreateDomain("Temp", null, AppDomain.CurrentDomain.SetupInformation);
using (TransactionScope scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
{
Console.WriteLine($"Transaction Before Call = {Transaction.Current?.TransactionInformation?.LocalIdentifier?.ToString() ?? "<null>"}");
domain.DoCallBack(AppDomainCallback);
Console.WriteLine($"Transaction After Call = {Transaction.Current?.TransactionInformation?.LocalIdentifier?.ToString() ?? "<null>"}");
scope.Complete();
}
AppDomain.Unload(domain);
}
public static void AppDomainCallback()
{
Thread.Sleep(3000);
}
}
}
Run Code Online (Sandbox Code Playgroud)
出乎意料的是,该程序生成以下输出:
Transaction Before Call = 1f980219-2583-4796-8d6d-256a6f100698:1
Transaction After Call = <null>
Run Code Online (Sandbox Code Playgroud)
如果我将TransactionScopeAsyncFlowOption的 ctor 更改为TransactionScopeto TransactionScopeAsyncFlowOption.Suppress,则事务在调用后仍然存在。
我怀疑跨逻辑上下文的事务范围流是由 CallContext 处理的,CallContext 在远程处理调用中传播,并且使用的键继承MarshalByRefObject,并且由于它们没有向任何注册ISponsor,代理将在初始租用时间后断开连接。然后,一旦我们从调用返回,逻辑调用上下文就会与原始上下文合并,这意味着事务不再存在。
我正在寻找一种方法来规避这个问题,并且这是否被视为.NET 中的错误?