Debug the Windows Service onstart event.

Source: Internet
Author: User
Debugging Windows service is a common issue.

The common solution is to select attach to process in the debugger after the service is run.

However, this method also has some limitations, such asCodeIt is basically difficult to debug. when attach arrives at our service, this part of code has been executed. so someone suggested that you could write another project to call this onstart method, or move the code in the onstart method to another project for testing. however, these methods are not debugged in the form of a Windows service, and cannot reflect the running status of the service in the most real way (such as permission issues and other environmental issues ).

My approach is to add "debugger. launch () "call. When the service is running here, a dialog box for selecting the debugger will pop up and paused at the current position. in this way, we can manually start the debugger in the code.

The sample code is as follows:

 
UsingSystem. diagnostics;
 
 
 
Public Partial ClassMyservice: servicebase {
PublicMyservice (){
 
Initializecomponent ();
 
}
 
 
 
Protected Override VoidOnstart (String[] ARGs ){
 
# IfDebug
 
Debugger. Launch ();// Launches and attaches a debugger to the process.
 
# Endif
 
// Todo: add your initialize code here.
 
}
 
 
 
Protected Override VoidOnstop (){
}
 
}
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.