[英]Azure SQL DB connection issues from C#
I'm having trouble connecting with my .NET 5 web app to an Azure SQL DB.我在将 .NET 5 Web 应用程序连接到 Azure SQL DB 时遇到问题。 The weird thing is that I can connect to the DB using Azure Data Studio.
奇怪的是我可以使用 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.我已将 Azure SQL Server 防火墙配置为接受来自我的 IP 地址以及 Azure 应用服务的所有入站/出站 IP 地址的连接。 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) ).
在将本地 IP 地址添加到防火墙之前,我无法与 Azure Data Studio 连接,因此按预期工作,但随后我使用了 Azure 本身提供的连接字符串(采用 ADO.NET 格式,因为我使用的是实体框架) ) 在我的 Web 应用程序中,并且该应用程序无法连接到数据库(在与SQL Server 建立连接时发生与网络相关或特定于实例的错误而超时。找不到服务器或无法访问服务器。验证实例名称正确并且 SQL Server 配置为允许远程连接。(提供程序:TCP 提供程序,错误:40 - 无法打开到 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!我真的不明白为什么我的 localhost 实例无法连接,因为尝试连接到数据库的 IP 地址与我使用 Azure Data Studio 时的 IP 地址相同! 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!!).
而且 Azure 中的应用服务实例也无法连接(在 SQL Server 的防火墙中,我还启用了来自 Azure 应用服务的连接!!)。 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; 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;连接超时=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?!为什么从 WSL 而不是 Windows 本地运行应用程序会导致 Azure SQL Server 的防火墙阻止我?! Is WSL exposed to the internet with a different IP address?
WSL 是否使用不同的 IP 地址暴露给 Internet?
And why does the same happen to an Azure App Service that runs the containerized version of my app?为什么运行我的应用程序容器化版本的 Azure 应用服务会发生同样的情况?
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).
错误:40 - 无法打开与 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转到 SQL Server 配置管理器 --> SQL Server 网络配置 --> MSSQLSERVER 的协议 在右窗格拆分页中,您必须禁用
Please refer Connectivity Errors , Troubleshoot connecting to the SQL Server , Known Issue and error: 40 and SO请参阅连接错误、 连接到 SQL Server 的疑难解答、 已知问题和错误:40和SO
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.