简体   繁体   English

无法从 Azure 应用服务连接到 SQL 数据库

[英]Can't connect to SQL Database from Azure Application Service

I have two Application Services running .NET Core 3.1 apps that connect to an Azure SQL Database.我有两个运行 .NET Core 3.1 应用程序的应用程序服务连接到 Azure SQL 数据库。 One app is a simple test app to test connectivity to the database using the same DI pipeline, the other is the full application.一个应用程序是一个简单的测试应用程序,用于使用相同的 DI 管道测试与数据库的连接性,另一个是完整的应用程序。 The test application connects to the database and produces the desired output while the production application fails to connect with the following exception message:测试应用程序连接到数据库并生成所需的 output,而生产应用程序无法连接并显示以下异常消息:

A network-related or instance-specific error occurred while establishing a connection to SQL Server.与 SQL 服务器建立连接时发生与网络相关或特定于实例的错误。 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.验证实例名称是否正确,并且 SQL 服务器配置为允许远程连接。 (provider: SNI_PN11, error: 26 - Error Locating Server/Instance Specified) (提供者:SNI_PN11,错误:26 - 错误定位服务器/指定实例)

I've modified the rules on the database to include my development workstations IP address and both applications connect to the database as expected.我已经修改了数据库的规则以包括我的开发工作站 IP 地址,并且两个应用程序都按预期连接到数据库。 On the database firewall, I've also done the following:在数据库防火墙上,我还做了以下事情:

  1. Added the IP address for the test and production apps to the database firewall.将测试和生产应用程序的 IP 地址添加到数据库防火墙。 It's the IP address designated as virtual ip address in the properties section of the App Service.它是应用服务属性部分中指定为virtual ip address的 IP 地址。 The two app services share a virtual IP address.这两个应用服务共享一个虚拟 IP 地址。
  2. Enabled the Allow Azure services and resources to access this server setting in the console.在控制台中启用了Allow Azure services and resources to access this server设置。

The applications' appsettings.json files are identical.应用程序的appsettings.json文件是相同的。

When I deploy the test app to the production server it still works.当我将测试应用程序部署到生产服务器时,它仍然可以工作。 Something in the production app is causing the connection to fail.生产应用程序中的某些内容导致连接失败。 Why can't the production app connect and what else can be done to troubleshoot the issue?为什么生产应用程序无法连接,还有什么办法可以解决问题?

Wondering if there was something wrong with my connection string, I added some logging to write it out to the log file.想知道我的连接字符串是否有问题,我添加了一些日志记录以将其写入日志文件。 I realized the connection string was wrong - it was trying to connect to a different database.我意识到连接字符串是错误的——它试图连接到不同的数据库。 AppSettings.Production.json was overriding the connection string, my test app had an empty AppSettings.Production.json which is why it worked. AppSettings.Production.json 覆盖了连接字符串,我的测试应用程序有一个空的 AppSettings.Production.json 这就是它起作用的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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