间歇性 System.ComponentModel.Win32Exception:找不到网络路径

Posted

技术标签:

【中文标题】间歇性 System.ComponentModel.Win32Exception:找不到网络路径【英文标题】:Intermittent System.ComponentModel.Win32Exception: The network path was not found 【发布时间】:2016-11-26 01:21:53 【问题描述】:

自从我们迁移到 Azure 后,我们发现我们的数据库存在一些问题。

System.ComponentModel.Win32Exception:找不到网络路径

使用此调用堆栈:

System.Data.Entity.Core.EntityException: The underlying provider failed on Open. ---> System.Data.SqlClient.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: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server) ---> System.ComponentModel.Win32Exception: The network path was not found
   --- End of inner exception stack trace ---
   at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
   at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
   at System.Data.SqlClient.TdsParser.Connect(ServerInfo serverInfo, SqlInternalConnectionTds connHandler, Boolean ignoreSniOpenTimeout, Int64 timerExpire, Boolean encrypt, Boolean trustServerCert, Boolean integratedSecurity, Boolean withFailover)
   at System.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(ServerInfo serverInfo, String newPassword, SecureString newSecurePassword, Boolean ignoreSniOpenTimeout, TimeoutTimer timeout, Boolean withFailover)
   at System.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(ServerInfo serverInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString connectionOptions, SqlCredential credential, TimeoutTimer timeout)
   at System.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(TimeoutTimer timeout, SqlConnectionString connectionOptions, SqlCredential credential, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance)
   at System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, SqlCredential credential, Object providerInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString userConnectionOptions, SessionData reconnectSessionData)
   at System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions)
   at System.Data.ProviderBase.DbConnectionFactory.CreateNonPooledConnection(DbConnection owningConnection, DbConnectionPoolGroup poolGroup, DbConnectionOptions userOptions)
   at System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection)
   at System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
   at System.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource`1 retry)
   at System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry)
   at System.Data.SqlClient.SqlConnection.Open()
   at System.Data.Entity.Infrastructure.Interception.InternalDispatcher`1.Dispatch[TTarget,TInterceptionContext](TTarget target, Action`2 operation, TInterceptionContext interceptionContext, Action`3 executing, Action`3 executed)
   at System.Data.Entity.Infrastructure.Interception.DbConnectionDispatcher.Open(DbConnection connection, DbInterceptionContext interceptionContext)
   at System.Data.Entity.Infrastructure.DbExecutionStrategy.<>c__DisplayClass1.<Execute>b__0()
   at System.Data.Entity.Infrastructure.DbExecutionStrategy.Execute[TResult](Func`1 operation)
   at System.Data.Entity.Core.EntityClient.EntityConnection.Open()
   --- End of inner exception stack trace ---
   at System.Data.Entity.Core.EntityClient.EntityConnection.Open()
   at System.Data.Entity.Core.Objects.ObjectContext.EnsureConnection(Boolean shouldMonitorTransactions)
   at System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction[T](Func`1 func, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction, Boolean releaseConnectionOnSuccess)
   at System.Data.Entity.Core.Objects.ObjectQuery`1.<>c__DisplayClass7.<GetResults>b__5()
   at System.Data.Entity.Infrastructure.DbExecutionStrategy.Execute[TResult](Func`1 operation)
   at System.Data.Entity.Core.Objects.ObjectQuery`1.GetResults(Nullable`1 forMergeOption)
   at System.Data.Entity.Core.Objects.ObjectQuery`1.<System.Collections.Generic.IEnumerable<T>.GetEnumerator>b__0()
   at System.Data.Entity.Internal.LazyEnumerator`1.MoveNext()
   at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable`1 source)
   at System.Linq.Queryable.FirstOrDefault[TSource](IQueryable`1 source, Expression`1 predicate)

建议是将等级从 S0 增加到 S1,我们这样做了,但仍然没有帮助。

第二个问题是:

System.ComponentModel.Win32Exception:连接尝试失败,因为连接方在一段时间后没有正确响应,或者连接失败,因为连接的主机没有响应

第三个是:

System.Data.SqlClient.SqlException:用户“MyUserName”登录失败。

我们试过了:

public class SqlAzureConfiguration : DbConfiguration
    
        public SqlAzureConfiguration() 
            SetExecutionStrategy("System.Data.SqlClient", () => new SqlAzureExecutionStrategy()); 
        
    

这没有帮助。任何想法是什么问题以及如何解决?

编辑 1:我们的网站现在一直失败。我用 DTU 的数量截屏。如您所见,我们有 20 个 DTU,即使使用率非常低,它也会不断失败。

编辑 2

我们怀疑会话超出限制,我们在 AppInsight 上跟踪了会话,但仍然不是问题:

【问题讨论】:

您使用的是什么连接字符串? (请从中删除任何敏感信息以保护您的服务器) @drediske onnectionString="Server=tcp:********.database.windows.net,1433;数据源=*****.database.windows.net;初始Catalog=Production;Persist Security Info=False;User ID=*****;Password=****;Pooling=False;MultipleActiveResultSets=True;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;"跨度> 不确定是否相关,但当我们收到 Webhook 时会发生这种情况。而且由于我们可以为我们正在使用一些锁的同一家公司接收 webhook,这可能是问题吗? 您能通过 andrela@microsoft.com 向我发送电子邮件,并附上您的数据库和服务器名称吗? 【参考方案1】:

我们增加了连接字符串中的超时时间:

连接超时=30;

连接超时=1800;

这很有帮助。已经一个星期了,到目前为止我们还没有发现任何问题。

【讨论】:

【参考方案2】:

任何遇到此错误的人,尤其是在 Azure 上,尝试将“tcp:”添加到应用程序连接字符串中的 db-server-name 中。这会强制 sql 客户端使用 tcp 与 db 进行通信。我假设默认情况下连接是 UDP,并且可能存在间歇性连接问题。 这与 OP 发布的内容略有不同,因为他的连接字符串具有 tcp,尽管如此尝试这个很重要

【讨论】:

以上是关于间歇性 System.ComponentModel.Win32Exception:找不到网络路径的主要内容,如果未能解决你的问题,请参考以下文章

Azure DevOps Build 出现错误:找不到程序集“System.ComponentModel.Annotations”

无法将类型为“System.Windows.Forms.SplitContainer”的对象强制转换为类型“System.ComponentModel.ISupportInitialize”

csharp System.ComponentModel.DataAnnotations

DevExpress 在 .net6 'System.ComponentModel.Win32Exception' 上将 GridView 导出到 Excel [重复]

csharp 需要添加引用System.ComponentModel.Composition

使用 AvalonDock 2.0 时未处理的“System.ComponentModel.Win32Exception”