4

I have built a windows service to populate a database with my email inbox every 5 minutes.

I used a class inside my windows service the class gets my emails and writes them to my database, the class has been tested and works.

All i need the windows service to do is use a timer and call the class every 5 minutes, but i have no idea whats going on as i cant even test my windows service.

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

Also do u have to uninstall and re-install every time you want to test the service or is there an update service option? Please answer this i'm really interested even tho its not my main question.

This is my windows service, if u can point out any errors that would be amazing since i cant test for them. I think my timer might be wrong if some one could 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");
                }
            }
        }
        }


    }
Péter Török
  • 114,404
  • 31
  • 268
  • 329
Pomster
  • 14,567
  • 55
  • 128
  • 204
  • 2
    I used this walk though to create the stub of my service : http://msdn.microsoft.com/en-us/library/zt39148a.aspx – Pomster May 08 '12 at 07:17
  • 2
    Add some debug code that will write some 'logging' or run every 1 minute if a specific file on disk exists. That should help you guarantee that the service is actually running and working. Add extended exception logging too so that if it goes wrong there is a correct emergency scenario ready. – CodingBarfield May 08 '12 at 07:19

5 Answers5

3

Surely you can test it. All you need is

  1. start up the service
  2. observe that it triggers the expected call after 5 minutes
  3. (observe that it triggers the expected call every 5 minutes for a couple more times)

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

To detect that the timer works correctly, you can inspect its log file. It also helps of course if you make the called class method configurable instead of hardcoding it. So you can run your automated tests using a dummy worker class which does not flood your inbox :-)

To make it even more testable, you can extract the timing logic from your service class too, so that it becomes runnable from a regular application. Then you can test it even easier, even using a unit test framework such as NUnit. This allows you to do more thorough testing, using different timing intervals etc. And the service class itself becomes an almost empty shell whose only job is to launch and call the other classes. If you have verified that all the classes containing real program logic (i.e. all the code which can fail) is unit tested and works fine, you can have much greater confidence in that your whole app, when integrated from its smaller parts, works correctly too.

Update

Looking through your code, it seems that you don't initialize flag anywhere, so its default value will be false. You should initialize it to true in the constructor, otherwise your email retriever won't ever get called even if the timer fires properly.

To set the interval to 1 minute, my first guess would be

scheduleTimer1.Interval = 1 * 60 * 1000;
Community
  • 1
  • 1
Péter Török
  • 114,404
  • 31
  • 268
  • 329
  • 2
    So basically Server Explorer and check the event log? i only have a Started. Could you tel me why my code is above. – Pomster May 08 '12 at 07:20
  • 2
    Thanks this is a great answer :) i think its the timer giving me problems, how can i move it?, so my service can become and empty shell just with the triggers onStart and OnContinue ect. Whats the math to set the timer to 1 min?? – Pomster May 08 '12 at 07:32
  • 2
    @Pomster, please see my update, including the possible reason why your service is not working as expected :-) – Péter Török May 08 '12 at 07:46
3

James Michael Hare has on his blog written about a really nice template/framework he has made, making it lot easier to develop (and debug) Windows Services: C# Toolbox: A Debuggable, Self-Installing Windows Service Template (1 of 2)

It provides you with all the basics you need to quickly get started. And best of all, it give you a really nice way to debug your service as if it was a regular console application. I could also mention that it provides out of the box functionality to install (and uninstall) your service. Part two of the post can be found at this link.

I've used this myself a couple of times, and can really recommend it.

Julian
  • 20,008
  • 17
  • 77
  • 108
  • 2
    Ok thanks man il check it out :), So the service needs to be uninstalled and installed every time to test, there is no update setup or something Like that? – Pomster May 08 '12 at 07:25
  • 2
    @Pomster: no, you do _not_ have to install/uninstall your service every time to test it. The framework makes it so you can run your service in debug mode from within Visual Studio (like normal: _Debug>Start Debugging (F5)_). The install/uninstall feature is for when you're actually going to _install_ your service in your production/test environment. – Julian May 08 '12 at 07:28
  • 2
    Oh, and would the debugger still return event_log results? – Pomster May 08 '12 at 07:35
  • 1
    @Pomster: what do you mean by "return event_log results"? When using this framework and starting the service from within Visual Studio, it will run as a _normal_ console application. Meaning you'll be able to step through the code in debug mode to inspect values, flow logic, you can write debug info with ´Console.WriteLine´ and so on. Please read through the links I provided (both part 1 and 2), and you'll find that everything is explained pretty good there. – Julian May 08 '12 at 07:45
  • 1
    I just read though them thanks there Great really explain how to turn it into a console app. thanks been very helpful :) – Pomster May 08 '12 at 07:53
1

Refactor you logic in another class.

Write a simple console application invoking this class

Test it like a normal application.

Once it runs standalone, it should run as a service. Beware on permissions and service registration, there are a couple of issues there (like having a sys user, or a desktop session).

A good practice is to use system logs (e.g. the ones you can inspect with eventvwr)

BigMike
  • 6,683
  • 1
  • 23
  • 24
  • 2
    Ok but i have my Functionality a class its just my timer and the service onstart and oncontinue so on. – Pomster May 08 '12 at 07:23
  • 1
    wrap in another class event the Timing stuffs. You have to check if that works. – BigMike May 08 '12 at 07:26
  • 2
    Ok cool, yes i think its my timer giving me trouble, iv never worked with them befor this, and never worked with windows services :\ sigh – Pomster May 08 '12 at 07:34
0

1.add this line to the place you want to break, then you can debug your service.

System.Diagnostics.Debugger.Break();

or

2.try to attach to your service progress from process explorer, then you can also debug your service.

or

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

neohope
  • 1,822
  • 15
  • 29
0

You can attach a debugger to you running service instance from Visual Studio. Click "Debug" in the main menu, "Attach to Process...", select your service process from the list and click "Attach".

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

Marek Dzikiewicz
  • 2,844
  • 1
  • 22
  • 24