cco*_*ook 3 c# sql-server ado.net networking named-pipes
类似(如果不是相同的问题)随机遇到网络路径未找到异常,但我有代码可以重现该问题,因此我想再次询问,因为它似乎是一个独立于硬件的真实问题并且可以重现。
这是错误:
提供程序:命名管道提供程序,错误:40 - 无法打开与 SQL Server 的连接) ---> System.ComponentModel.Win32Exception (0x80004005):在 System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection) 处找不到网络路径owningObject、UInt32 waitForMultipleObjectsTimeout、布尔值allowCreate、布尔值onlyOneCheckConnection、DbConnectionOptions userOptions、DbConnectionInternal& 连接)位于 System.Data.ProviderBase.DbConnectionPool.WaitForPendingOpen()
为了重现这一点,我创建了一个每分钟运行一次的控制台应用程序(我们还有一个 Dapper DAL 测试,因此有参数):
internal class Program
{
private static int _totalOpenConnections;
private static readonly Stopwatch Timer = new Stopwatch();
private static bool _hasError;
private static int Main(string[] args)
{
var list = Enumerable.Range(1, Settings.Default.TotalCommandsToExecute);
// simple ADO.NET test
if (args.Length > 0 && args[0].Equals("ado", StringComparison.OrdinalIgnoreCase))
{
Console.WriteLine("Beginning ADO.NET Test...");
Timer.Restart();
Parallel.ForEach(list, new ParallelOptions {MaxDegreeOfParallelism = Settings.Default.ConcurrentCount},
i => AsyncContext.Run(async () =>
{
try
{
PrintStatus(i);
await TestADONet();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
_hasError = true;
}
}));
Timer.Stop();
Console.WriteLine($"Completed ADO.NET Test in {Timer.ElapsedMilliseconds} ms");
}
if (_hasError)
return 1;
return 0;
}
private static void PrintStatus(int index)
{
Console.WriteLine(
$"Started: {index} of {Settings.Default.TotalCommandsToExecute}\tCurrently Open: {_totalOpenConnections}");
}
private static async Task TestADONet()
{
using (var conn = new SqlConnection(Settings.Default.TestConnection))
{
await conn.OpenAsync();
Interlocked.Increment(ref _totalOpenConnections);
var command = new SqlCommand("SELECT 1 Field1, 2 Field2, 3 Field3", conn);
var reader = await command.ExecuteReaderAsync();
while (reader.Read())
{
var result = new TestEntity
{
Field1 = reader.GetInt32(0),
Field2 = reader.GetInt32(1),
Field3 = reader.GetInt32(2)
};
}
}
Interlocked.Decrement(ref _totalOpenConnections);
}
public class TestEntity
{
public int Field1 { get; set; }
public int Field2 { get; set; }
public int Field3 { get; set; }
}
}
Run Code Online (Sandbox Code Playgroud)
应用程序设置 ConcurrentCount = 100,TotalCommandsToExecute = 200。这个想法是通过并行异步命令来充分利用连接池。
该应用程序重现了它,但是,它也出现在控制台应用程序、Web 应用程序(ASP.NET MVC 和 ASP.NET WebForms)的生产中。
它也相当随机地发生。我们已经让 Rackspace 和一些 DBA 就这个问题在环境中进行了爬行,但没有结果,这导致了这个应用程序 - 它在开发环境中重现了它。
连接字符串相当平淡,形式为“Data Source=;Database=;User Id=;Password=”
SQL Server 2014,但这发生在两个独立的服务器(dev/rackspace)上
测试中的查询是故意良性的
"SELECT 1 Field1, 2 Field2, 3 Field3"
该测试确实使用 Nito.AsyncEx(此处使用的唯一非系统程序集)来获得异步支持。同样,该问题出现在不使用此程序集的其他应用程序中,因此我认为这不是问题 - 如果没有,请告诉我,我会以另一种方式重现它。
非常感谢任何想法!
问题出在命名管道上。它可能在虚拟机中更多地表达(从下面的链接推测)。使用 TCP/IP,通过在连接字符串中添加 tcp: 并指定端口解决了该问题。
一些相关案例:
结论,除非 SQL Server 在同一台计算机上,否则始终显式使用 TCP/IP。您也可以将 SQL Server 配置为不接受命名管道,但今后我也只会将其添加到我的连接字符串中。