Transactionscope timeout max只有10分钟

时间:2017-01-27 11:29:45

标签: c# entity-framework transactions transactionscope

我无法修改客户端上的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();   
        }
    }
}

1 个答案:

答案 0 :(得分:1)

我有同样的问题。你需要在machine.config中更改它。这些设置不起作用。但是更改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);


        }