在 TransactionScope 错误中打开 SqlConnection

Opening SqlConnection inside TransactionScope error

我们一直在尝试在 TransactionScope 中使用 SqlConnection。当我们构建站点并尝试调用此数据库时,我们 运行 出现错误:

A transport-level error has occurred when receiving results from the server. (provider: TCP Provider, error: 0 - An existing connection was forcibly closed by the remote host.)

涉及的错误发生在cnn.Open().

using (var scope = new TransactionScope())
using (var cnn = new SqlConnection(connectionString))
{
    cnn.Open();
    int result = cnn.QuerySingle<int>("SELECT 1");
    Console.WriteLine(result);
}

我们创建了一个控制台应用程序来找出问题所在,并发现通过将我们的连接字符串关键字 'Pooling' 从 'false' 更改为 'true' 可以将此更改为 运行在控制台应用程序中并成功 return 我们的结果。

我们对站点连接字符串进行了相同的更改,出现与之前 returns 相同的错误。

此代码不起作用的原因是什么?

我假设 web.config 是法律,因为通过 Kudu 服务查看文件显示了我预期的连接字符串,但显然在 Azure 中情况并非如此。

我发现 Azure 发布配置文件正在覆盖我们的 web.config 连接字符串,此覆盖仍然包含 'Pooling=false'。

删除它现在允许我们的代码 运行 按预期进行。

This blog post 更多解释:

"When this code runs on a developer’s local machine, the value returned will be the one from the web.config file. However when this code runs in Windows Azure Web Sites, the value returned will instead be overridden with the value entered in the portal"