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

标签 c# debugging service

我收到错误消息“无法从命令行或调试器启动服务。必须先安装 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/

相关文章:

winapi - Windows服务和Windows驱动程序有什么区别?

windows - 通过批处理文件停止依赖于其他服务的 Windows 服务

c# - 为什么使用 Caliburn Micro Conductor.OneActive 时,Blend Interaction 事件触发器会多次触发?

c# - 我们能否在 C# 中重载 Point 对象以使其支持 double ?

c# - 测试代码的速度?

java - 如何混淆ProGuard,但在测试过程中保持名称可读性?

android - 前台服务在通知点击时被杀死

c# - 如何从 URL 获取图像到 pictureBox? ( Windows Mobile )

javascript - 如何获取promise的 "initiator"调试信息?

我们能否始终针对崩溃问题获得正确(或完整)的堆栈转储