无法从命令行或调试器启动服务

问题描述 投票:0回答:1

我正在开发一个Windows服务项目,但我面临着不同的问题。

1.

无法从命令行或调试器启动服务。

我已经使用 installutil 安装了服务,但是安装服务后,它可以在 mmc 中启动。

  1. 我之前使用过这个代码:
public void onDebug()
{
    OnStart(null);
}
#if DEBUG
            Service1 myService = new Service1();
            myService.onDebug();
            Thread.Sleep(System.Threading.Timeout.Infinite);
#else
            ServiceBase[] ServicesToRun;
            ServicesToRun = new ServiceBase[]
            {
                new Push_Message_Service()
            };
            ServiceBase.Run(ServicesToRun);
#endif

因为我想在Visual Studio中调试但有时需要在Service mmc中启动服务。当我需要启动服务时,我不想在 Release 文件夹中构建。 所以请大家帮我解决问题,谢谢大家!

c# windows-services visual-studio-debugging installutil
1个回答
0
投票

我在 Visual Studio 2022 中创建了一个 Windows 服务项目,当我调试 Windows 服务时,它弹出错误消息。

如果不安装并启动服务,您似乎无法从 Visual Studio 中的代码进行调试。 您是否遇到过相同的错误消息?如果是这样,请确保您已经通过 installutil 安装了 Windows 服务并启动了您的服务。 请参考如何启动服务

正如您上面提到的,您已经使用

#if DEBUG
来调试 Windows 服务并且工作正常。

或者您可以通过Windows服务控制管理器启动服务,然后将调试器附加到线程。 了解更多信息,请关注调试服务

这是一张类似的票:

调试 Windows 服务的更简单方法

希望能帮到你。

© www.soinside.com 2019 - 2024. All rights reserved.