Windows Service debugging Summary (with Demo) and summary demo

Source: Internet
Author: User

Windows Service debugging Summary (with Demo) and summary demo

This article is copyrighted by mephisto and the blog Park. You are welcome to repost it, but you must keep this statement and provide the original article link. Thank you for your cooperation.

Reading directory
  • Introduction
  • Build Environment
  • Debugging method
  • Download Demo

This article is copyrighted by mephisto and the blog Park. You are welcome to repost it, but you must keep this statement and provide the original article link. Thank you for your cooperation.

 

Introduction

Sometimes it is inevitable to build some Windows Services. Now that code is written, debugging is required. Since there are not many people involved in this task and the debugging methods are not complete for everyone, let's summarize the debugging methods here.

Build Environment 1: create a Window Service

File> new project> Windows service.

Run the command directly and the prompt is as follows:

Let's add a service installer, some references, and others according to his meaning. The result is as follows:

  

In this way, the environment is basically set up, and then the service is installed. Run bin \ Debug \ Install \ install. bat.

Debugging method 1: common debugging

"The service must be run from the context of the Service Control Manager rather than in Visual Studio. Therefore, the debugging service is not as simple as debugging Other Visual Studio Application types. To debug a service, you must start the service and then attach the debugger to the process in which the service is running. Then you can use all the standard debugging functions of Visual Studio to debug your application ".

So we start the service, and then attach the service process through vs, and then we can debug it. As follows:

 

Ii. Special debugging

If we don't want to create a service, we want to debug the Code. In fact, we can use other alternative methods, but we only need to modify the code.

We can find the Program entry: Program. cs.

The original code is as follows:

1 static class Program 2 {3 /// <summary> 4 // main entry point of the application. 5 // </summary> 6 static void Main () 7 {8 ServiceBase [] ServicesToRun; 9 ServicesToRun = new ServiceBase [] 10 {11 new ServiceDebug () 12 }; 13 ServiceBase. run (ServicesToRun); 14} 15}

The modified code is as follows:

 1         protected override void OnStart(string[] args) 2         { 3             Timer timer = new Timer(); 4             timer.Interval = 1000; 5             timer.Elapsed += new ElapsedEventHandler(timer_Elapsed); 6             timer.Start(); 7  8         } 9 10         private void timer_Elapsed(object sender, ElapsedEventArgs e)11         {12 13         }14 15         protected override void OnStop()16         {17         }18 19         public void Test(string[] args)20         {21             OnStart(args);22         }
1 static class Program 2 {3 /// <summary> 4 // main entry point of the application. 5 /// </summary> 6 static void Main () 7 {8 ServiceDebug service = new ServiceDebug (); 9 service. test (null); 10 11 while (true) 12 {13 System. threading. thread. sleep (1000); 14} 15 return; 16 17 ServiceBase [] ServicesToRun; 18 ServicesToRun = new ServiceBase [] 19 {20 new ServiceDebug () 21}; 22 ServiceBase. run (ServicesToRun); 23} 24}

In this way, we can perform debugging.

II. General OnStart debugging

Sometimes we want to debug the Onstart method normally. However, after the service is started, this method is already running, So how should we debug it.

We can add Debugger. Launch (); In Front Of The Onstart method body to enjoy debugging. As follows:

 1         protected override void OnStart(string[] args) 2         { 3             Debugger.Launch(); 4  5             Timer timer = new Timer(); 6             timer.Interval = 1000; 7             timer.Elapsed += new ElapsedEventHandler(timer_Elapsed); 8             timer.Start(); 9 10         }

After the service is started, the following message is displayed: select the corresponding solution.

 

Download Demo

Source code download

This article is copyrighted by mephisto and the blog Park. You are welcome to repost it, but you must keep this statement and provide the original article link. Thank you for your cooperation.

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.