Windows服务调试状态下用Console启动

最近一直在用服务,发现服务也没有那么难调试。

Windows服务调试状态下用Console启动:步骤分两步

第一步改Program,启动代码

    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        static void Main(string[] args)
        {
            if (Environment.UserInteractive)
            {
                //交互模式下执行
                var test = new MyChatService();
                test.TestStartupAndStop(args);
            }
            else
            {
                ServiceBase[] ServicesToRun;
                ServicesToRun = new ServiceBase[]
                {
                new MyChatService()
                };
                ServiceBase.Run(ServicesToRun);
            }
        }
    }
MyChatService 为我的服务,TestStartupAndStop代码:在这个里面启动服务
        public void TestStartupAndStop(string[] args)
        {
            OnStart(args);
            Console.ReadLine();
            OnStop();
        }

第二步修改项目属性=》控制台应用程序

 ok,完成。

 

猜你喜欢

转载自www.cnblogs.com/zhuyapeng/p/11797647.html