随笔——写windows服务的时候如何调试 c# .net

流程

1、更改项目 应用程序——输出类型——windows应用程序 改为 控制台应用程序

2、Program启动类中添加调用代码

3、服务类里面添加启动方法去启动OnStart和 Console.ReadLine();停止OnStop方法。

 

操作

1、更改项目

 

 2、Program添加代码

 

  /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        static void Main()
        {
#if DEBUG
            //调试的时候记得把 应用程序——输出类型——windows应用程序 改为 控制台应用程序
            if (Environment.UserInteractive)
            {
                Service1 service1 = new Service1();
                service1.TestStartupAndStop();
            }
#endif
#if !DEBUG
//下面这块是调用服务的,创建服务自动生成的
ServiceBase[] ServicesToRun; ServicesToRun = new ServiceBase[] { new Service1() }; ServiceBase.Run(ServicesToRun); #endif }

 

3、服务类里面添加方法调用start和stop

     /// <summary>
        /// 添加个内部方法,用于调试
        /// </summary>
        /// <param name="args"></param>
        internal void TestStartupAndStop()
        {
#if DEBUG
            this.OnStart(new string[0]);
            Console.ReadLine();
            this.OnStop();
#endif
        }

 

posted @ 2020-11-03 16:28  兮去  阅读(190)  评论(0编辑  收藏  举报