How do I know if my Windows service is running?

I created a Windows service to populate the database with an email inbox every 5 minutes.

I used the class inside my Windows service, the class received my letters and wrote them to my database, the class was tested and works.

All I need to do is use a timer and call the class every 5 minutes, but I have no idea what is happening, since I cannot even check my Windows service.

Please someone tell me what to do to check if there is a way to check, or just blink luck and pray that it works lol.

Also, do you need to uninstall and reinstall every time you want to test the service, or is there an upgrade option? Please answer this, I'm really interested even in the fact that this is not my main question.

This is my windows service if you can point out any errors that would be awesome as I cannot check them. I think my timer might be wrong if someone can look at it?

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Diagnostics; using System.Linq; using System.ServiceProcess; using System.Text; using System.Timers; namespace EmailWindowsService { public partial class MyEmailService : ServiceBase { private Timer scheduleTimer1 = null; private DateTime lastRun; private bool flag; public MyEmailService() { InitializeComponent(); if (!System.Diagnostics.EventLog.SourceExists("MySource")) { System.Diagnostics.EventLog.CreateEventSource( "MySource", "MyNewLog"); } eventLogEmail.Source = "MySource"; eventLogEmail.Log = "MyNewLog"; scheduleTimer1 = new Timer(); scheduleTimer1.Interval = 5 * 60 * 1000; scheduleTimer1.Elapsed += new ElapsedEventHandler(scheduleTimer_Elapsed); } protected override void OnStart(string[] args) { flag = true; lastRun = DateTime.Now; scheduleTimer.Start(); eventLogEmail.WriteEntry("Started"); } protected override void OnStop() { scheduleTimer.Stop(); eventLogEmail.WriteEntry("Stopped"); } protected override void OnPause() { scheduleTimer.Stop(); eventLogEmail.WriteEntry("Paused"); } protected override void OnContinue() { scheduleTimer.Start(); ; eventLogEmail.WriteEntry("Continuing"); } protected override void OnShutdown() { scheduleTimer.Stop(); eventLogEmail.WriteEntry("ShutDowned"); } protected void scheduleTimer_Elapsed(object sender, ElapsedEventArgs e) { RetriveEmailClass Emails = new RetriveEmailClass(); if (flag == true) { eventLogEmail.WriteEntry("In getting Email Method"); Emails.ServiceEmailMethod(); lastRun = DateTime.Now; flag = false; } else if (flag == false) { if (lastRun.Date < DateTime.Now.Date) { Emails.ServiceEmailMethod(); eventLogEmail.WriteEntry("In getting Email Method"); } } } } } 
+4
source share
5 answers

Of course you can check it out. All you need is

  • start the service
  • note that it calls the expected call after 5 minutes.
  • (note that it calls the expected call every 5 minutes a couple more times)

You can check this manually or (preferably) create / use an automatic test harness that allows you to check repeatedly and reliably, as many times as you want. This is possible even with a simple batch file.

To find that the timer is working correctly, you can check its log file. Of course, this also helps if you create a method of the called class, rather than hardcoding. That way, you can run your automated tests using a dummy working class that doesn't flood your inbox :-)

To make it even more verifiable, you can also extract the synchronization logic from your service class so that it can be run from a regular application. Then you can test it even easier, even using a unit test framework like NUnit. This allows for more rigorous testing using different time intervals, etc. And the service class itself becomes an almost empty shell, whose only task is to start and call other classes. If you have verified that all classes containing real program logic (i.e., all code that may fail) will be checked by one and will work perfectly, you can have a lot more confidence that your entire application, when it is integrated from its small parts, it works correctly.

Update

Looking through your code, it seems that you are not initializing flag anywhere, so its default value will be false . You must initialize it to true in the constructor, otherwise your email retriever will never be called, even if the timer fires properly.

To set the interval to 1 minute, I would prefer

 scheduleTimer1.Interval = 1 * 60 * 1000; 
+2
source
James Michael Hare wrote on his blog about a really good template / framework that he made, which greatly facilitates the development (and debugging) of Windows Services: C # Toolbar: debugged, self-stopping Windows service template (1 of 2)

It provides you with all the basics you need to get started quickly. And most importantly, it gives you a great way to debug your service, as if it were a regular console application. I could also mention that it provides out of the box functionality for installing (and uninstalling) your service. The second part of the message can be found at this link .

I have used it myself a couple of times and can really recommend it.

+3
source

Restore logic in another class.

Write a simple console application calling this class

Test it like a regular application.

As soon as it works autonomously, it should work as a service. Beware of permissions and registration of services, there are a couple of problems (for example, having a sys user or a desktop session).

It is good practice to use system logs (for example, those that you can check with eventvwr)

+1
source

1. attach this line to the place you want to split, then you can debug your service.

 System.Diagnostics.Debugger.Break(); 

or

2. Try connecting to your service from the process explorer, then you can also debug your service.

or

3. Use a log file to log what your service is doing.

0
source

You can connect the debugger to start the service instance from Visual Studio. Click "Debug" in the main menu "Attach to the process ...", select your service process from the list and click "Attach".

If you need to debug the launch of your service, you need to use System.Diagnostics.Debugger.Break() .

0
source

Source: https://habr.com/ru/post/1411242/


All Articles