Windows 服务在启动时挂起

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1234188/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 12:56:41  来源:igfitidea点击:

Windows Service Hung on Starting during boot

.netwindowsvb.netclr

提问by BlueMonkMN

We have two services developed in Visual Studio .NET 2008 with VB.NET. Both were developed completely separately by separate individuals (one of them being myself), but both are reporting the same error during boot: "service hung on starting" appears in the System event log after booting. The services proceed to start up fine after that message (just a few messages down the line is the notice that the service started). Is there something about the slowness of loading the .NET framework or JIT compiling the code that's causing this or what? It starts and stops fine when done manually. Mine is a very simple service with no startup code other than that provided by the framework.

我们有两个在 Visual Studio .NET 2008 和 VB.NET 中开发的服务。两者都是由不同的人(其中一个是我自己)完全单独开发的,但在启动过程中都报告了相同的错误:启动后系统事件日志中出现“服务挂起”。在该消息之后,服务继续正常启动(只有几条消息是服务启动的通知)。加载 .NET 框架或 JIT 编译导致此或什么的代码的速度是否缓慢?手动完成时,它可以正常启动和停止。Mine 是一个非常简单的服务,除了框架提供的启动代码外,没有其他启动代码。

Update 1:This is all I've got in OnStart:

更新 1:这是我在 OnStart 中的全部内容:

  host = New ServiceHost(GetType(FSE.Licensing.FSELicense))
  host.Open()

FSELicense does not define a constructor, so it just gets the default empty public constructor provided by VB.NET, I guess.

FSELicense 没有定义构造函数,所以它只是获取 VB.NET 提供的默认空公共构造函数,我猜。

Update 2:My question has morphed based on the solution proposed by a colleague which supposedly fixed the problem. This solution does in fact simply add a dependency on another service that I did not think was necessary since my service does not do anything until a request is made of it. However, it does declare a variable of a COM type. Is it possible that having a reference to this COM type (even though there is no instance) will cause the COM DLL to load at the same time as the service, and possibly rely on a service? I didn't realize COM references were like static links in .NET if so.

更新 2:根据一位同事提出的解决方案,我的问题已经发生了变化,据称该解决方案解决了该问题。这个解决方案实际上只是添加了对另一个我认为没有必要的服务的依赖,因为我的服务在发出请求之前不会做任何事情。但是,它确实声明了一个 COM 类型的变量。是否有可能引用此 COM 类型(即使没有实例)会导致 COM DLL 与服务同时加载,并可能依赖于服务?如果是这样,我没有意识到 COM 引用就像 .NET 中的静态链接。

回答by Scott Fletcher

I solved my problem by adding a service dependency to the service for "HTTP SSL service" (HTTPFilter). I had the exact same problem on an XP machine with a Windows Service that I wrote in .NET that exposed a WCF endpoint with a netpipe and http binding. When the system started up, it would always hang on the host.Open() call, and eventually timeout. I could start the service manually with no problem.

我通过向“HTTP SSL 服务”(HTTPFilter) 的服务添加服务依赖项解决了我的问题。我在带有 Windows 服务的 XP 机器上遇到了完全相同的问题,该服务是我在 .NET 中编写的,该服务公开了一个带有网络管道和 http 绑定的 WCF 端点。当系统启动时,它总是挂在 host.Open() 调用上,并最终超时。我可以毫无问题地手动启动该服务。

See this link for instructions on adding a service dependency. The DependOnServicevalue that I used was HTTPFilter

有关添加服务依赖项的说明,请参阅此链接。DependOnService我使用的价值是HTTPFilter

回答by oefe

Try to minimize the processing in OnStart; in particular, avoid blocking for an unknown amount of time; e.g. don't try to connect remote computers -- no database queries, no service requests.

尽量减少处理OnStart;特别是,避免在未知时间内阻塞;例如,不要尝试连接远程计算机——没有数据库查询,没有服务请求。

If you need to do something that might block or otherwise take a long time, spawn a separate thread for this task if possible.

如果你需要做一些可能会阻塞或需要很长时间的事情,如果可能的话,为这个任务生成一个单独的线程。

However, you cannot always avoid blocking completely, as you need to make sure that your services is operational when you return from OnStart. In such cases, you can use ServiceBase.RequestAdditionalTimeto request more time for OnStartto finish.

但是,您不能总是完全避免阻塞,因为您需要确保从OnStart. 在这种情况下,您可以使用ServiceBase.RequestAdditionalTime请求更多时间OnStart来完成。

回答by Klaus Schüssler

Adding a dependency on HTTPFilter sometimes solves the problem for us. We've used this workaround for some windows server 2008 systems. This HTTPFilter service doesn't seem to be installed on some Windows 7 Professional 64bit systems, which will then actually caused for the problem that the wcf service host doesnt start(cannot find the dependency).

添加对 HTTPFilter 的依赖有时可以为我们解决问题。我们已经在一些 windows server 2008 系统上使用了这个解决方法。这个HTTPFilter服务在某些Windows 7 Professional 64位系统上似乎没有安装,这实际上会导致wcf服务主机无法启动(找不到依赖项)的问题。

回答by David

This is a guess, but during system startup, so many things are loading behind the scenes that this slows things down already.

这是一个猜测,但在系统启动期间,在幕后加载了很多东西,这已经减慢了速度。

Then you also have to worry about components that rely on other services to be started. For example, if your service relied on the SQL Server Agent, which relies on the SQL Server Database Engine services, you'd have to wait for the to load in the order of the dependencies.

然后你还要担心依赖其他服务启动的组件。例如,如果您的服务依赖于依赖于 SQL Server 数据库引擎服务的 SQL Server 代理,则您必须等待按依赖项的顺序加载。

So...

所以...

Depending on what FSE.Licensing.FSELicense does internally, it may be waiting for other services that it depends on to load first, or it could just be that the machine is slow, and loading all of the background processes is happening at the same time and your service is simply competing for resources with all of those processes.

根据 FSE.Licensing.FSELicense 在内部执行的操作,它可能正在等待它所依赖的其他服务先加载,或者可能只是机器很慢,并且正在加载所有后台进程同时发生并且您的服务只是在与所有这些进程竞争资源。

However, the warning you're seeing i just a warning. I've seen this on some of my services that can take a while to load. This is obvious and I probably don't even need to say it, but the service control manager expects services to load within a specific time period (I'm not sure what that is) but if a service appears to hang, it logs this message but in the meantime, the service is still trying to load. As long as your service is starting, I wouldn't worry too much about this warning, unless you can see some way to code it better to avoid this.

但是,您看到的警告只是警告。我已经在我的一些服务上看到了这种情况,这些服务可能需要一段时间才能加载。这是显而易见的,我可能甚至不需要说出来,但服务控制管理器希望服务在特定时间段内加载(我不确定那是什么)但如果服务似乎挂起,它会记录此消息,但与此同时,该服务仍在尝试加载。只要您的服务正在启动,我就不会太担心这个警告,除非您能找到更好的编码方法来避免这种情况。

One thing you could check is in the Services console, look at the properties of your service and look at the "Dependencies" tab. it may be blank, but it may give you some insight.

您可以检查的一件事是在服务控制台中,查看服务的属性并查看“依赖项”选项卡。它可能是空白的,但它可能会给你一些洞察力。