Error in windows service

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mrk98
    New Member
    • Oct 2009
    • 3

    Error in windows service

    Hi,

    I am getting the following error when I try to start the windows service I have written:

    ReadOutlookServ ice on local computer started and then stopped.Some services stop automatically if they have no work to do, for example the Performance logs and alerts service.

    My Service.cs contains the following:
    Code:
     protected override void OnStart(string[] args)
            {
                // TODO: Add code here to start your service.
                eventLog1.WriteEntry("ReadOutLookService OnStart");
               
                tm.Enabled = true;
                //tm.Start;
                tm.Elapsed += new ElapsedEventHandler(Timer_Elapsed);
            }
    
           protected void Timer_Elapsed(object sender,
             System.Timers.ElapsedEventArgs e)
            {
                try
                {
                    //eventLog1.WriteEntry("Timer started");
                    _Application olApp = new ApplicationClass();
                    _NameSpace olNs = olApp.GetNamespace("MAPI");
                    MAPIFolder oInbox = olNs.GetDefaultFolder(OlDefaultFolders.olFolderInbox);
                    Items oItems = oInbox.Items;
                    for (int i = 1; i <= oItems.Count; i++)
                    {
                        _MailItem oMail = (_MailItem)oItems.Find("DTE Time Reminder");
                        oMail.Display(false);
                    }
    
                }
                catch (System.Exception ex)
                {
                    throw (ex);
                }
            }
    How do I debug the service ? The service gets installed, but I am not able to run it, since it starts and stops immediately.

    Thanks in advance,
    mrk
    Last edited by PRR; Oct 2 '09, 11:30 AM. Reason: [Code] tags added. Please post code in [Code] [/Code] tags.
  • PRR
    Recognized Expert Contributor
    • Dec 2007
    • 750

    #2
    Hi,
    1. Try and log the error in a text file.
    Code:
    try{
    
    }
    catch(Exception)
    {
    // code to log text file
    }
    2. Check the service account. It should have permission to write to event logs and file system.

    Comment

    • mrk98
      New Member
      • Oct 2009
      • 3

      #3
      Hi,
      Can you elaborate how I can give permission to the ServiceAccount to write to eventlog or file system ? I have used LocalSystem account.
      Thanks,
      mrk

      Comment

      • mrk98
        New Member
        • Oct 2009
        • 3

        #4
        Can anybody give some pointers ? I just want to create and run a simple windows service..

        Comment

        • PRR
          Recognized Expert Contributor
          • Dec 2007
          • 750

          #5
          Hi,
          Run the service under "LocalSystem"(f or testing, later on maybe you can change it to some other account). This is the highest account( most privileged). Try and run the code in a console application, see if you find any bugs. If it runs fine, then maybe its a rights issue.
          Creating Winsows service (look into ServiceAccount) .

          Comment

          • Plater
            Recognized Expert Expert
            • Apr 2007
            • 7872

            #6
            Generally when your service produces that "error" about starting and stopping, it means it had no work to do. Which would lead me to believe there is trouble in your OnStart call, in that the service control doesn't believe you are trying to do anything. Your timer_elapsed function might not be recognized as a true thread that does work?

            Comment

            Working...