I'm having trouble connecting with my .NET 5 web app to an Azure SQL DB. The weird thing is that I can connect to the DB using Azure Data Studio.
I've configured the Azure SQL Server firewall to accept connections from my IP address, and all of the Inbound/Outbound IP addresses of my Azure App Service. Before adding my local IP address to the firewall, I could not connect with Azure Data Studio, so that works as intended, but then I use the connection string provided by Azure itself (in ADO.NET format, since I'm using Entity Framework) inside my web app, and the app can't connect to the DB (it times out with 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) ).
I can't really understand why my localhost instance can't connect, as the IP address trying to connect to the db is the same as when I use Azure Data Studio! And also the App Service instance in Azure can't connect either (in the SQL Server's firewall I've also enabled connectivity from Azure App Services!!). The connection string is of the form
Server=tcp:[my server].database.windows.net;Initial Catalog=[my db];Persist Security Info=False;User ID=[my username];Password=[my password];MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;
Any hints?
EDIT: to recap as requested,
I've found out the issue in the meantime, but I don't understand the cause:
Why would running the app locally from WSL instead of Windows result in the Azure SQL Server's firewall blocking me?! Is WSL exposed to the internet with a different IP address?
And why does the same happen to an Azure App Service that runs the containerized version of my app?
Since you can access the server from the same machine, it is not a firewall issue.
There are two possibilities you could check:
error: 40 - Could not open a connection to SQL Server).
To work through these issues:
Go to SQL Server Configuration manager --> SQL Server Network Configuration -->Protocol for MSSQLSERVER In the right pane split page you will have to disable
Please refer Connectivity Errors , Troubleshoot connecting to the SQL Server , Known Issue and error: 40 and SO
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.