How to debug C # developed windows service - data compilation Windows Service II: test the new service, a debugging Windows service Windows service: New Windows Service, install, uninstall Service

 

Original title: How C # Windows service program for debugging

Original Address: https: //jingyan.baidu.com/article/456c463b18e1b00a583144b3.html

The first:

Attach to Process

1, first of service for installation , and then start the service , as shown below:

 

 2, VS project open, select the menu Debug -> Attach to Process option, as shown below:

 

 

3. Select the service process (if you can not find the check  shows all user processes  options) will be able to find the corresponding service exe program, as shown below:

 

 4, attach to the process while OnStart function has already been implemented, so the Onstart not debug. This may be provided by loading debug delay starting the service, only when the timer Timer initialization, a parameter can be passed, as shown below:

 

 

5,

Note: The start time of normal service for about 30 seconds, when the service start time more than 30 seconds will complain!

 So do not do too much in OnStart operation, you can also use this method to start the service delay to prevent timeouts when starting the service.

 

The second:

Modify the source code (recommended)

OnStart method to find the service master class by

protected override void OnStart(string[] args)

{

  //todo something

}

changed to

public void OnStart()

{

   //todo something

}

As shown below:

 

 

Program.cs file comment out the following code:

The ServiceBase [] ServicesToRun; 

ServicesToRun = new new the ServiceBase [] 

{ 

new new Servicel () 

}; 

ServiceBase.Run (ServicesToRun); 

// modified 

Servicel S1 = new new Servicel (); 

s1.OnStart (); 

// as shown below:

 

 In OnStart using the method in F9 break point, and then press F5 can be debugged by tracing the code, as shown below:

 

 Note: When debugging, you need to previously initiated service first stop . Code written after commissioning, remember to modify and restore the first 2 steps of the can.

 

 

--------------------------------------------------

Other Reference article:

Article: Windows Service II: testing the new service, debugging Windows Service

Local content logging referential significance.

 

Article: https: //www.cnblogs.com/dotnet261010/p/6179480.html

Article Title: Windows Service 1: New Windows Service, install, uninstall Service

Good article describes how to create and install the service, and of course unloaded.

Mounting portion referential significance, offers a variety of methods.

 

 

 

 

Guess you like

Origin www.cnblogs.com/Tpf386/p/11459775.html
Recommended