Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
312 views
in Technique[技术] by (71.8m points)

.net - Debugging windows services

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 studio IDE. When I try to attach the process from Debug tab in the IDE, the windows service process is shown in the list, but it is not highlighted to be attached. Is there any other main process that I should attach to debug service application. Any relevant information posted is appreciated.

Thanks.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...