Why is TransactionScope timeout defined in machine.config?

As we know from this answer , the maxTimeout for TransactionScope is defined in the machine.config file.

So what's the reason? Why can't we override it in app.config (web.config) or just in code?

+4


source to share


1 answer


Why is the TransactionScope timeout defined in the machine.config?

  • The machine.config configuration file stores machine level configuration settings. The machine.config settings apply to all web applications on the server. and the TransactionScope timeout value is what everyone wants to keep consistent across all your web applications.
  • The advantage of this is that if you want to change the transaction scope timeout value for the entire IIS hosted application, you can do so by changing the value in one place. However, this does not mean that you cannot override it in the app.config or web.config file. There is a way to override this.

Why can't we override this in app.config (web.config) or just in code?

  • Several days ago I was also looking for possible ways to override and found below two solutions that helped me. Here is a link that contains a complete article on the subject. Link

1. Override in the web.config / app.config file: - in the machine.config file, under the "system.transactions" section group node, in the "machineSettings" section node there is an attribute named "allowExeDefinition" with the default value = "MachineOnly ". Change this value to "MachineToApplication". This allows you to add the following to your web.config application and override the whole machine setting:

<system.transactions>
<machineSettings maxTimeout="00:20:00" />
</system.transactions>

      



  • Although this approach changes the machine width setting, it does not change the maximum waiting time of the entire machine. One will be able to store whatever value is set for maxTimeout, while the other will be able to set whatever value is appropriate for a particular application in app.config. This way, each application can override the maxTimeout parameter for the whole machine and set its own maxTimeout parameter.

    2. Override in code:

  • This approach uses the reflection API to access the private data of Microsoft class members. If you are not familiar with API reflection, you can refer to this link
  • It doesn't require changing machine.config, it doesn't open another application for possible DOS situations, and it bypasses any company policy. Below is the code that does this:

    public static class TransactionManagerHelper
        {
            public static void OverrideMaximumTimeout(TimeSpan timeout)
            {
                //TransactionScope inherits a *maximum* timeout from Machine.config.  There 
                  no way to override it from
                //code unless you use reflection.  Hence this code!
                //TransactionManager._cachedMaxTimeout
                var type = typeof(TransactionManager);
                var cachedMaxTimeout = type.GetField("_cachedMaxTimeout", 
                BindingFlags.NonPublic | BindingFlags.Static);
                cachedMaxTimeout.SetValue(null, true);
    
                //TransactionManager._maximumTimeout
                var maximumTimeout = type.GetField("_maximumTimeout", BindingFlags.NonPublic | BindingFlags.Static);
                maximumTimeout.SetValue(null, timeout);
            }
        }
    
          

  • To use it, call the following before creating the TransactionScope object:

    TransactionManagerHelper.OverrideMaximumTimeout (TimeSpan.FromMinutes (5));

Machine.config path

  • 32-bit system

% Windir% \ Microsoft.NET \ Framework [version] \ Config \ machine.config

  • 64-bit system

% Windir% \ Microsoft.NET \ Framework64 [version] \ Config \ machine.config

0


source







All Articles