gpt4 book ai didi

c# - 在 C# 中调试窗口系统服务

转载 作者:行者123 更新时间:2023-11-30 15:06:11 27 4
gpt4 key购买 nike

我收到错误消息“无法从命令行或调试器启动服务。必须先安装 Windows 服务(使用 installutil.exe),然后使用 ServerExplorer、Windows 服务管理工具或 NET START 命令启动。

那么,有没有办法在不安装的情况下运行或测试 Windows 服务?我应该在控制台应用程序中构建我的项目,然后在测试后将代码传输到 Windows Server 项目吗?

谢谢。

最佳答案

我倾向于将静态 Main 方法添加到我的服务类中,以便它可以作为控制台应用程序调用以进行调试,但也可以作为服务安装和运行。

类似下面的内容:

    public partial class ControllerService : ServiceBase
{

static void Main(string[] args)
{
ControllerService service = new ControllerService();

cmdLine = CommandLineParser.Parse(args);

if (Environment.UserInteractive)
{
switch (cmdLine.StartMode)
{
case StartMode.Install:
//Service Install Code Here
break;
case StartMode.Uninstall:
//Service Uninstall Code Here
break;
case StartMode.Console:
default:
service.OnStart(args);
consoleCloseEvent.WaitOne();
break;
}
}
else
{
ServiceBase.Run(service);
}
}

protected override void OnStart(string[] args)
{
//Do Start Stuff Here
}

protected override void OnStop()
{
if (Environment.UserInteractive)
{
consoleCloseEvent.Set();
}
}
}

关于c# - 在 C# 中调试窗口系统服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8157178/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com