首页 > 解决方案 > 将 ASP.NET 样板应用程序部署到 Azure 时出现 SQLException

问题描述

过去几天,我一直在尝试将 ASP.NET 样板应用程序部署到 Azure,但一直未能成功。我一直偶然发现这个错误:

应用程序 .azurewebsites 域上的错误

首先,我创建了一个 Web App + SQL 资源,然后使用右键单击 -> 部署选项从 Visual Studio 中将我的项目发布到它,然后导入我从 Azure 获得的发布配置文件。我确保appsettings.production.json中的数据库连接字符串正确,但我似乎无法连接到数据库。如果我尝试通过 SQL Server Management Studio 连接到数据库,我会收到类似的错误。这意味着数据库本身有问题。

我是否需要在 Azure 中创建一个单独的 SQL 数据库,以使用 Web App + MySQL 资源附带的 SQL 数据库服务器?

任何帮助将不胜感激!我已经在这个问题上花费了太多时间。

这是 Azure 门户中所有资源的屏幕截图,这些资源是在创建 Web 应用程序 + MySQL 资源时创建的:

Azure 门户资源

编辑:这是我得到的错误:

Win32Exception: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. Unknown location SqlException: 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: 0 - A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.) Microsoft.Data.ProviderBase.DbConnectionPool.CheckPoolBlockingPeriod(Exception e) InvalidOperationException: An exception has been raised that is likely due to a transient failure. Consider enabling transient error resiliency by adding 'EnableRetryOnFailure()' to the 'UseSqlServer' call. Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerExecutionStrategy.Execute<TState, TResult>(TState state, Func<DbContext, TState, TResult> operation, Func<DbContext, TState, ExecutionResult<TResult>> verifySucceeded) Win32Exception: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.

标签: mysqlasp.netazureentity-framework-coreasp.net-boilerplate

解决方案


由于您提到您无法以任何其他方式访问 db,因此我提供了分步说明

您可以从 Azure 资源面板获取连接字符串

在此处输入图像描述

导航到连接安全选项卡并启用从其他 Azure 资源的访问。这样,WebApp 将能够访问 Azure 网络内的数据库服务器。我还添加了我的客户端 IP,以便稍后从我的 PC 测试数据库连接。完成后点击顶部的保存

在此处输入图像描述

在同一页面上,有一个名为配置 SSL 连接的设置。证书可以从https://docs.microsoft.com/uk-ua/azure/mysql/howto-configure-ssl下载。我使用了这个文件https://www.digicert.com/CACerts/BaltimoreCyber​​TrustRoot.crt.pem。或者,您现在可以禁用此功能。

在此处输入图像描述

现在我们有了测试数据库连接的一切。我将使用 HeidiSQL 客户端来展示它。

在此处输入图像描述 在此处输入图像描述

单击打开并查看您的数据库

在此处输入图像描述

据此,需要将CACertificateFile参数附加到连接字符串才能使用 SSL,因此您需要将文件放入存储库并在连接字符串中提供其相对路径


推荐阅读