Windows service program debugging

Source: Internet
Author: User

From: msdn

1. Because the Service must run from the context of the Service Control Manager rather than from Visual Studio, the debugging service is not similar to debugging Other Visual Studio applications.ProgramType is that simple. To debug the service, you must first start the service and then attachtoprocess a debugger ).

Processes attached to services enable you to debug most servicesCode, But not all; for example, because the service has been started, you cannot use this method to debug the code in the onstart method of the service, or debug the code in the main method used to load the service. (The code that is continuously circulating can be debugged, but cannot be initialized.
Service Running Environment
The code of the thread, and the initialization part of the code in the service thread (the initialization code is usually relatively short, of course, you can set sleep here to increase the execution time );
)

2. One way to solve this problem is to create a temporary service in the service application that helps debugging. You can install both services and then start the "virtual" service to load the service process. After the temporary service starts the process, you can use the Debug menu in Visual Studio to attach it to the service process. After attaching the process, you can set breakpoints and use these breakpoints to debug the code. When you exit the dialog box used to append the process, it is in debugging mode. You can use the Service Control Manager to start, stop, pause, and continue your service, thus hitting the configured breakpoint. After successful debugging, remove this "virtual" service. (I haven't understood the time I first read it. Now I have understood it first, that is, creating and creating a temporary service project in a solution, compile and install the project together with the project to be debugged to the System Service (a service A and B are connected at this time), and then start the temporary service a first, and attach it to the process of service A. At this time, the entire solution of vs enters the debugging state, which is the purpose of the temporary service. in the current status, vs not only monitors the process of service a, but also automatically attaches the process of service B at this time, so we can set breakpoints anywhere, haha
In fact, it is not necessarily a temporary service project, as long as it is a non-stop project that will not end automatically, it should be possible, the purpose is to let vs into the debug state, you can automatically attach process B.

)

3. Dbgbreakpoint or debugbreak (add an int 3 Assembly command to the x86 machine )(It is equivalent to adding a debugging exception to the compiled program. When the Program reaches the breakpoint, it automatically jumps out of the instant debugging window, which is similar to the window when an error occurs in a common program, you only need to select debugging and open the source code vs for debugging. This is a bit like the debugging method used for JS development and dynamic websites. I think this method is the simplest and most feasible. I recommend it.
)

Note: it may be difficult to debug the onstart method because the Windows Service Manager limits all attempts to start the service to 30 seconds. ( This is a fixed time for windows. It is estimated that this restriction can be modified, but I did not find it, some people found out and told me, :--). Now, we 'd better simplify the code for initializing the service runtime environment as much as possible and initialize the actual service-related information, executed in the main function of the Service
. otherwise, you will only have 30 seconds to check the running of the Code. If you are confident that debugging can be completed quickly, you can also write the preceding information. Is my skills sufficient, latency
)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.