C# 无法从命令行或调试器启动服务
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13987870/
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
Cannot start service from the command line or debugger
提问by Boomerang
I've created a windows service and installed it on a server. It appears to work fine ie doing what its meant to do. But when I log on to the server through remote desktop I get this message:
我创建了一个 Windows 服务并将其安装在服务器上。它似乎工作正常,即做它应该做的事情。但是当我通过远程桌面登录到服务器时,我收到以下消息:
Cannot start service from the command line or debugger. A windows Service must first be installed(using installutil.exe) and then started with the ServerExplorer, Windows Services Administrative tool or the NET START command.
无法从命令行或调试器启动服务。必须首先安装 Windows 服务(使用 installutil.exe),然后使用 ServerExplorer、Windows 服务管理工具或 NET START 命令启动。
I click on and then go to the services explorer to check the service, its started ok. No errors reported.
我点击然后去服务资源管理器检查服务,它开始正常。没有错误报告。
I've installed this so it uses Local System as "Log On As".
我已经安装了它,所以它使用本地系统作为“登录身份”。
Thanks.
谢谢。
回答by Jeroen Kok
Change the Main
method in Program
class as follows:
更改类中的Main
方法Program
如下:
/// <summary>
/// The main entry point for the application.
/// </summary>
private static void Main()
{
var myService = new MyService();
if (Environment.UserInteractive)
{
Console.WriteLine("Starting service...");
myService.Start();
Console.WriteLine("Service is running.");
Console.WriteLine("Press any key to stop...");
Console.ReadKey(true);
Console.WriteLine("Stopping service...");
myService.Stop();
Console.WriteLine("Service stopped.");
}
else
{
var servicesToRun = new ServiceBase[] { myService };
ServiceBase.Run(servicesToRun);
}
}
You have to add a Start
method to your service class:
您必须向Start
服务类添加一个方法:
public void Start()
{
OnStart(new string[0]);
}
Change the output type of the project to 'Console Application' instead of 'Windows Application' in the 'Application' tab of the project properties. Now you can just press F5 to start debugging but you can still run the executable as a Windows Service.
在项目属性的“应用程序”选项卡中将项目的输出类型更改为“控制台应用程序”而不是“Windows 应用程序”。现在您只需按 F5 即可开始调试,但您仍然可以将可执行文件作为 Windows 服务运行。
回答by fresko
Press CTRL-ALT-CANC (*), and go to Services tab. There is a list of services, search the one you need to start, select it and click "start". If it's not there, maybe it was uninstalled, not (correctly?) installed, or for some other reason your service is not known by Windows.
按 CTRL-ALT-CANC (*),然后转到服务选项卡。有一个服务列表,搜索您需要启动的服务,选择它并单击“启动”。如果它不存在,可能它已被卸载,没有(正确?)安装,或者由于某些其他原因 Windows 不知道您的服务。
(*) or CTRL-ALT-DEL(ete) or others, depending by the keyboard language.
(*) 或 CTRL-ALT-DEL(ete) 或其他,取决于键盘语言。
回答by TuanDPH
Goto App.config
转到 App.config
Find
找
<setting name="RunAsWindowsService" serializeAs="String">
<value>True</value>
Set to False
设置为假