Simplest AspNetCore MVC app in Linux App Service can't connect to a SQL Server database

Viewed 234

Summary

I recently updated my Linux App Service hosted from ASP.NET Core MVC 2.2 to 3.1 and ever since my application can no longer access the SQL Server database that lives in a VM, also in Azure.

#Findings I deployed in Linux App Service an empty ASP.NET Core MVC 3.1 app that has 2 views - one that with a result from controller with static data (return "Hello world!") and one that connects to the database.

The application runs fine on my machine, however in Azure, only the "static" view works fine. When I try to get the view with the database access, I get an error 502 after a loooooong time of waiting (~ 4 min)

502 - Web server received an invalid response while acting as a gateway or proxy server. There is a problem with the page you are looking for, and it cannot be displayed. When the Web server (while acting as a gateway or proxy) contacted the upstream content server, it received an invalid response from the content server.

When I looked in the App plan, I see that CPU has hit 90+%.

I went in Azure and checked the logs and found:

2020-03-18T22:19:31.461251550Z An unhandled exception has occurred while executing the request.
2020-03-18T22:19:31.467998655Z System.InvalidOperationException: Timeout expired. The timeout period elapsed prior to obtaining a connection from the pool. This may have occurred because all pooled connections were in use and max pool size was reached.
2020-03-18T22:19:31.468013855Z at System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection) 2020-03-18T22:19:31.468019355Z at System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource1 retry, DbConnectionOptions userOptions)
2020-03-18T22:19:31.468023955Z at System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource1 retry, DbConnectionOptions userOptions) 2020-03-18T22:19:31.468028355Z at System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource1 retry)
2020-03-18T22:19:31.468032555Z at System.Data.SqlClient.SqlConnection.Open()
2020-03-18T22:19:31.468086255Z at testing_aspnetcore3.Controllers.GogoController.Index() in C:\work\trash\trash\testing_aspnetcore3\Controllers\GogoController.cs:line 29
2020-03-18T22:19:31.468093955Z at lambda_method(Closure , Object , Object[] )
2020-03-18T22:19:31.468098155Z at Microsoft.Extensions.Internal.ObjectMethodExecutor.Execute(Object target, Object[] parameters)
2020-03-18T22:19:31.468589455Z at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.SyncActionResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments)
2020-03-18T22:19:31.468601655Z at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeActionMethodAsync()
2020-03-18T22:19:31.468841055Z at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
2020-03-18T22:19:31.468852055Z at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeNextActionFilterAsync()
2020-03-18T22:19:31.468856555Z --- End of stack trace from previous location where exception was thrown ---
2020-03-18T22:19:31.468860855Z at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Rethrow(ActionExecutedContextSealed context)
2020-03-18T22:19:31.468865155Z at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
2020-03-18T22:19:31.468869555Z at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeInnerFilterAsync()
2020-03-18T22:19:31.468881555Z --- End of stack trace from previous location where exception was thrown ---
2020-03-18T22:19:31.468885855Z at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.g__Awaited|24_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
2020-03-18T22:19:31.468890255Z at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResourceExecutedContextSealed context)
2020-03-18T22:19:31.468894155Z at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
2020-03-18T22:19:31.468898155Z at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeFilterPipelineAsync()
2020-03-18T22:19:31.468901955Z --- End of stack trace from previous location where exception was thrown ---
2020-03-18T22:19:31.468905955Z at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope)
2020-03-18T22:19:31.469268156Z at Microsoft.AspNetCore.Routing.EndpointMiddleware.g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)
2020-03-18T22:19:31.469314656Z at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
2020-03-18T22:19:31.469324256Z at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)

Details

Here's the code to access the database:

public IActionResult Index()
{
    using (var conn = new SqlConnection(ConnectionString))
    using (var sqlCommand = new SqlCommand("SELECT top 1 * from login", conn))
    {
        conn.Open();

        using (var reader = sqlCommand.ExecuteReader())
        {
            reader.Read();

            return Ok(reader["Username"]);
        }
    }
}

P.S. Problem occurs when deployed in Linux Container. Doesn't occur on Framework-dependent windows deployment, neither on windows container.

0 Answers
Related