ADO – Random IT Utensils https://blog.adamfurmanek.pl IT, operating systems, maths, and more. Sat, 02 Jan 2021 19:12:16 +0000 en-US hourly 1 https://wordpress.org/?v=6.6.2 Transaction Scope time limit in ADO.NET https://blog.adamfurmanek.pl/2018/03/17/transaction-scope-time-limit-in-ado-net/ https://blog.adamfurmanek.pl/2018/03/17/transaction-scope-time-limit-in-ado-net/#respond Sat, 17 Mar 2018 09:00:56 +0000 https://blog.adamfurmanek.pl/?p=2368 Continue reading Transaction Scope time limit in ADO.NET]]> In ADO.NET there is a hard-coded system-wide limit for transaction set to 10 minutes. You cannot change it just like that in your web.config or App.config. You can change it in machine.config though. First, where are the machine configs?

For 32 Bits: C:\Windows\Microsoft.NET\Framework\v4.0.30319\Config\machie.config
For 64 Bits: C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Config\machine.config

Now, how to change it?

Just add the following (for 12 hours):

< system.transactions>
     < defaultSettings timeout="12:00:00" />
< /system.transactions>

You can also try to change it with reflection:

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

public TransactionScope CreateTransactionScope(TimeSpan timeout)
{
    SetTransactionManagerField("_cachedMaxTimeout", true);
    SetTransactionManagerField("_maximumTimeout", timeout);
    return new TransactionScope(TransactionScopeOption.RequiresNew, timeout);
}

Also, something like this should also work if you use it in console app before running any transaction:

var machineSettings = (System.Transactions.Configuration.MachineSettingsSection)ConfigurationManager.GetSection("system.transactions/machineSettings");
var readOnly = (typeof(ConfigurationElement)).GetField("_bReadOnly", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
readOnly.SetValue(machineSettings, false);
machineSettings.MaxTimeout = TimeSpan.MaxValue;

using (var t = new TransactionScope(TransactionScopeOption.Required, new TimeSpan(12,0,0))) {
}

So many ways to change a value which should be configurable in code in the first place, however, sometimes Microsoft just forces you to google some weird stuff on the Internet. As a homework, try to find and download WinDBG.exe in 5 minutes (without downloading whole Windows SDK or anything similar having 300MB of tools when you need just one binary).

]]>
https://blog.adamfurmanek.pl/2018/03/17/transaction-scope-time-limit-in-ado-net/feed/ 0