【问题标题】:Azure and SQL Server: Named Pipes Provider, error: 40 - Could not open a connection to SQL ServerAzure 和 SQL Server:命名管道提供程序,错误:40 - 无法打开与 SQL Server 的连接
【发布时间】:2013-05-14 01:23:20
【问题描述】:

将 Azure 与 SQL Server 结合使用。在隐式调用 SimpleRoleProvider 的 30% 的情况下,我们会收到错误:命名管道提供程序,错误:40 - 无法打开与 SQL Server 的连接。该错误经常发生但不可重现。好像超时了..

[Authorize(Roles = "Administrator")]
public class AdminController : Controller
{
   ...
}

堆栈跟踪如下所示:

[Win32Exception (0x80004005): Access is denied]

[SqlException (0x80131904):建立与 SQL Server 的连接时发生与网络相关或特定于实例的错误。服务器未找到或无法访问。验证实例名称是否正确以及 SQL Server 是否配置为允许远程连接。 (提供者:命名管道提供者,错误:40 - 无法打开与 SQL Server 的连接)] System.Data.SqlClient.SqlInternalConnection.OnError(SqlException异常,布尔breakConnection,Action1 wrapCloseInAction) +5296071 System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose) +558 System.Data.SqlClient.TdsParser.Connect(ServerInfo serverInfo, SqlInternalConnectionTds connHandler, Boolean ignoreSniOpenTimeout, Int64 timerExpire, Boolean encrypt, Boolean trustServerCert, Boolean integratedSecurity, Boolean withFailover) +5308555 System.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(ServerInfo serverInfo, String newPassword, SecureString newSecurePassword, Boolean ignoreSniOpenTimeout, TimeoutTimer timeout, Boolean withFailover) +145 System.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(ServerInfo serverInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString connectionOptions, SqlCredential credential, TimeoutTimer timeout) +889 System.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(TimeoutTimer timeout, SqlConnectionString connectionOptions, SqlCredential credential, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance) +307 System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, SqlCredential credential, Object providerInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString userConnectionOptions) +434 System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions) +225 System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnectionPool pool, DbConnectionOptions options, DbConnectionPoolKey poolKey, DbConnectionOptions userOptions) +37 System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnectionOptions userOptions) +558 System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnectionOptions userOptions) +67 System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal& connection) +1052 System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, TaskCompletionSource1重试,DbConnectionOptions userOptions,DbConnectionInternal&连接)+78 System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection) +167 System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource1 retry, DbConnectionOptions userOptions) +143 System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry) +83 System.Data.SqlClient.SqlConnection.Open() +96 System.Web.DataAccess.SqlConnectionHolder.Open(HttpContext context, Boolean revertImpersonate) +88 System.Web.DataAccess.SqlConnectionHelper.GetConnection(String connectionString, Boolean revertImpersonation) +239 System.Web.Security.SqlRoleProvider.GetRolesForUser(字符串用户名)+762 WebMatrix.WebData.SimpleRoleProvider.GetRolesForUser(字符串用户名)+54 System.Web.Security.RolePrincipal.IsInRole(字符串角色) +9461104

我们自己的服务器上没有这个问题。这是配置问题吗?我们正在使用 Azure 的免费试用版。

--- 添加 ---

不好的是 MVC 框架“处理”请求,在给出的示例中(角色 管理)。框架的用途是增加一个抽象层来方便地处理“域请求”。那么遇到这种情况该怎么办呢?

提前感谢您的帮助!

【问题讨论】:

  • 奇怪,但我们感觉使用 IExplorer(与 Chrome 相比)会更频繁地发生错误。
  • 我不认为这是与浏览器相关的问题。你从哪里连接到数据库?您的网络服务器在本地吗?
  • 数据库是 Azure 数据库。是的,你是对的,这不可能是浏览器的问题。尽管如此,从统计数据来看,Chrome 效果更好(!?!?)

标签: sql-server asp.net-mvc asp.net-mvc-4 azure azure-sql-database


【解决方案1】:

我假设 Azure 和 SQL Server 是指SAAS Azure SQL Database(名称更改速度快于我无法跟踪的速度),而不是 IAAS SQL Server on Windows Azure。首先,您需要确定您的应用程序是否受到限制,请参阅 Connection Constraints 并仔细阅读这篇文章:Causes of Connection Termination。如果您的应用程序受到限制,那么您需要重新审视您的设计并解决您受到限制的具体原因。

【讨论】:

  • 感谢您的帮助。我们将本地数据库部署到 Sql Azure,猜想这使它成为 Windows Azure 上的 SQL Server。大多数会话由于超时而受到限制。坏事是 MVC 框架“处理”给出的示例中的请求。框架的用途是增加一个抽象层来方便地处理“域请求”。那么在这种情况下该怎么办呢?
  • 没有“因超时而受到限制”之类的东西。节流意味着您订阅的一个非常具体的状态,而不仅仅是“错误”的另一个名称。请仔细阅读链接的文章并遵循其中的建议。
【解决方案2】:

预计在 SQL Azure 上。它应该发生在大约 0.5% 的时间,而不是 30%。

Here's a previous question that addressed this topic

SQL Azure 中的连接失败很常见。这是因为你的 应用程序将创建一个连接池,但是当您认为 这些连接已经结束,Azure 可以终止它们 你永远不会知道。

您应该在代码中实现重试逻辑,以防出现这些超时之一。 Microsoft 已经为此提供了一个库:Transient Fault Handling Application Block

【讨论】:

    猜你喜欢
    • 2011-03-08
    • 2013-02-18
    • 1970-01-01
    • 2017-12-20
    • 2016-09-28
    • 1970-01-01
    • 1970-01-01
    • 2012-04-14
    相关资源
    最近更新 更多