When we creating a windows service, we need to test it before installing.Window service is not directly run through Visual Studio.There are many other method to debug the window service. I explained here one of the method to debug the window service.
This is the easier way for testing or debugging windows services by changing the code only in Program.cs file.
Here, below is the code:
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
static void Main()
{
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
new ServiceClassName() // ServiceClassName where OnStart and OnStop method defined
};
ServiceBase.Run(ServicesToRun);
#if (!DEBUG)
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
new ServiceName()
};
ServiceBase.Run(ServicesToRun);
#else
ClassName obj = new ClassName();
obj.MethodName(); //MethodName that you want to call for debugging
#endif
}
}
Hope this code will help you. Thanks
0 Comment(s)