EF core 2 ASP.NET intermittent network errors

0

I have a EF Core 2 Dbcontext injected in the usual way into all Controllers of a Web API:

services.AddDbContext<KpiContext>(options => 
  options.UseSqlServer(config.ConnectionStrings.KpiDB,
                       providerOptions => providerOptions.EnableRetryOnFailure()));

This is injected into controllers in the standard way as, say, kpidb and then it's passed to a static method which does this:

somestaticmethod(KpiContext kpidb) {
  // create something
  kpidb.Add(something);
  kpidb.SaveChanges();
}

This SaveChanges() intermittently fails with:

System.Data.SqlClient.SqlException (0x80131904): A network-related or
instance-specific error occurred while establishing a connection to SQL
Server. The server was not found or was not accessible. Verify that the
instance name is correct and that SQL Server is configured to allow remote
connections. (provider: SQL Network Interfaces, error: 26 - Error Locating
Server/Instance Specified)

I can't see a pattern to this but it happens quite often, perhaps around 10% of the time. I've tried:

  • AddDbContextPool instead of AddDbContext
  • Using Transient scope for the context
  • simultaneous ping shows no network interruptions at time of failure
  • Using port for SQL instance to prevent enumeration makes no difference
  • Simple connect and SELECT every 2 seconds script from the source to target never fails

Nothing seems to make a difference. I know that DBContext isn't thread safe but there are no async EF calls to fall foul of.

Any ideas appreciated.

c#
asp.net
entity-framework-core
asked on Stack Overflow Aug 27, 2019 by PLK • edited Aug 27, 2019 by PLK

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0