OnStart method for debugging Windows services

Source: Internet
Author: User

OnStart method for debugging Windows Services

1. Add the following code within the OnStart method

            #if DEBUG
            System.Diagnostics.Debugger.Launch ();
            #endif

The complete code is as follows

        protected override void OnStart (string[] args)
        {
            #if DEBUG
            System.Diagnostics.Debugger.Launch ();
            #endif
            //add method code
        }

2. Strike a breakpoint within the OnStart method

3. Start the service and hit the breakpoint within the OnStart method.

Note: Debugger.launch () is in static void Main (), as follows

        static void Main ()
        {
            #if DEBUG
                        System.Diagnostics.Debugger.Launch ();
            #endif

                        servicebase[] ServicesToRun;
                        ServicesToRun = new servicebase[] 
            { 
                new Serviceemail () 

            };
                        Servicebase.run (ServicesToRun);
	 }

Note: Because of permissions issues, you may not be able to select the Visual Studio you are opening, and a new Visual Studio will open, the new interface cannot find the code layer and cannot be interrupted, and the solution is to run Visual Studio as an administrator, Then start the service, and in the pop-up select the Visual Studio page, select the Visual Studio you are debugging, and never create a new one.



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.