简体   繁体   English

安装后Windows服务无法启动

[英]Windows Service doesn't start after installation

I have built one windows service that sends out email after every 30 minutes in C#. 我已经构建了一个Windows服务,在C#中每30分钟发送一次电子邮件。 The Service start mode is set to Automatic. 服务启动模式设置为自动。 But still windows doesn't start automatic. 但仍然无法自动启动Windows。 I need to manually start by going to Services.msc and right clicking the service and select start 我需要手动启动,转到Services.msc并右键单击该服务并选择start

When the StartMode is set to automatic, that just means that it will start up when Windows boots up. 当StartMode设置为自动时,这意味着它将在Windows启动时启动。

You can start the service yourself in a custom action in your installer. 您可以在安装程序中的自定义操作中自行启动该服务。 I assume you have an Installer class already and that it is already a custom action for your setup project since the service is installing, but not starting. 我假设您已经有一个Installer类,并且自安装该服务以来它已经是您的安装项目的自定义操作,但是没有启动。

Override the OnAfterInstall method in the Installer class you have and you can start the service like this: 覆盖您拥有的Installer类中的OnAfterInstall方法,您可以像这样启动服务:

protected override void OnAfterInstall(IDictionary savedState) {
    base.OnAfterInstall(savedState);

    ServiceController sc = new ServiceController(“MyServiceName”);
    sc.Start();
}

However, a scheduled task is not a bad way to go. 但是,计划任务并不是一个糟糕的方法。

Why put yourself through all the overhead and suffering of troubleshooting and maintaining a windows service for a time based/polling application? 为什么要让自己完成故障排除和为基于时间/轮询应用程序维护Windows服务的所有开销和痛苦? The windows OS has built in support for this. Windows操作系统内置了对此的支持。 Just create a simple console application. 只需创建一个简单的控制台应用 Run it as a scheduled task. 将其作为计划任务运行。

You should already have unit tests and decoupling to make the code unit-testable. 您应该已经进行了单元测试和解耦,以使代码可单元测试。 If you don't your troubleshooting is overly difficult. 如果不这样做,您的故障排除就太困难了。 Once you have your code in this unit-testable format flipping to a console app is a no brainer. 一旦你有了这个单元可测试格式的代码,翻转到控制台应用程序是没有道理的。

I knew a guy who made everything a windows service and labeled it SOA. 我认识一个让一切都成为Windows服务并将其标记为SOA的人。 Piling up windows services for polling/time based mechanisms isn't SOA. 为基于轮询/时间的机制堆积Windows服务不是SOA。 Its so sloppy compared to console applications and so much more difficult to maintain I can't even begin to express how bad an idea it is. 它与控制台应用程序相比如此草率,而且难以维护,我甚至无法开始表达它是多么糟糕的想法。 I had to deal with about ~20-30 of these win services and once they were converted to n-tier and a console app suddenly the stability of the application went through the roof and my life got 10x easier. 我不得不处理大约20-30个这样的获胜服务,一旦它们被转换为n层和控制台应用程序突然之间,应用程序的稳定性经历了屋顶,我的生活变得轻松了10倍。 So please, do yourself a favor and listen to somebody who has been through months and many iterations of these types of app. 所以,请帮自己一个忙,听听经历了几个月和这些类型的应用程序的多次迭代的人。 Run it as a scheduled task in a console app. 在控制台应用程序中将其作为计划任务运行。

Auto-starting services are subject to problems with service initialization order. 自动启动服务会受到服务初始化顺序的影响。 You have plenty of dependencies, the TCP/IP stack better be in working order before you try to send an email, for example. 例如,在尝试发送电子邮件之前,您有很多依赖项,TCP / IP堆栈最好处于正常工作状态。 Look in the Windows event log for an exception message that prevented OnStart() from getting the service started. 在Windows事件日志中查找阻止OnStart()启动服务的异常消息。

This can be configured for a service, check out the Dependencies tab for the Print Spooler service for example. 这可以配置为服务,例如,查看Print Spooler服务的Dependencies选项卡。 This is however difficult to deal with, hard to figure out exactly which services need to be running and hard to write the registry entries that configure the dependencies. 然而,这很难处理,很难确切地知道哪些服务需要运行并且难以编写配置依赖项的注册表项。

Punt the problem: don't send an email message right away. 解决问题:不要立即发送电子邮件。 Wait a while, 30 minutes for example. 等一会儿,例如30分钟。

You install it with installutil? 你用installutil安装它? You're right, it doesn't start the service, even if it's set to automatic. 你是对的,它不会启动服务,即使它被设置为自动。 If I were you, I'd provide a batch file which calls installutil and then also runs 'net start whatever'. 如果我是你,我会提供一个调用installutil的批处理文件,然后运行'net start whatever'。 Or if you're using other kinds of installation, those should provide this ability too. 或者如果你正在使用其他类型的安装,那些也应该提供这种能力。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM