In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly shows you "C# how to achieve Windows service testing and debugging", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "C# how to achieve Windows service testing and debugging" this article.
1. Test Windows service
In order for the Windows service program to work properly, we need to create an entry point for the program for it as you would for a normal 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 the Windows service program starts. The service does not really start running until the object's OnStart () method is called. If you want to start multiple services at the same time in a Windows service program, you can simply define an instance object of multiple subclasses of the ServiceBase class in the Main () function by creating an array object of the ServiceBase class.
Namespace WindowsServiceDemo {the main entry point of the static class Program {/ application. / / static void Main () {ServiceBase [] ServicesToRun; ServicesToRun = new ServiceBase [] {/ / Service 1 new MyService (), / / Service 2 new Service1 ()}; ServiceBase.Run (ServicesToRun) }
Because the Windows service does not have direct user interaction, the status of the service must be known by logging. To test the windows service, you can do this by rewriting the method in the service and logging in the method.
1. Create a new Common class with a WriteLog logging method in it. The log path is written in the configuration file to achieve the flexibility of the project.
Namespace WindowsServiceDemo {public class Common {/ logging / public static void WriteLog (string strInfo) {string strPath=ConfigurationManager.AppSettings ["FilePath"] Using (StreamWriter sw = new StreamWriter (strPath, true)) {sw.WriteLine (strInfo + ", current time:" + DateTime.Now.ToString ()); sw.Close ();}}
2. Right-click on the Service1 design interface-- > View the code, open the Service1 code, rewrite the OnStart (), OnStop (), OnPause (), OnContinue () methods respectively, and call the WriteLog method of the Common class in the method to record the running status of the service.
Namespace WindowsServiceDemo {public partial class MyService: ServiceBase {public MyService () {InitializeComponent () Code executed at service startup / protected override void OnStart (string [] args) {try {Common.WriteLog ("service startup") } catch (Exception ex) {Common.WriteLog ("Service startup error:" + ex.Message) }} / protected override void OnStop () {try {Common.WriteLog ("Service stop") } catch (Exception ex) {Common.WriteLog ("Service stop error:" + ex.Message) } / protected override void OnPause () {try {Common.WriteLog ("service pauses") } catch (Exception ex) {Common.WriteLog ("Service pause error:" + ex.Message) }} / protected override void OnContinue () {try {Common.WriteLog ("Service recovery") } catch (Exception ex) {Common.WriteLog ("Service recovery error:" + ex.Message);}
3. Start, pause, resume and stop the service in the service control manager to view the generated log:
The running status of the service is recorded correctly in the log, which proves that there is no problem with the service.
Debug Windows services
To debug a Windows service, you can attach the service to the process.
1. Select Debug-> attach to the process in the menu bar options
2. In the attach to process interface, select the appropriate service process, and click attach.
Note: to attach a service to a process, you must ensure that the service is in a startup state, otherwise the process of the service cannot be seen in the process.
These are all the contents of the article "how to implement Windows Service testing and debugging in C #". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.