C# Windows 服务 SQL Server 连接问题 [英] C# Windows service SQL Server connection issue
问题描述
在 Windows Server 2012 上,我创建了一个使用服务帐户运行的 C# Windows 服务.
On Windows Server 2012, I have created a C# Windows service which runs with a service account.
在构造函数中,我正在使用 ADO.NET 从 SQL Server 数据库读取配置.
In constructor I am reading configuration from a SQL Server database using ADO.NET.
当我尝试启动服务时,它失败并出现以下异常.几次尝试启动服务后,它正在启动.
When I am trying to start the service, it fails with below exceptions. After a few attempts to start the service, it is getting started.
"等待操作超时outconnection Timeout Expired.超时尝试使用登录前握手时经过的时间段确认."
"The wait operation timed outconnection Timeout Expired. The timeout period elapsed while attempting to consume the pre-login handshake acknowledgement."
以下是来自日志的异常信息
Below is the exception message from Logs
等待操作超时outconnection Timeout Expired.尝试使用登录前握手确认时超时时间已过.
这可能是因为登录前握手失败或服务器无法及时响应.
尝试连接到此服务器所花费的时间为 - [Pre-Login] 初始化 = 21169;握手=332;
The wait operation timed outconnection Timeout Expired. The timeout period elapsed while attempting to consume the pre-login handshake acknowledgement.
This could be because the pre-login handshake failed or the server was unable to respond back in time.
The duration spent while attempting to connect to this server was - [Pre-Login] initialization=21169; handshake=332;
at system.Data.sqlclient.sqlinternalconnection.onError(sqlException 异常, Boolean breakconnection, Action'l wrapcloseinAction)
在系统.数据.Sqlclient.TdsParser.ThrowExceptionAndwarning(TdsParserStateobject stateobj, Boolean callerHasconnectionLock, Boolean asyncclose)
在 system.Data.sqlclient.TdsParserstateoblect.ReadsniError(TdsParserstateobject stateobj, unt32 error)
在 system.Data.sqlclient.TdsParserstateoblect.ReadsnisyncoverAsync()
在系统.数据.Sqlclient.TdsParserStateob)ect.TryReadNetworkPacket()
在系统.数据.Sqlclient.TdsParser.consumePreLoginiiandshake(Boolean encrypt, Boolean trustservercert, Booleanintegratedsecurity, Boolean& marscapable)
在 system.Data.sqlclient.TdsParser.connect(serverinfo serverinfo, sqlinternalconnectionTds connHandler, Boolean ignoresniopenTimeout, 1nt64 timerExpire, Boolean encrypt, Boolean trustservercert, Boolean integrationsecurity, Boolean withFailover)
at system.Data.sqlclient.sqlinternalconnectionTds.AttemptoneLogin(serverinfo serverinfo, string newPassword, securestring newsecurePassword, Boolean ignoresniopenTimeout, TimeoutTimer timeout, Boolean withFailover)
在 system.Data.sqlclient.sqlinternalconnectionTds.LoginNoFailover(serverinfo serverinfo, string newPassword, securestring newsecurePassword, Boolean redirecteduserinstance, sqlconnectionstring connectionoptions, sqlcredential credential, TimeoutTimer timeout)
在 system.Data.sqlclient.sglinternalconnectionTds.openLoginEnlist(TimeoutTimer timeout, sqlconnectionstring connectionoptions, sqlcredential credential, string newPassword, securestring newsecurePassword, Boolean redirecteduserinstance)
在 system.Data.sqlclient.sqlinternalconnectionTds..ctor(DbconnectionPoolidentity identity, sqlconnectionstring connectionoptions, sqlcredential credential, object providerinfo, string newPassword, securestring newsecurePassword, Boolean redirecteduserinstance, sqlconnectionstring userconnectionoptions, sessionData reconnectsessionData)
在 system.Data.sqlclient.sqlconnectionFactory.createconnection(Dbconnectionoptions options, DbconnectionPoolKey poolKey, object poolGroupProviderinfo, DbconnectionPool pool, Dbconnection owningconnection, Dbconnectionoptions useroptions)
在 system.Data.ProviderBasembconnectionFactory.createPooledconnection(DbconnectionPool pool, Dbconnection owningobject, Dbconnectionoptions options, DbconnectionPoolKey poolKey, Dbconnectionoptions useroptions)
在 system.Data.ProviderBasembconnectionPool.createobject(Dbconnection owningobject, Dbconnectionoptions useroptions, Dbconnectioninternal oldconnection)
在 system.Data.ProviderBasembconnectionPool.usercreateRequest(Dbconnection owningobject, Dbconnectionoptions useroptions, Dbconnectioninternal oldconnection)
在 system.Data.ProviderBasembconnectionPool.TryGetconnection(Dbconnection owningobject, uInt32 waitFormultipleobjectsTimeout, Boolean allowcreate, Boolean onlyonecheckconnection, Dbconnectionoptions useroptions, Dbconnectioninternal& connection)
在 system.Data.ProviderBase.DbconnectionPool.TryGetconnection(Dbconnection owningobject, Taskcompletionsource'l retry, Dbconnectionoptions useroptions, Dbconnectioninternal& connection)
在 system.Data.ProviderBasembconnectionFactory.TryGetconnection(Dbconnection owningconnection, Taskcompletionsource'l retry, Dbconnectionoptions useroptions, Dbconnectioninternal oldconnection, Dbconnectioninternal& connection)
在 system.Data.ProviderBase.Dbconnectioninternal.Tryopenconnectioninternal(Dbconnection externalconnection, DbconnectionFactory connectionFactory, Taskcompletionsource'l retry, Dbconnectionoptions useroptions)在 system.Data.ProviderBasembconnectionclosed.Tryopenconnection(Dbconnection 外连接,DbconnectionFactory connectionFactory,Taskcompletionsource'l retry,Dbconnectionoptions useroptions)在 system.Data.sqlclient.sqlconnection.Tryopeninner(Taskcompletionsource'l retry)在 system.Data.sqlclient.sqlconnection.Tryopen(Taskcompletionsource'l retry)在 system.Data.sqlclient.sqlconnection.open()
at system.Data.sqlclient.sqlinternalconnection.onError(sqlException exception, Boolean breakconnection, Action'l wrapcloseinAction)
at System. Data. Sqlclient.TdsParser.ThrowExceptionAndwarning(TdsParserStateobject stateobj, Boolean callerHasconnectionLock, Boolean asyncclose)
at system.Data.sqlclient.TdsParserstateoblect.ReadsniError(TdsParserstateobject stateobj, unt32 error)
at system.Data.sqlclient.TdsParserstateoblect.ReadsnisyncoverAsync()
at System. Data. Sqlclient.TdsParserStateob)ect.TryReadNetworkPacket()
at System. Data. Sqlclient.TdsParser.consumePreLoginiiandshake(Boolean encrypt, Boolean trustservercert, Boolean integratedsecurity, Boolean& marscapable)
at system.Data.sqlclient.TdsParser.connect(serverinfo serverinfo, sqlinternalconnectionTds connHandler, Boolean ignoresniopenTimeout, 1nt64 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.sglinternalconnectionTds.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.ProviderBasembconnectionFactory.createPooledconnection(DbconnectionPool pool, Dbconnection owningobject, Dbconnectionoptions options, DbconnectionPoolKey poolKey, Dbconnectionoptions useroptions)
at system.Data.ProviderBasembconnectionPool.createobject(Dbconnection owningobject, Dbconnectionoptions useroptions, Dbconnectioninternal oldconnection)
at system.Data.ProviderBasembconnectionPool.usercreateRequest(Dbconnection owningobject, Dbconnectionoptions useroptions, Dbconnectioninternal oldconnection)
at system.Data.ProviderBasembconnectionPool.TryGetconnection(Dbconnection owningobject, uInt32 waitFormultipleobjectsTimeout, Boolean allowcreate, Boolean onlyonecheckconnection, Dbconnectionoptions useroptions, Dbconnectioninternal& connection)
at system.Data.ProviderBase.DbconnectionPool.TryGetconnection(Dbconnection owningobject, Taskcompletionsource'l retry, Dbconnectionoptions useroptions, Dbconnectioninternal& connection)
at system.Data.ProviderBasembconnectionFactory.TryGetconnection(Dbconnection owningconnection, Taskcompletionsource'l retry, Dbconnectionoptions useroptions, Dbconnectioninternal oldconnection, Dbconnectioninternal& connection)
at system.Data.ProviderBase.Dbconnectioninternal.Tryopenconnectioninternal(Dbconnection outerconnection, DbconnectionFactory connectionFactory, Taskcompletionsource'l retry, Dbconnectionoptions useroptions)
at system.Data.ProviderBasembconnectionclosed.Tryopenconnection(Dbconnection outerconnection, DbconnectionFactory connectionFactory, Taskcompletionsource'l retry, Dbconnectionoptions useroptions)
at system.Data.sqlclient.sqlconnection.Tryopeninner(Taskcompletionsource'l retry)
at system.Data.sqlclient.sqlconnection.Tryopen(Taskcompletionsource'l retry)
at system.Data.sqlclient.sqlconnection.open()
推荐答案
在连接字符串中添加MultiSubnetFailover=True"后问题解决
Issue resolved after adding "MultiSubnetFailover=True" in connection string
感谢大家的帮助.
这篇关于C# Windows 服务 SQL Server 连接问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!