15

I recently started up an ASP.NET Core Web-Application (WebAPI) using Entity Framework Core for database communications and everything was running really smooth as long as I was running the application on my laptop in my dev environment.

For hosting the web-app and database I'm using an Ubuntu-server (16.04.3 LTS). I installed the latest SQL server (Microsoft SQL Server 2017 (RC2) - 14.0.900.75 (X64)) and I can connect to it from my application just fine as long as the application is running on windows. However, using the same code and connection string on Ubuntu all queries just time out. I get the following output (exception/stacktrace):

fail: Microsoft.AspNetCore.Server.Kestrel[13]
      Connection id "0HL6RV4D2F9I6": An unhandled exception was thrown by the application.
System.Data.SqlClient.SqlException: Timeout expired.  The timeout period elapsed prior to completion of the operation or the server is not responding. ---> System.ComponentModel.Win32Exception: Unknown error 258
   --- End of inner exception stack trace ---
   at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
   at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
   at System.Data.SqlClient.TdsParserStateObject.ReadSniError(TdsParserStateObject stateObj, UInt32 error)
   at System.Data.SqlClient.TdsParserStateObject.ReadSniSyncOverAsync()
   at System.Data.SqlClient.TdsParserStateObject.TryReadNetworkPacket()
   at System.Data.SqlClient.TdsParserStateObject.TryPrepareBuffer()
   at System.Data.SqlClient.TdsParserStateObject.TryReadByteArray(Byte[] buff, Int32 offset, Int32 len, Int32& totalRead)
   at System.Data.SqlClient.TdsParserStateObject.TryReadChar(Char& value)
   at System.Data.SqlClient.TdsParser.TryReadPlpUnicodeCharsChunk(Char[] buff, Int32 offst, Int32 len, TdsParserStateObject stateObj, Int32& charsRead)
   at System.Data.SqlClient.TdsParser.TryReadPlpUnicodeChars(Char[]& buff, Int32 offst, Int32 len, TdsParserStateObject stateObj, Int32& totalCharsRead)
   at System.Data.SqlClient.TdsParser.TryReadSqlStringValue(SqlBuffer value, Byte type, Int32 length, Encoding encoding, Boolean isPlp, TdsParserStateObject stateObj)
   at System.Data.SqlClient.TdsParser.TryReadSqlValue(SqlBuffer value, SqlMetaDataPriv md, Int32 length, TdsParserStateObject stateObj)
   at System.Data.SqlClient.SqlDataReader.TryReadColumnInternal(Int32 i, Boolean readHeaderOnly)
   at System.Data.SqlClient.SqlDataReader.GetValues(Object[] values)
   at Microsoft.EntityFrameworkCore.Storage.Internal.UntypedRelationalValueBufferFactory.Create(DbDataReader dataReader)
   at Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable.Enumerator.BufferlessMoveNext(Boolean buffer)
   at Microsoft.EntityFrameworkCore.Storage.Internal.SqlServerExecutionStrategy.Execute[TState,TResult](Func`2 operation, Func`2 verifySucceeded, TState state)
   at Microsoft.EntityFrameworkCore.Query.QueryMethodProvider.<_ShapedQuery>d__3`1.MoveNext()
   at System.Linq.Enumerable.First[TSource](IEnumerable`1 source)
   at lambda_method(Closure , QueryContext )
   at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.<>c__DisplayClass20_0`1.<CompileQueryCore>b__0(QueryContext qc)
   at System.Linq.Queryable.First[TSource](IQueryable`1 source, Expression`1 predicate)
   at Considition.WebAPI.Controllers.GameController.InitiateGame(String apikey) in /home/fredrik/WebApps/Considition/Considition.WebAPI/Controllers/GameController.cs:line 70
   at lambda_method(Closure , Object , Object[] )
   at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.<InvokeActionMethodAsync>d__27.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.<InvokeNextActionFilterAsync>d__25.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Rethrow(ActionExecutedContext context)
   at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
   at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.<InvokeNextResourceFilter>d__22.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Rethrow(ResourceExecutedContext context)
   at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
   at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.<InvokeAsync>d__20.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.Builder.RouterMiddleware.<Invoke>d__4.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.Cors.Infrastructure.CorsMiddleware.<Invoke>d__7.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.Hosting.Internal.RequestServicesContainerMiddleware.<Invoke>d__3.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.Server.Kestrel.Internal.Http.Frame`1.<RequestProcessingAsync>d__2.MoveNext()
ClientConnectionId:19a604ef-758e-4797-8780-6b95dcbfdd17
Error Number:-2,State:0,Class:11
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[2]
      Request finished in 35515.1854ms 500

The line in my code where the exception occurs is:

var teamGameState = ctx.GeneratedGameStates.First(x => x.TeamId == teamId);

The table only has one entry and looking at the Activity Monitor in SSMS, I can see that the translated SQL is something like (SELECT TOP 1 * FROM ...) and that the query is taking 35 seconds.

I've been reading about other people having a similar issue when trying to connect to the SQL Server from Linux with the same code that was working fine from windows, but I could not find a solution.

Is anyone else experiencing this issue and more importantly did someone solve it?

Shehan Dhaleesha
  • 627
  • 1
  • 10
  • 30
Fredrik Ek
  • 623
  • 1
  • 6
  • 10
  • What is your connection string look like? Code-wise you can take an app running on windows and run it as is on Linux but you would still need to update your connection string. Your error indicates that you are not able to connect to SQL Server in linux – Smit Aug 10 '17 at 00:59
  • I think it connects though as I can see the query and the time it takes in SSMS. Anyway I use the exact same connectionstring on windows and linux: "xxDatabaseContext": "Server=xx.yy.net;Database=xxDatabase;User Id=xx;Password=*******;Trusted_Connection=False;" How should they differ? – Fredrik Ek Aug 11 '17 at 08:46
  • Can you ping the SQL Server server from the Linux server? Did you try connecting from the Linux server using SQL Operations Studio? – influent Nov 20 '17 at 17:45
  • Yes and yes. I did set up the sql server on the linux machine and can connect to it and run queries just fine. – Fredrik Ek Nov 22 '17 at 08:34
  • I saw the query is taking 35 seconds. Maybe you can setting command timeout setting ? By default timeout is 30 sec https://learn.microsoft.com/en-us/dotnet/api/system.data.sqlclient.sqlcommand.commandtimeout?view=netframework-4.7.2 – King Jk Apr 10 '19 at 06:07
  • 1
    Hi, it is a bit too late for this question, but. Did u solve this issue, because i am face with it too ? my sqlserver version = Microsoft SQL Server 2017 (RTM) - 14.0.1000.169 (X64) Aug 22 2017 17:04:49 Copyright (C) 2017 Microsoft Corporation Developer Edition (64-bit) on Windows Server 2019 Standard 10.0 (Build 17763: ) (Hypervisor) – Vadym Buhaiov Sep 11 '19 at 10:07
  • @VadymBuhaiov There is an open issue on SqlClient on Linux with MARS enabled [https://github.com/dotnet/SqlClient/issues/422](https://github.com/dotnet/SqlClient/issues/422) If you are using EF core 5. My workaround was to use `.AsSplitQuery()` on the timeout queries. – Alexrgs Apr 28 '21 at 17:51

1 Answers1

1

I think this is a duplicate of: https://github.com/aspnet/EntityFrameworkCore/issues/5989

Solution there: update the server software, because you are on RC2.

coding Bott
  • 4,287
  • 1
  • 27
  • 44