Tuesday, December 17, 2013

Set transaction timeout longer then the default in WCF

Trnsactions in WCF

The default transaction timeout is really 1 minute, you can change that on the WCF service, if you want to change that default there are actually 3 places you might need to change. WCF, the application configuration and maybe the machine configuration

Changes to the WCF service

   [ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple,
             UseSynchronizationContext = true,
             InstanceContextMode = InstanceContextMode.PerCall,
             ReleaseServiceInstanceOnTransactionComplete = false,
             TransactionTimeout = "01:00:00")]   

Changes to the web.config

changing the transaction time-out to one hour

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

Changes to the machine config

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

Changing the machine.config transaction timout in code

Can also be done in code, by using reflection. I got the code from the blog here.

public void SetTrnsactionTimeout(TimeSpan timeOut)
{
   //create a object of the type specified by the fully qualified name
   Type oSystemType = typeof(global::System.Transactions.TransactionManager);

   System.Reflection.FieldInfo oCachedMaxTimeout = oSystemType.GetField("_cachedMaxTimeout", 
         System.Reflection.BindingFlags.NonPublic | 
         System.Reflection.BindingFlags.Static);

   System.Reflection.FieldInfo oMaximumTimeout = oSystemType.GetField("_maximumTimeout", 
      System.Reflection.BindingFlags.NonPublic | 
      System.Reflection.BindingFlags.Static);

   oCachedMaxTimeout.SetValue(null, true);
   oMaximumTimeout.SetValue(null, timeOut);
}

No comments: