Windows Service two: Testing a new service, debugging a Windows service

Source: Internet
Author: User
Tags try catch

First, testing Windows services

In order for the Windows Service program to function properly, we need to create a program entry point for it, like creating a generic application. Like other applications, Windows services do this in the main () function of Program.cs. First we create an instance of the Windows service in the main () function, which should be an object of a subclass of the ServiceBase class, and then we call a run () method defined by the base class ServiceBase class. However, calling the run () method does not mean that you have started the Windows service program and must wait until the OnStart () method of the object is called to actually start running. If you want to start multiple services at the same time in a Windows service program, you can simply define an instance object for the subclasses of multiple ServiceBase classes in the main () function by creating an array object of the ServiceBase class.

1 namespaceWindowsservicedemo2 {3     Static class Program4     {5         /// <summary>6         ///The main entry point for the application. 7         /// </summary>8         Static voidMain ()9         {Ten servicebase[] ServicesToRun; OneServicesToRun =Newservicebase[] A             {  -                 //Service 1 -                 NewMyService (), the                 //Service 2 -                 NewService1 () -             }; - Servicebase.run (servicestorun); +         } -     } +}

Because the Windows service does not have direct user interaction, the status of the service must be known through logging. To test the Windows service, you can do this by rewriting the methods inside the service and logging the log in the method.

1, the new common class, the class has a Writelog log method. The log path is written in the configuration file, enabling the project to be flexible.

1 namespaceWindowsservicedemo2 {3      Public classCommon4     {5         /// <summary>6         ///Record Log7         /// </summary>8         /// <param name= "Strinfo" ></param>9          Public Static voidWritelog (stringstrinfo)Ten         { One             stringstrpath=configurationmanager.appsettings["FilePath"]; A             using(StreamWriter SW =NewStreamWriter (strpath,true)) -             { -Sw. WriteLine (Strinfo +", Current time:"+DateTime.Now.ToString ()); the SW. Close (); -             } -              -         } +     } -}
View Code

2. In the Service1 design field, right--View the code, open the Service1 code, rewrite OnStart (), OnStop (), OnPause (), OnContinue () method, respectively, Call the common class's Writelog method inside the method to log the service's running state.

1 namespaceWindowsservicedemo2 {3      Public Partial classMyservice:servicebase4     {5          PublicMyService ()6         {7 InitializeComponent ();8         }9 Ten         /// <summary> One         ///code to execute when the service starts A         /// </summary> -         /// <param name= "args" ></param> -         protected Override voidOnStart (string[] args) the         { -             Try -             { -Common.writelog ("service startup"); +             } -             Catch(Exception ex) +             { ACommon.writelog ("Service startup error:"+Ex. Message); at             } -         } -  -         /// <summary> -         ///code to execute when the service is stopped -         /// </summary> in         protected Override voidOnStop () -         { to             Try +             { -Common.writelog ("Service stopped"); the             } *             Catch(Exception ex) $             {Panax Notoginseng                  -Common.writelog ("Service Stop error:"+Ex. Message); the             } +         } A  the         /// <summary> +         ///code executed when the service is paused -         /// </summary> $         protected Override voidOnPause () $         { -             Try -             { theCommon.writelog ("Service suspended"); -             }Wuyi             Catch(Exception ex) the             { -                  WuCommon.writelog ("Service Pause Error:"+Ex. Message); -             } About         } $  -         /// <summary> -         ///code that executes when the service resumes -         /// </summary> A         protected Override voidOnContinue () +         { the             Try -             { $Common.writelog ("Service Recovery"); the             } the             Catch(Exception ex) the             { the                  -Common.writelog ("Service Recovery error:"+Ex. Message); in             } the         } the  About     } the}

3, in the Service Control Manager to start, pause, resume, stop the service, view the generated log:

Log inside the correct record of the operation of the service status, to prove that the service is not a problem.

Second, debugging Windows services

Debugging a Windows service can take the method of attaching a service to a process.

1. Select Debug-Attach to process in menu bar options

2, in the attach to the process interface, select the appropriate service process, click Attach.
Note: To attach a service to a process, you must ensure that the service is in the boot state, otherwise the process of the service cannot be seen inside the process.

Three, Summary:
1, Windows service debugging cannot be directly F5, can be debugged by attaching to process mode (Debugging premise: Start the service, run as Administrator vs)
2, Windows services because there is no direct user interaction, the status of the service must be known through the log, appropriate to join the try Catch
3, all possible changes in the content should not be written dead, as far as possible through the configuration file to achieve, this is an important indicator of project flexibility
4, Windows services are used for timing operations, large data volume operation, monitoring operations and other aspects

Windows Service two: Testing a new service, debugging a Windows service

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.