machine.config を変更せずにトランザクションに 10 分以上かかるようにするには、次のコードを使用します。
private void SetTransactionManagerField(string fieldName, object value)
{
typeof(TransactionManager).GetField(fieldName, BindingFlags.NonPublic | BindingFlags.Static).SetValue(null, value);
}
public TransactionScope CreateTransactionScope(TimeSpan timeout)
{
// or for netcore / .net5+ use these names instead:
// s_cachedMaxTimeout
// s_maximumTimeout
SetTransactionManagerField("_cachedMaxTimeout", true);
SetTransactionManagerField("_maximumTimeout", timeout);
return new TransactionScope(TransactionScopeOption.RequiresNew, timeout);
}
使用法:
using (var ts = CreateTransactionScope(TimeSpan.FromMinutes(20)))
{
DoLongCode();
ts.Complete();
}
この記事に基づいて記事
のコードはもともとここに貼り付けられました。回答のコードはリファクタリングされ、簡素化されました。