1

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,

  • dotnet run locally doesn't connect
  • containerized app service doesn't connect
  • Azure Data Studio locally does connect

I've found out the issue in the meantime, but I don't understand the cause:

  • dotnet run from WSL doesn't work; if I run the app from Windows it does work!!
  • I've therefore changed the App Service to build from source instead of running a containerized image, and it also does work.

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?

Etchelon
  • 832
  • 11
  • 27
  • So you are running the web app on IIS which is located on the same machine as the one you use to connect with Azure Data Studio? – Charlieface Oct 24 '21 at 20:40
  • I don't actually use IIS, it's a .NET 5 (Core let's say) app which runs as a simple console app. Actually, I've just found out that running the app from Windows works, but not if I run the app from WSL! Now with this in mind I need to find out why the same issue happens for the containerized app service on Azure. – Etchelon Oct 25 '21 at 00:25
  • Containerized probably has a different IP address? – Charlieface Oct 25 '21 at 00:47
  • Can you edit your question and add this info, and also please simplify your explanation to explain what works and what doesn't (i.e. in table form) – Nick.Mc Oct 25 '21 at 06:32

1 Answers1

0

Since you can access the server from the same machine, it is not a firewall issue.

There are two possibilities you could check:

  1. The connection string that your program is using is not what you expect. Try logging the connection string.
  2. The server is configured to use named pipes and not tcp. Azure Data Studio is configured to use named pipes and therefore works. Try checking the server configuration.
Shiraz Bhaiji
  • 64,065
  • 34
  • 143
  • 252