1

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!

Iker
  • 41
  • 1
  • Hi Iker, did you land up using this solution and if so, how did it go? – hamish Oct 03 '21 at 04:10
  • Hi Hamsih. Unfortunately I can't test in production, so the only thing I can tell you is that in develop environment it works... Tell me if it works for you :) – Iker Oct 08 '21 at 13:29

0 Answers0