Debugging windows services

后端 未结 5 846
抹茶落季
抹茶落季 2020-12-10 20:55

I have created a windows service and installed it manually. Later started the service from Services tool. Now I want to dubug the windows service application from Visual st

相关标签:
5条回答
  • 2020-12-10 21:31

    This doesn't answer the exact question, but for what it's worth, I have found the easiest way to develop and debug a windows service is to put all of the logic into a class library and then call the logic from either a windows service (in production) or from a regular windows form (during development). The windows form would have a Start and Stop button which would simulate the start and stop behavior of the service.

    To make it easy to switch between the two modes, I just use a command line parameter and handle that in the Main method like this:

    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        private static void Main(string[] args)
        {
            if (args.Length > 0 && args[0] == "/form")
            {
                var form = new MainForm();
                Application.Run(form);
                return;
            }
    
            var servicesToRun = new ServiceBase[]
            {
                new BackgroundService()
            };
    
            ServiceBase.Run(servicesToRun);
        }
    }
    

    Then in the "Command line arguments" field of the Visual Studio project properties, you can just add the "/form" parameter, and it will always pop up the form when debugging locally. This way you don't have to worry about attaching to a process or anything like that. You just click Debug as usual, and you're good to go.

    0 讨论(0)
  • 2020-12-10 21:32

    The easiest way I have found to do this is to put a call to System.Diagnostics.Debugger.Break() in the Main() function, the constructor, or the OnStart() callback of your service. When you start the service from the SCM, you will be presented with a dialog that allows you to start a new instance of Visual Studio or attach to an existing instance. The service will stop at your programmatic breakpoint. You can debug from there.

    Of course, all of this is predicated on the fact that you are actually able to debug a Windows service. The fact that you see the service in the "attach to" list, but it isn't highlighted indicates that you do not have sufficient privilege on your machine to debug it. Get with your system administrator to figure out what permissions are required. When I've done this, I've usually had to log off and log back on after the permissions were changed to get it to work.

    EDIT:

    I believe this echoes the infomation at the link @divo provided, but here is how I was able to get permission to debug my Windows service (straight from my system admin).

    1. Go to Start|Control Panel|Administrative Tools|Local Security Policy.
    2. Expand the local policies and click on User Rights Assignments.
    3. Look at the "Debug Programs" setting. Your username or primary group should be listed in this setting.

    Note that if your system is a member of a domain, then this setting will be controlled by group policy and cannot be changed from the local machine level.

    0 讨论(0)
  • 2020-12-10 21:45

    Well, this is a pretty old question, but nobody's said it, so I figured I'd at least mention what I just encountered. Make sure in your "Attach to Process" dialog, you have "Show processes from all users" selected. Until I did that, I could only see my .vshost.exe file in the list of processes.

    0 讨论(0)
  • 2020-12-10 21:52

    Debugging services requires certain permissions:

    Setting Debug Permissions - Debugging a System Service

    If you can't get the necessary permission another option to debug your service is via System.Diagnostics.Trace. You can monitor the messages being traced by your service with Sysinternal's DbgView.

    0 讨论(0)
  • 2020-12-10 21:53

    If you have JIT debugging turned on, DebugBreak() at startup will allow you to launch into the debugger.

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