WSL2 Azure SQL Connection Issue

Asiful Nobel 196 Reputation points
2022-07-01T13:41:49.213+00:00

I am running a .NET 6 web app on WSL2 (Windows Sub-system for Linux), but having connection issue with Azure SQL connection. I keep getting the following exception.

System.InvalidOperationException: The exception handler configured on ExceptionHandlerOptions produced a 404 status response. This InvalidOperationException containing the original exception was thrown since this is often due to a misconfigured ExceptionHandlingPath. If the exception handler is expected to return 404 status responses then set AllowStatusCode404Response to true.  
---> System.Data.Entity.Core.EntityException: The underlying provider failed on Open.  
---> 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: TCP Provider, error: 40 - Could not open a connection to SQL Server)  
at System.Data.ProviderBase.DbConnectionPool.CheckPoolBlockingPeriod(Exception e)  
at System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection)  
at System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection)  
at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal& connection)  
at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection)  
at System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection)  

I am using the connection string in the following format.

Server=tcp:[ServerName].database.windows.net,1433;Initial Catalog=[DatabaseName];Persist Security Info=False;User ID=[UserName];Password=[Password];MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;  

This works on Windows, but it does not work on WSL2. I checked with tcpping and nslookup that the database server and port is reachable, but the app cannot connect to it for some reason.

Does anyone know why this is happening and is there a solution?

Azure SQL Database
Developer technologies ASP.NET ASP.NET Core
0 comments No comments
{count} vote

Accepted answer
  1. Alberto Morillo 34,671 Reputation points MVP Volunteer Moderator
    2022-07-01T17:52:42.263+00:00

    It seems it cannot access the Azure SQL Database. Have you added the public IP address of your Internet connection to the whitelist of Azure SQL Database firewall. Please read here to know how.

    It could be also related to DNS resolution. If you the server [ServerName].database.windows.net, the ping should fail but when the ping fails it returns the current IP address of the Azure SQL Database server. If you don't see that then the issue may be related to DNS resolution from your computer.

    We need to open the ports from the WSL2 please read this article. Here you will find more information about networking and WSL2,

    Here you will find all steps to configure WSL2 to access a SQL Server/SQL Azure instance.

    Make sure the corporate network allows outbound traffic on port TCP 1433.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.