Menu ▼


Changing program.cs file to debug the windows service

By changing the code temporarily (we have to roll back the changes to deploy the service, other wise service will not work) in program.cs main function we can debug the code.

  • Change the code in Service1.cs
    void serviceTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
    {
        logServiceExecution();
    }

    public void logServiceExecution()
    {
        if (isScheduleMatched())
            File.AppendAllText(fileLocation, "Service running at : " + DateTime.Now.ToString() + Environment.NewLine);
    }
  • Change the code in Program.cs
    static void Main()
    {
        //ServiceBase[] ServicesToRun;
        //ServicesToRun = new ServiceBase[] 
        //{ 
        //    new Service1() 
        //};
        //ServiceBase.Run(ServicesToRun);

        Service1 obj = new Service1();
        obj.logServiceExecution();
    }
  • Put a break point in Program.cs or logServiceExecution() method in Service.cs, then rebuild your solution and run. Now you are able to debug the service successfully.





comments powered by Disqus


Search dotnetlearners.com