简体   繁体   中英

Azure SQL DB connection issues from C#

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?

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.

error: 40 - Could not open a connection to SQL Server).

  • In most cases, this error occurs because the SQL Server service is stopped.
  • If you are using SQL Server Express with Advanced Services or a named instance, this error will occur if the report server URL or connection string for the report server database is not correct.

To work through these issues:

  • Verify that the SQL Server ( MSSQLSERVER ) service is started.
  • Make sure that TCP/IP is enabled as a client protocol on the application server.
  • Try increasing the connection timeout value. We recommend using a connection timeout of at least 30 seconds.
  • Search for services in control panel, that will show a window with all services in your system, find your service SQLEXPRESS and click on Start . It should start your server instance.
  • If it is already in Start. Just Stop and start or simply Restart it.

在此处输入图片说明

Go to SQL Server Configuration manager --> SQL Server Network Configuration -->Protocol for MSSQLSERVER In the right pane split page you will have to disable

  • Shared Memory - Enable
  • Named Pipes - Enable
  • TCP/IP - Enable

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM