如何将 Azure SQL 托管实例连接到 Azure 应用服务(用 .NET Core 2.2 编写)

How to connect an Azure SQL Managed Instance to an Azure App Service (written in .NET Core 2.2)

我有一个部署到 Azure 应用服务的 .NET Core 2.2 Web 应用程序,我正在连接到 Azure SQL 托管实例。 Appsettings.json中要写入的连接字符串应该是什么。我尝试了在 Azure 门户中找到的以下连接字符串,但它不起作用。

"AZURESQLCONNSTR_":Server=tcp:azure-abcde.database.windows.net,1433;Persist Security Info=False;User ID=userid;Password=password;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;

但是当我尝试 运行 下面的 Azure 应用服务时,我没有得到任何数据。

http://productwebapp.azurewebsites.net/api/ProductDetails/GetProductDetails.

能否请您提出解决此问题的方法?

您必须允许某些 IP 地址才能连接到它。查看 SQL 数据库服务器的防火墙设置。你可以通过你电脑的IP地址(或IP范围),来访问SQL数据库

确保启用允许 Azure 服务和资源访问服务器

编辑:

关注资源

Create an Azure SQL Database managed instance

Connect an Azure App Service hosted application

正如您所说,您有一个部署到 Azure 应用服务的 .NET Core 2.2 Web 应用程序,您想要连接到 Azure SQL 托管实例。

您的 Web 应用程序是 Azure App Service 主机应用程序。

我想你可以参考这个教程:Connect an Azure App Service hosted application:

托管实例只能通过私有 IP 地址访问,因此要从 Azure 应用服务访问它,您首先需要在应用程序和托管实例 VNet 之间建立连接。参见 Integrate your app with an Azure Virtual Network

有关疑难解答,请参阅 Troubleshooting VNets and Applications. If a connection cannot be established, try synching the networking configuration

将 Azure 应用服务连接到托管实例的一个特例是将 Azure 应用服务集成到与托管实例 VNet 对等的网络。这种情况需要设置以下配置:

  • 托管实例 VNet 不能有网关
  • 托管实例 VNet 必须设置使用远程网关选项
  • 对等 VNet 必须设置允许网关传输选项

另一个参考:https://docs.microsoft.com/bs-latn-ba/azure/sql-database/sql-database-connect-query-dotnet-core

希望对您有所帮助。