title: Getting transient errors when making calls against Azure SQL Database from Azure Function
body: <p>We are using .NET Core 2.1 and Entity Framework Core 2.1.1</p>
  
  <p>I have the following setup in Azure West Europe</p>
  
  <ul>
  <li><p>Azure SQL Database
  -- Premium P2 250 DTU
  -- Public endpoint, no VNET peering
  -- "Allow access to Azure Services" = ON</p></li>
  <li><p>Azure Functions
  -- Consumption Plan
  -- Timeout 10 Minutes</p></li>
  <li><p>Azure Blob storage 
  -- hot tier</p></li>
  </ul>
  
  <p>Multiple blobs are uploaded to Azure Blob storage, Azure Functions (up to 5 concurrently) are fired via Azure Event Grid. Azure Functions check structure of the blobs against metadata stored in Azure SQL DB. Each blob contains up to  500K records and 5 columns of payload data. For each record Azure Functions makes a call against Azure SQL DB, so no caching.</p>
  
  <p>I am getting often, when multiple blobs are processed in parallel (up to 5 asynchronous Azure Functions call at the same time), and when the blob size is larger 200K-500K records, the following transient and connection errors from .NET Core Entity Framework:</p>
  
  <p>1.
  An exception has been raised that is likely due to a transient failure. Consider enabling transient error resiliency by adding 'EnableRetryOnFailure()' to the 'UseSqlServer' call.</p>
  
  <p>2.
  A connection was successfully established with the server, but then an error occurred during the pre-login handshake. (provider: SSL Provider, error: 0 - The wait operation timed out.)</p>
  
  <p>3.
  Connection Timeout Expired.  The timeout period elapsed while attempting to consume the pre-login handshake acknowledgement.  This could be because the pre-login handshake failed or the server was unable to respond back in time.  This failure occurred while attempting to connect to the routing destination. The duration spent while attempting to connect to the original server was - [Pre-Login] initialization=13633; handshake=535; [Login] initialization=1; authentication=0; [Post-Login] complete=156;    The duration spent while attempting to connect to this server was - [Pre-Login] initialization=5679; handshake=2044;</p>
  
  <p>4.
  A connection was successfully established with the server, but then an error occurred during the pre-login handshake. (provider: SSL Provider, error: 0 - The wait operation timed out.)</p>
  
  <ol start="5">
  <li>Server provided routing information, but timeout already expired.</li>
  </ol>
  
  <p>At the same time there are any/no health events reported for the Azure SQL Database during the test, and the metrics look awesome: MAX Workers &lt; 3.5%, Sum Successful Connections &lt; 35, MAX Sessions Percentage &lt; 0.045%, Max Log UI percentage &lt; 0.024%, Sum Failed Connections = 0, MAX DTU &lt; 10%, Max Data IO &lt; 0.055%, MAX CPU &lt; 10%.</p>
  
  <p>Running connection stats on Azure SQL DB (sys.database_connection_stats_ex): No failed, aborted or throttled connections.</p>
  
  <pre><code>select *
  from sys.database_connection_stats_ex
  where start_time &gt;= CAST(FLOOR(CAST(getdate() AS float)) AS DATETIME)
  order by start_time desc
  </code></pre>
  
  <p>Has anyone faced similar issues in combintation with .Net Core Entity Framework and Azure SQL Database. Why I am getting those transient errors, why Azure SQL Database metrics look so good not reflecting at all that there are issues?</p>
  
  <p>Thanks a lot in advance for any help.</p>
  
  <pre><code>using Microsoft.EntityFrameworkCore;
  
  namespace MyProject.Domain.Data
  {
      public sealed class ApplicationDbContextFactory : IApplicationDbContextFactory
      {
          private readonly IConfigurationDbConfiguration _configuration;
          private readonly IDateTimeService _dateTimeService;
  
          public ApplicationDbContextFactory(IConfigurationDbConfiguration configuration, IDateTimeService dateTimeService)
          {
              _configuration = configuration;
              _dateTimeService = dateTimeService;
          }
  
          public ApplicationDbContext Create()
          {
              //Not initialized in ctor due to unit testing static functions.
              var options = new DbContextOptionsBuilder&lt;ApplicationDbContext&gt;()
                  .UseSqlServer(_configuration.ConfigurationDbConnectionString).Options;
  
              return new ApplicationDbContext(options, _dateTimeService);
          }
      }
  }
  </code></pre>