How do I debug Windows services in Visual Studio?

后端 未结 17 647
梦毁少年i
梦毁少年i 2020-11-28 03:56

Is it possible to debug the Windows services in Visual Studio?

I used code like

System.Diagnostics.Debugger.Break();

but it is givi

相关标签:
17条回答
  • 2020-11-28 04:04

    Either that as suggested by Lasse V. Karlsen, or set up a loop in your service that will wait for a debugger to attach. The simplest is

    while (!Debugger.IsAttached)
    {
        Thread.Sleep(1000);
    }
    
    ... continue with code
    

    That way you can start the service and inside Visual Studio you choose "Attach to Process..." and attach to your service which then will resume normal exution.

    0 讨论(0)
  • 2020-11-28 04:05

    I just added this code to my service class so I could indirectly call OnStart, similar for OnStop.

        public void MyOnStart(string[] args)
        {
            OnStart(args);
        }
    
    0 讨论(0)
  • 2020-11-28 04:05

    Unfortunately, if you're trying to debug something at the very start of a Windows Service operation, "attaching" to the running process won't work. I tried using Debugger.Break() within the OnStart procecdure, but with a 64-bit, Visual Studio 2010 compiled application, the break command just throws an error like this:

    System error 1067 has occurred.
    

    At that point, you need to set up an "Image File Execution" option in your registry for your executable. It takes five minutes to set up, and it works very well. Here's a Microsoft article where the details are:

    How to: Launch the Debugger Automatically

    0 讨论(0)
  • 2020-11-28 04:10

    You can also try this.

    1. Create your Windows service and install and start…. That is, Windows services must be running in your system.
    2. While your service is running, go to the Debug menu, click on Attach Process (or process in old Visual Studio)
    3. Find your running service, and then make sure the Show process from all users and Show processes in all sessions is selected, if not then select it.

    enter image description here

    1. Click the Attach button
    2. Click OK
    3. Click Close
    4. Set a break point to your desirable location and wait for execute. It will debug automatic whenever your code reaches to that point.
    5. Remember, put your breakpoint at reachable place, if it is onStart(), then stop and start the service again

    (After a lot of googling, I found this in "How to debug the Windows Services in Visual Studio".)

    0 讨论(0)
  • 2020-11-28 04:12

    You should separate out all the code that will do stuff from the service project into a separate project, and then make a test application that you can run and debug normally.

    The service project would be just the shell needed to implement the service part of it.

    0 讨论(0)
  • 2020-11-28 04:12

    Given that ServiceBase.OnStart has protected visibility, I went down the reflection route to achieve the debugging.

    private static void Main(string[] args)
    {
        var serviceBases = new ServiceBase[] {new Service() /* ... */ };
    
    #if DEBUG
        if (Environment.UserInteractive)
        {
            const BindingFlags bindingFlags =
                BindingFlags.Instance | BindingFlags.NonPublic;
    
            foreach (var serviceBase in serviceBases)
            {
                var serviceType = serviceBase.GetType();
                var methodInfo = serviceType.GetMethod("OnStart", bindingFlags);
    
                new Thread(service => methodInfo.Invoke(service, new object[] {args})).Start(serviceBase);
            }
    
            return;
        }
    #endif
    
        ServiceBase.Run(serviceBases);
    }
    

    Note that Thread is, by default, a foreground thread. returning from Main while the faux-service threads are running won't terminate the process.

    0 讨论(0)
提交回复
热议问题