3. Calling the OnStart() method from another Main() function ( DO NOT need to INSTALL the service)
Saving the best for last! :)
In this method you don't need to install the service. Just use the technique given below.
-- Add a new class to your project. For eg :
DebugService.cs
-- Then add the following code in that class:
class DebugService
{
static void Main()
{
#if DEBUG
ServiceDemo obj = new ServiceDemo(); // Create object of your Service's class.
obj.OnStart(); // Call the public OnStart() method, as mentioned in the next step.
System.Threading. Thread.Sleep(System.Threading.Timeout.Infinite);
#endif
}
} -- Now get back to your windows service class. Then add a public OnStart() method which will call the protected OnStart() method..
public void OnStart() // This is another Onstart method which is public in nature.
{
this.OnStart(null); // Here it calls the protected OnStart() method of your service.
} -- Right click on your project in the Solution Explorer then go to the
Properties of it, there you can find the '
Startup Object' field. Now set the
Startup Object as
DebugService ( the new class where you wrote the Main() function)
-- Set a breakpoint and start debugging directly from Visual Studio by pressing F5.
Note: The OnStart method has a time limit of 30 seconds on attempts to start a service.