Question Windows Service does not appear under services.msc is called

aindrea

Member
Joined
Jun 5, 2018
Messages
21
Programming Experience
1-3
I have developed a Windows Service, and it will be incumbent on me to develop it further. Haplessly, a great obstacle for this is the fact that it does not appear in the service list after services.msc is called even though installation works quite finde with InstallUtils.exe.

The main-class looks like this:
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceProcess;
using System.Text;

namespace WindowsService1
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        static void Main()
        {
            ServiceBase[] ServicesToRun;
            ServicesToRun = new ServiceBase[] 
            { 
                new Scheduler() 
            };
            ServiceBase.Run(ServicesToRun);
        }
    }
}
The service has a scheduler like this:

Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Timers;

namespace WindowsService1
{
    public partial class Scheduler : ServiceBase
    {
        private Timer timer1 = null;

        public Scheduler()
        {
            InitializeComponent();
        }

        private void timer1_Tick(object sender,ElapsedEventArgs e)
        {
            Library.WriteErrorLog("Timer ticked.");
        }

        protected override void OnStart(string[] args)
        {
            timer1 = new Timer();
            this.timer1.Interval = 30000;
            this.timer1.Elapsed += new System.Timers.ElapsedEventHandler(this.timer1_Tick);
        }

        protected override void OnStop()
        {
            timer1.Enabled = false;
            Library.WriteErrorLog("WindowsService1 stopped.");
        }
    }
}
...and there is a library class there, too, which simply writes some text into log files (this is what it is supposed to do for the time being):

Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace WindowsService1
{
    public static class Library
    {
        public static void WriteErrorLog(Exception ex)
        {
            StreamWriter sw = null;
            try 
            {
                sw = new StreamWriter(AppDomain.CurrentDomain.BaseDirectory + "\\LogFile.txt",true);
                sw.WriteLine(DateTime.Now.ToString()+": "+ex.Source.ToString().Trim()+"; "+ex.Message.ToString().Trim());
                sw.Flush();
                sw.Close();
            }catch{}

        }

        public static void WriteErrorLog(string Message) 
        {
            StreamWriter sw = null;
            try
            {
                sw = new StreamWriter(AppDomain.CurrentDomain.BaseDirectory + "\\LogFile.txt", true);
                sw.WriteLine(DateTime.Now.ToString() + ": " + Message);
                sw.Flush();
                sw.Close();
            }
            catch { }
        }

    }
}
The project installer file has two sets of properties: serviceInstaller1:
GenerateMember: True
Modifiers: Private
DelayedAutoStart: False
Parent: ProjectInstaller
ServiceName: Service1
ServiceDependsOn: String[]Array
StartType: Manual

... as well as serviceProseccinstaller1:

GenerateMember: True
Modifiers: Private
Account: LocalSystem
Parent: ProjectInstaller

What hack is needed to make the service appear where it should after installation?

Btw.: Is it allowed to upload zip-files with project data instead of pasting the classes into the forum editor window?
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
2,471
Location
Sydney, Australia
Programming Experience
10+
Is it allowed to upload zip-files with project data instead of pasting the classes into the forum editor window?
If you click the Go Advanced button then the advanced editor will allow you to upload attachments, including ZIP files. You should start by deleting the 'bin' and 'obj' folders from all projects because they contain binaries, which are not allowed in attachments. You can then zip the solution folder and attach the result. That said, attaching whole projects should never be your first choice. Posting relevant code directly should always be the first option.
 

aindrea

Member
Joined
Jun 5, 2018
Messages
21
Programming Experience
1-3
After all, my service is visible now. It seems that I used the wrong installation tool. Instead of install utils I did it like this:

sc.exe create WindowsService1 binPath="<my path to>\WindowsService1.exe"

Now it is clearly visible.
 
Top Bottom