Easier way to debug a Windows service

后端 未结 28 2039
春和景丽
春和景丽 2020-11-22 15:36

Is there an easier way to step through the code than to start the service through the Windows Service Control Manager and then attaching the debugger to the thread? It\'s ki

28条回答
  •  感动是毒
    2020-11-22 16:30

    I also think having a separate "version" for normal execution and as a service is the way to go, but is it really required to dedicate a separate command line switch for that purpose?

    Couldn't you just do:

    public static int Main(string[] args)
    {
      if (!Environment.UserInteractive)
      {
        // Startup as service.
      }
      else
      {
        // Startup as application
      }
    }
    

    That would have the "benefit", that you can just start your app via doubleclick (OK, if you really need that) and that you can just hit F5 in Visual Studio (without the need to modify the project settings to include that /console Option).

    Technically, the Environment.UserInteractive checks if the WSF_VISIBLE Flag is set for the current window station, but is there any other reason where it would return false, apart from being run as a (non-interactive) service?

提交回复
热议问题