An Easy Approach to test Windows Service


Recently I involved in a project where I had to write a windows service to interact with SharePoint portal for generating alerts and various processes. The service is called “AlertingEngineHostingService”. For testing the service I found that the following code snippet (reflection approach) quite handy as it allows you to rapidly perform fixes and tests without having to deploy as usual.

using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Reflection;

namespace TestWindowsService
{
    class Program
    {
        static void Main(string[] args)
        {
            ServiceBase[] servicesToRun;

            servicesToRun = new ServiceBase[]  
                {  
                        new AlertingEngineHostingService()  
                };

            if (Environment.UserInteractive)
            {
                Type type = typeof(ServiceBase);
                BindingFlags flags = BindingFlags.Instance | BindingFlags.NonPublic;
                MethodInfo method = type.GetMethod("OnStart", flags);

                foreach (ServiceBase service in servicesToRun)
                {
                    method.Invoke(service, new object[] { args });
                }

                Console.WriteLine("Press any key to exit");
                Console.Read();

                MethodInfo stopMethod = type.GetMethod("OnStop", flags);

                foreach (ServiceBase service in servicesToRun)
                {
                    stopMethod.Invoke(service, new object[] { args });
                }
            }
        }
    }
}

Jan 09, 2012 Update: ————————————-

Another alternative that could be used is, have the following precompile statement in the Program.cs file:

#if DEBUG
             AlertingEngineHostingService srv = null;
            try
            {
                srv = new AlertingEngineHostingService();
                srv.StartAsProcess();
            }
            catch (System.Exception)
            {
                if (srv != null)
                    srv.WriteLogEntry();

            }
#else
            ServiceBase[] ServicesToRun;
            ServicesToRun = new ServiceBase[] 
            { 
                new AlertingEngineHostingService() 
            };
            ServiceBase.Run(ServicesToRun);
#endif

And have the following debug helper method in the service class (in my case it was – AlertingEngineHostingService.cs):

public void StartAsProcess()
{           // your code here
            // ....
            OnEventTrigger(null, null);
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.