Easier way to debug a Windows service

后端 未结 28 1569
春和景丽
春和景丽 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:11

    I think it depends on what OS you are using, Vista is much harder to attach to Services, because of the separation between sessions.

    The two options I've used in the past are:

    • Use GFlags (in the Debugging Tools for Windows) to setup a permanent debugger for a process. This exists in the "Image File Execution Options" registry key and is incredibly useful. I think you'll need to tweak the Service settings to enable "Interact with Desktop". I use this for all types of debugging, not just services.
    • The other option, is to separate the code a bit, so that the service part is interchangable with a normal app startup. That way, you can use a simple command line flag, and launch as a process (rather than a Service), which makes it much easier to debug.

    Hope this helps.

    0 讨论(0)
  • 2020-11-22 16:11

    Use Windows Service Template C# project to create a new service app https://github.com/HarpyWar/windows-service-template

    There are console/service mode automatically detected, auto installer/deinstaller of your service and several most used features are included.

    0 讨论(0)
  • What I usually do is encapsulate the logic of the service in a separate class and start that from a 'runner' class. This runner class can be the actual service or just a console application. So your solution has (atleast) 3 projects:

    /ConsoleRunner
       /....
    /ServiceRunner
       /....
    /ApplicationLogic
       /....
    
    0 讨论(0)
  • 2020-11-22 16:13

    For routine small-stuff programming I've done a very simple trick to easily debug my service:

    On start of the service, I check for a command line parameter "/debug". If the service is called with this parameter, I don't do the usual service startup, but instead start all the listeners and just display a messagebox "Debug in progress, press ok to end".

    So if my service is started the usual way, it will start as service, if it is started with the command line parameter /debug it will act like a normal program.

    In VS I'll just add /debug as debugging parameter and start the service program directly.

    This way I can easily debug for most small kind problems. Of course, some stuff still will need to be debugged as service, but for 99% this is good enough.

    0 讨论(0)
  • 2020-11-22 16:13

    Here is the simple method which I used to test the service, without any additional "Debug" methods and with integrated VS Unit Tests.

    [TestMethod]
    public void TestMyService()
    {
        MyService fs = new MyService();
    
        var OnStart = fs.GetType().BaseType.GetMethod("OnStart", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static);
    
        OnStart.Invoke(fs, new object[] { null });
    }
    
    // As an extension method
    public static void Start(this ServiceBase service, List<string> parameters)
    {
         string[] par = parameters == null ? null : parameters.ToArray();
    
         var OnStart = service.GetType().GetMethod("OnStart", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static);
    
         OnStart.Invoke(service, new object[] { par });
    }
    
    0 讨论(0)
  • 2020-11-22 16:14

    I use a variation on JOP's answer. Using command line parameters you can set the debugging mode in the IDE with project properties or through the Windows service manager.

    protected override void OnStart(string[] args)
    {
      if (args.Contains<string>("DEBUG_SERVICE"))
      {
        Debugger.Break();
      }
      ...
    }
    
    0 讨论(0)
提交回复
热议问题