【发布时间】:2019-10-21 17:47:33
【问题描述】:
我有一个应用程序应该使用命名实例连接到在远程服务器上运行的 SQL Server。
使用 IISExpress,我可以连接到 SQL Server,但是从 Docker(Linux 容器)它失败了 - 访问上下文时它只是挂起 - 没有抛出异常或错误消息。例如
var context = serviceScope.ServiceProvider.GetService<AppContext>();
context.Users.ToList();
在调试器中它永远不会到达异常处理程序。
相反,我看到很多这样的输出:
The thread 125 has exited with code 0 (0x0).
The thread 164 has exited with code 0 (0x0).
The thread 0xa4 has exited with code 0 (0x0).
使用 node.js,我甚至可以毫无问题地从 docker 连接到 SQL Server。
当我使用例如[ipaddress or host.docker.internal],1433 - 不幸的是,在远程 SQL Server 上,我无法配置固定端口。
- 使用 IISExpress 工作(因此在 Windows 上运行)
- 使用固定端口而不是命名实例连接到在开发计算机上运行的 SQL Server 时有效(但我无法更改我们的生产服务器)
- 在使用 node.js 时有效!来自 docker 也带有命名实例(两者:带有主机名或 IP 地址 - 所以我可以解析远程名称,防火墙不是问题)
- 尝试使用 Microsoft 容器作为基础的 ASP.NET Core 2.2 和 3.0preview5,例如FROM mcr.microsoft.com/dotnet/core/aspnet:3.0-buster-slim AS 基础
连接字符串:
"Data Source=remoteIP\\WEBDB;Initial Catalog=TestDB;User ID=testuser;Password=******;"
还尝试使用“Integrated Security=False;”在连接字符串中
服务器身份验证设置为混合
我可以重现这也是没有实体框架:
var conString = "Data Source=remoteIP\\WEBDB;Initial Catalog=TestDB;Integrated Security=False;User ID=testuser;Password=********";
using (SqlConnection conn = new SqlConnection(conString))
using (SqlCommand cmd = new SqlCommand("SELECT * FROM Users", conn))
{
conn.Open();
var rows = cmd.ExecuteNonQuery();
Console.WriteLine($"rows: {rows}");
}
它会挂在conn.Open();线上。
知道我能做什么吗?
更新:
- 输入的连接字符串不正确。
- 完整示例更新代码
编辑
这行得通:
var conString = "Data Source=10.0.75.1,1433;Initial Catalog=TestDB;Integrated Security=False;User ID=testuser;Password=********";
这不起作用:
var conString = "Data Source=10.0.75.1\\Developer;Initial Catalog=TestDB;Integrated Security=False;User ID=testuser;Password=********";
我可以 ping 远程 IP 地址以及远程服务器名称。
不幸的是,由于缺少依赖项,在 ubuntu 映像上安装 mssql-cli 失败。
【问题讨论】:
-
只使用 一个 反斜杠,如果需要转义则使用两个。
-
尝试将
remoteIP\\WEBDB改为ServerName\InstanceName,如果ServerName解析失败,尝试使用FQDN。
标签: c# sql-server docker asp.net-core