It this a good option to use SqlAzureExecutionStrategy but not in user initiated transactions code?

160 views Asked by At

SqlAzureExecutionStrategy can't be used with user initiated transactions. It throws an Exception as soon as you execute the code inside a transaction. The MS solution is to change the code

var executionStrategy = new SqlAzureExecutionStrategy();

executionStrategy.Execute(
    () =>
    {
         myCode();
    });

But this is not an option because I have a lot of legacy code, I can't change so many code and the code inside the transaction scope maybe can't be executed twice.

So my solution is to use SqlAzureExecutionStrategy when there is no transaction and use DefaultExecutionStrategy if the code is inside a transaction (at least I have retries in some parts of the code)

public class MyConfiguration : DbConfiguration
{
   public MyConfiguration()
  {
       SetExecutionStrategy("System.Data.SqlClient",
 () =>
          {
               IDbExecutionStrategy returValue;

               if (Transaction.Current == null)
                   returValue = new SqlAzureExecutionStrategy();
               else
                   returValue = new DefaultExecutionStrategy();

               return returValue;
          });
  }
}

I have made some quick tests and it works... but can I have problems? it is a good idea or a terrible idea?

Thanks!

0

There are 0 answers