4

I have written a windows service which in turn calls a web service. When I run the windows service from a test app., it works perfectly. However when I install the service and then start it, it stops almost immediately. The only two entries I see in the log are Constructor and Thread Started. Not sure what is wrong.

public partial class WindowsService : ServiceBase
{
    public LogManager.LogFile _log;
    public Thread m_thread;
    protected TimeSpan m_delay;

    CommonFunctions _cf = new CommonFunctions();
    DBFunctions _db = new DBFunctions();

    public WindowsService()
    {
        InitializeComponent();
        _log = new LogManager.LogFile(@"c:\test\servicelog.txt", true, true);
        _log.WriteToLog("Constructor", LogLevel.Level0);
    }


    protected override void OnStart(string[] args)
    {

       m_delay = new TimeSpan(0,0,300);
       base.OnStart(args);

        try
        {
            m_thread = new System.Threading.Thread(Execute);
            m_thread.Start();
            _log.WriteToLog("Thread Started", LogLevel.Level0);
        }
        catch (Exception ex)
        { _log.WriteToLog(ex.Message, LogLevel.Level0); }

    }

  public void Execute()
    {
        _log.WriteToLog("Begin Execute...", LogLevel.Level0);

        try
        {

            ProcessNewLMSUsers();

         }
        catch (Exception ex)
        {
             _log.WriteToLog(ex.Message.ToString());
        }

     }

    private void ProcessNewLMSUsers()
    {
        try
        {
            _log.WriteToLog("Begin: Processing new LMS Users", LogLevel.Level1);

            // Check for new users in the LMS.
            string callErrorText = "";
            bool userAdded = false;

            LMSWS.SSO lms = _cf.GetLMSSSOWS(); **// this is a web service**
            lms.Timeout = 99999;


          }

             REST OF THE CODE.................
     }
stackuser
  • 672
  • 1
  • 9
  • 21
  • Have you checked the system logs? Take a look at the accepted answer to this question for an overview. http://stackoverflow.com/questions/1067531/are-there-any-log-file-about-windows-services-status – immutabl Nov 21 '12 at 16:02

1 Answers1

0

I can't see there is anything wrong with your code. but you can try to put a "Thread.Sleep(20000); " code at the begining of OnStart method. e.g.

protected override void OnStart(string[] args)
{
   Thread.Sleep(20000); 

   m_delay = new TimeSpan(0,0,300); //set a break-point here
   base.OnStart(args);

    try
    {
        m_thread = new System.Threading.Thread(Execute);
        m_thread.Start();
        _log.WriteToLog("Thread Started", LogLevel.Level0);
    }
    catch (Exception ex)
    { _log.WriteToLog(ex.Message, LogLevel.Level0); }

}

and once you start this service program in Windows Service, then you have to quickly attach you source code to the service program. In visual studio, it's menu "Debug" -> "Attach to Process...". then you can set break-point in your source code anywhere to check what's going wrong.

Yang You
  • 2,618
  • 1
  • 25
  • 32
  • 1
    Replace the call to `System.Threading.Thread.Sleep(20000)` with a call to `System.Diagnostics.Debugger.Launch()`. This should cause a dialog to pop up when you start the service, allowing you to open a debug session of Visual Studio with the code paused at the invocation. I've been told this might not work on Windows 8, FYI. – Matt Davis Nov 23 '12 at 19:43
  • Thanks! and sorry for the delay in responding. I was able to debug the code using Syste.Diagnostics.Debugger.Launch() and found that the code was working perfectly, but was not writing into the log file. I changed the value to logLevel.Level0 and got it to work. But now I have another problem. The windows service is installed and running. However the Execute() is executed only once. How do I get it executed at regular intervals ? Thanks Again! – stackuser Nov 27 '12 at 15:05
  • Usually i put a 'while' loop in the thread method ('Execute' here). – Yang You Nov 27 '13 at 00:45