C# 重新启动后“等待服务连接时达到超时”错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1986292/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
"A timeout was reached while waiting for the service to connect" error after rebooting
提问by Zack Elan
I have a custom-written Windows service that I run on a number of Hyper-V VMs. The VMs get rebooted a couple times an hour as part of some automated tests being run. The service is set to automatic start and almost all of the time, it starts up fine.
我有一个在许多 Hyper-V VM 上运行的自定义编写的 Windows 服务。作为正在运行的一些自动化测试的一部分,虚拟机每小时重新启动几次。该服务设置为自动启动,并且几乎所有时间都能正常启动。
However, maybe 5% of the time, with no pattern that I can discern, the service fails to start. When it fails, I get an error in Event Viewer saying
但是,可能有 5% 的时间,在我无法辨别模式的情况下,服务无法启动。当它失败时,我在事件查看器中收到一条错误消息
A timeout was reached (30000 milliseconds) while waiting for the My Service Name service to connect.
等待 My Service Name 服务连接时超时(30000 毫秒)。
When this occurs, I can start the service manually, or restart again, and the service will start fine.
发生这种情况时,我可以手动启动该服务,或者再次重新启动,该服务将正常启动。
The thing I can't figure out is that the 30 second timeout doesn't appear to be occurring in my code. The very first line of my service class's OnStart() method logs "Starting..." to its log4net log. When the service fails to start, I don't even get anything logged at all, which indicates to me that either log4net can't log for whatever reason, or the timeout is occurring before my OnStart() gets called.
我无法弄清楚的是,我的代码中似乎没有发生 30 秒超时。我的服务类的 OnStart() 方法的第一行将“Starting...”记录到其 log4net 日志中。当服务无法启动时,我什至没有记录任何内容,这向我表明无论出于何种原因 log4net 无法登录,或者在调用 OnStart() 之前发生超时。
The service runs on a variety of OSes, from XP all the way up to Win7 and 2008R2, and I know that setting the service to delayed start may solve this for Vista and later, but that seems like a hack.
该服务在各种操作系统上运行,从 XP 一直到 Win7 和 2008R2,我知道将服务设置为延迟启动可能会解决 Vista 及更高版本的问题,但这似乎是一种黑客攻击。
I haven't been able to remote debug this because of the fact that it happens so intermittently and during system startup, and I'm at a loss as to further ways to try to figure out what's going on. Any ideas?
我一直无法远程调试它,因为它间歇性地发生在系统启动期间,而且我不知道进一步尝试找出发生了什么的方法。有任何想法吗?
回答by Jeremy McGee
My guess - and that's all it is - is that the disk is thrashing hard during startup, to the point where the .NET Framework itself isn't starting in the 30 seconds that Windows allocates for services to start.
我的猜测 - 这就是全部 - 是磁盘在启动过程中剧烈抖动,以至于 .NET Framework 本身在 Windows 分配给服务启动的 30 秒内没有启动。
A kludgy workaround may be to set the service to start manually, then write a very small stub service in unmanaged code (e.g. C++, Delphi) to start the service.
一个笨拙的解决方法可能是将服务设置为手动启动,然后用非托管代码(例如 C++、Delphi)编写一个非常小的存根服务来启动服务。
Another approach may be to start the service remotely from another machine. The sc
command should do the job nicely.
另一种方法可能是从另一台机器远程启动服务。该sc
命令应该可以很好地完成工作。
回答by Paul Farry
I think I may have also found another contributing factor to this kind of does not start on reboot error.
我想我可能还发现了另一个导致这种不会在重启时启动错误的因素。
It appears that if the Windows Event Log is set to Overwrite Events > 7days.. size 512kb.. But a lot of activity has occurred within this window, then Event Log is effectively full because it can't overwrite the number of events generated inside that timeframe. If you set the eventlog to a much larger size OR to Overwrite as needed then you won't experience this issue
看来,如果 Windows 事件日志设置为覆盖事件 > 7 天.. 大小 512kb.. 但是在此窗口中发生了大量活动,则事件日志实际上已满,因为它无法覆盖内部生成的事件数量那个时间范围。如果您将事件日志设置为更大的大小或根据需要覆盖,那么您将不会遇到此问题
回答by Jason Richmeier
For what it's worth, I discovered that I received this message (almost immediately upon service startup) because I did not have version 4.5 of the .NET framework installed on the target machine. I rolled back the version I was using to version 4.0 (which was already installed on the target machine) and the service worked as expected.
无论如何,我发现我收到了这条消息(几乎是在服务启动时立即收到),因为我没有在目标机器上安装 .NET 框架的 4.5 版。我将我使用的版本回滚到 4.0 版(已安装在目标机器上)并且服务按预期工作。
回答by Robert Achmann
My issue with the same error was that the .Net installation on the server was not working correctly.
我遇到相同错误的问题是服务器上的 .Net 安装无法正常工作。
To figure this out:
要弄清楚这一点:
I made a small console app with identical logic as the executing service, and I made a try-catch around the whole code piece, dumping it all out to console.
我制作了一个与执行服务具有相同逻辑的小型控制台应用程序,并围绕整个代码段进行了尝试,将其全部转储到控制台。
Not sure why the information didn't bubble up, but we saw the valuable messages about the Framework errors that we would never have seen otherwise.
不知道为什么信息没有冒出来,但我们看到了关于框架错误的有价值的消息,否则我们永远不会看到。
回答by JDTLH9
I was seeing this error in the Event Viewer when trying to install a service with powershell.
尝试使用 powershell 安装服务时,我在事件查看器中看到此错误。
The problem I had was that I had different values for "Service Name" and "Service Display Name" in my powershell script to those that I had specified in the program.cs file of my Console Application.
我遇到的问题是我的 powershell 脚本中的“服务名称”和“服务显示名称”的值与我在控制台应用程序的 program.cs 文件中指定的值不同。
回答by Kosmo
We are having the same problem on Windows 2016 Server.
我们在 Windows 2016 Server 上遇到了同样的问题。
A fix that seems to be working is changing the user under which the service running from Local Service Account to local Administrator (not sure what's the cause).
似乎有效的修复程序正在将运行服务的用户从本地服务帐户更改为本地管理员(不确定原因是什么)。