Transactionscope 超时最大值仅为 10 分钟

Transactionscope timeout max is only 10 mins

我无法在客户端上修改 machine.config

在嵌套事务中是否有任何其他方法可以使它们持续超过 10 分钟?隔离级别或任何其他选项是否有助于解决超时问题。我在 DoWork() 内的交易没问题,我认为是外部交易超时了。

private void DoAllWork()
{
    using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required, TransactionManager.MaximumTimeout))
    {
        DoWork();
        scope.Complete();   
    }
}
private void  DoWork()
{
    Foreach(Some long running loop)
    {   
        using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required, TransactionManager.MaximumTimeout))
        {
            // Entity Framework queries / modification / Save Changes
            scope.Complete();   
        }
    }
}

我遇到了同样的问题。您需要在 machine.config.These 设置中更改它不起作用。但是更改 machine.config 并不好,所以我这样解决了。我正在做的是在 运行 时间获取事务管理器对象并更新它的值,它对我有用。

   public TransactionScope CreateTransactionScope(TimeSpan timeout)
    {
        SetTransactionManagerField("_cachedMaxTimeout", true);
        SetTransactionManagerField("_maximumTimeout", timeout);

          var transactionScopeOptions = new TransactionOptions
            {
                IsolationLevel = IsolationLevel.ReadUncommitted,
                Timeout = timeout
            };

        return new TransactionScope(TransactionScopeOption.RequiresNew, transactionScopeOptions);
    }


      private void SetTransactionManagerField(string fieldName, object value)
    {
        typeof(TransactionManager).GetField(fieldName, BindingFlags.NonPublic | BindingFlags.Static).SetValue(null, value);
    }

我是这样用的

          using (var scope = CreateTransactionScope(TimeSpan.FromMinutes(50)))
            {
        // do something 
        scope.Complete();


        }
        catch (Exception e)
        {
            new Logger().LogException(e);


        }