'SqlException Timeout occurring instantaneously

During startup of an AspNetCore application, we are seeing this exception get thrown:

An error occurred while starting the application.
Win32Exception: The wait operation timed out
Unknown location

SqlException: Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding.
System.Data.SqlClient.SqlCommand+<>c.<ExecuteDbDataReaderAsync>b__122_0(Task<SqlDataReader> result)

Win32Exception: The wait operation timed out

Show raw exception details
SqlException: Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding.
System.Data.SqlClient.SqlCommand+<>c.<ExecuteDbDataReaderAsync>b__122_0(Task<SqlDataReader> result)
System.Threading.Tasks.ContinuationResultTaskFromResultTask<TAntecedentResult, TResult>.InnerInvoke()
System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, object state)
System.Threading.Tasks.Task.ExecuteWithThreadLocal(ref Task currentTaskSlot)
Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.ExecuteAsync(IRelationalConnection connection, DbCommandMethod executeMethod, IReadOnlyDictionary<string, object> parameterValues, CancellationToken cancellationToken)
Microsoft.EntityFrameworkCore.Query.Internal.AsyncQueryingEnumerable<T>+AsyncEnumerator.BufferlessMoveNext(DbContext _, bool buffer, CancellationToken cancellationToken)
Microsoft.EntityFrameworkCore.Storage.ExecutionStrategy.ExecuteImplementationAsync<TState, TResult>(Func<DbContext, TState, CancellationToken, Task<TResult>> operation, Func<DbContext, TState, CancellationToken, Task<ExecutionResult<TResult>>> verifySucceeded, TState state, CancellationToken cancellationToken)
Microsoft.EntityFrameworkCore.Storage.ExecutionStrategy.ExecuteImplementationAsync<TState, TResult>(Func<DbContext, TState, CancellationToken, Task<TResult>> operation, Func<DbContext, TState, CancellationToken, Task<ExecutionResult<TResult>>> verifySucceeded, TState state, CancellationToken cancellationToken)
Microsoft.EntityFrameworkCore.Query.Internal.AsyncQueryingEnumerable<T>+AsyncEnumerator.MoveNext(CancellationToken cancellationToken)
Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor+AsyncSelectEnumerable<TSource, TResult>+AsyncSelectEnumerator.MoveNext(CancellationToken cancellationToken)
Microsoft.EntityFrameworkCore.Query.Internal.AsyncLinqOperatorProvider+ExceptionInterceptor<T>+EnumeratorExceptionInterceptor.MoveNext(CancellationToken cancellationToken)
System.Linq.AsyncEnumerable.Aggregate_<TSource, TAccumulate, TResult>(IAsyncEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, TAccumulate> accumulator, Func<TAccumulate, TResult> resultSelector, CancellationToken cancellationToken)

What is interesting about this case is that the exception is thrown instantaneously - we make a request to the app, the startup code is initiated and the exception is thrown all within a second. Also, if we start a trace on the DB using SQL profiler, we are not seeing any statement being executed on the server.

If we recycle the app pool, the problem goes away. What could be causing this? My only guess is that some bad state is cached in the EF Core layer which is causing EF to throw the exception without even attempting to make a request to the DB.

Here are some relevant versions on the environment:

.NET Core 4.6.30411.01 X64 v4.0.0.0
Microsoft.AspNetCore.Hosting version 2.1.1-rtm-30846
Microsoft Windows 10.0.14393 

EDIT 1:

The code has been requested, here is the code that throws the exception:

public async Task<List<OperationalMessagingQueue>> GetAllIncludeMessageAndMarketsAsNoTrackingAsync()
{
    return await DbContext.GetSet<OperationalMessagingQueue>().AsNoTracking()
        .Include(q => q.Messages)
        .ThenInclude(m => m.OperationalMessage.EcfClaimNotify.Markets)
        .ToListAsync();
}

When the exception is thrown, the call stack always starts in the startup code, Startup.Configure(), as per this binding in UseStartup():

public static IHostBuilder CreateHostBuilder(string[] args) =>
    Host.CreateDefaultBuilder(args)
        .ConfigureWebHostDefaults(webBuilder =>
        {
            webBuilder.UseStartup<Startup>();
        });


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source