asp.net - 无法从 docker 支持的 asp.net core 项目连接到 sql server

标签 asp.net docker asp.net-core sql-server-2014-express

我有一个 ASP.NET Core 2.2 项目,我为其打开了 docker 支持。只要不需要数据库连接,该项目就可以正常运行。例如登录。当我输入用户凭据时,出现如下错误。

An unhandled exception occurred while processing the request. 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.)

System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, object providerInfo, bool redirectedUserInstance, SqlConnectionString userConnectionOptions, SessionData reconnectSessionData, bool applyTransientFaultHandling) InvalidOperationException: An exception has been raised that is likely due to a transient failure. If you are connecting to a SQL Azure database consider using SqlAzureExecutionStrategy.

Microsoft.EntityFrameworkCore.Storage.Internal.SqlServerExecutionStrategy+d__7.MoveNext()



以前在我的sql server配置管理器中没有启用TCP/IP时,错误是不同的。(不记得错误)
我按照链接中提到的步骤https://jack-vanlightly.com/blog/2017/9/24/how-to-connect-to-your-local-sql-server-from-inside-docker来解决这个问题。

我也启用了 TCP/IP 和命名管道。我也可以使用来自 SQL Management Studio 的 IP 进行连接。

当前连接字符串:
"ConnectionStrings": {
    "DefaultConnection": "Server=xxx.xx.xx.x,1433;Database=TestDB;User ID=username;Password=pwd;Trusted_Connection=True;MultipleActiveResultSets=true"
  },

如果我遗漏了任何步骤,请告诉我。我想从我的 docker 项目连接到本地 sql

最佳答案

这花了我一些时间来解决,有一些小问题可能是错误的,这让我很沮丧。这可能会变得更加令人沮丧,因为自动生成的 .net 核心 Web 应用程序 Visual Studio 2017 项目不能直接开箱即用。将这种经验作为您第一次接触 Docker 的经验并不理想。

我最初也有一个错误的假设,即 docker 镜像将与容器内的 SQL Server 一起提供,但事实并非如此,它试图访问必须预先安装在主机上的数据库服务器。

要遵循的步骤(针对 windows docker 镜像进行测试,而不是针对 linux);

1) 通过运行命令提示符并键入 IPCONFIG 来获取主机的 IP 地址

2) 将 appsettings.json 文件中的数据库连接字符串设置为此 Ip 地址,后跟 SQL Server 端口号,即;

192.168.X.X,1433

3)设置连接字符串不使用Trusted_Connection(从连接字符串中删除这个)和用户ID和密码中的硬编码;

用户 ID=sa;密码=SuperSecurePassword;

如果我不这样做,在某些 SQL Server 配置上我会得到一个不寻常的错误(不太记得具体细节!)

连接字符串显示看起来像这样;

“服务器=192.168.X.X,1433;数据库=MyDatabase;用户ID=sa;密码=SuperSecurePassword;MultipleActiveResultSets=true”

4)现在在主机上你需要打开windows防火墙,并为TCP端口1433添加一个新的入站连接规则。

5) 做完这些,还是不行,重启试试;挣扎了半天,不得不重启,才恢复了生机;我不确定 Docker 是否正确初始化,但这主要是猜测!我真的不喜欢说重启是解决问题的方法,它不是真正的答案,但在某些情况下它确实有效。

编辑,最后一件事,以管理员模式运行visual studio(右键单击图标,“以管理员身份运行”)也有帮助。

为旧线程的复活而道歉,但考虑到需要完成的事情很少,这个问题似乎仍然存在,并且关于如何解决这个问题的可用信息有点不完整。

关于asp.net - 无法从 docker 支持的 asp.net core 项目连接到 sql server,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48145881/

相关文章:

asp.net - 使用sharpsvn API从ASP.Net访问Visual SVN

javascript - 将通过 Ajax(和 Web 服务方法)获得的数组转换为 javascript 数组

linux - 每次我尝试构建我的 Docker Ubuntu 16 :04 image 时出现错误请求 (400) 消息

c# - 我的模型中缺少帐户、ManagerViewModel 文件夹和 ApplicationUser.cs 类

c# - jQuery 对话框在 Response.TransmitFile 之后不会从代码隐藏中关闭

c# - 在 Visual Studio 中使用设计 View 时隐藏母版页

docker - 没有基本身份验证凭据 - `docker push image_name`

docker - docker克隆现有的Elasticsearch容器

data-binding - Asp.Net Core Model绑定(bind),如何获取空字段绑定(bind)为空字符串?

javascript - signalR 核心定义传输