0

I changed my console application to use as windows service using ServiceBase. I installed it using the following command. But I didn't find the service in services. I checked the log it says

"No public installers with the RunInstallerAttribute.Yes attribute could be found in the C:\Test\MyService.exe assembly"

How do I create installer for Console application? Please let me know.

"C:\Windows\Microsoft.NET\Framework\v4.0.30319\installutil.exe" "c:\MyService.exe"


    using System.ServiceProcess;
    public static class Program
    {
        public static bool Cancelled { get; set; }


        #region Nested classes to support running as service
        public const string ServiceName = "MyService";

        public class Service : ServiceBase
        {
            public Service()
            {
                ServiceName = Program.ServiceName;
            }

            protected override void OnStart(string[] args)
            {
                Program.Start(args);
            }

            protected override void OnStop()
            {
                Program.Stop();
            }
        }
        #endregion


        static void Main(string[] args)
        {
            if (!Environment.UserInteractive)
                // running as service
                using (var service = new Service())
                    ServiceBase.Run(service);
            else
            {
                // running as console app
                Start(args);

                Console.WriteLine("Press any key to stop...");
                Console.ReadKey(true);

                Stop();
            }

        }

        private static void Start(string[] args)
        {
            // onstart code here
            try
            {
              SaveMessage();
            }
            catch (Exception e)
            {
        LogError();
            }
        }

        private static void Stop()
        {
            // onstop code here
            DisposeAll();
        }
}
nav100
  • 2,923
  • 19
  • 51
  • 89
  • 1
    See [this question](https://stackoverflow.com/questions/7922105/install-windows-service-created-in-visual-studio) - though personally I would use [Topshelf](https://www.nuget.org/packages/Topshelf/) - _"By referencing Topshelf, your console application **becomes** a service installer with a comprehensive set of command-line options for installing, configuring, and running your application as a service."_ – stuartd Aug 07 '17 at 15:03

2 Answers2

1

I believe you need to extend from the System.Configuration.Install.Installer

Something like

public class ServiceRegister: Installer 
{

    public ServiceRegister() 
    {
        ServiceProcessInstaller serviceProcessInstaller =
                        new ServiceProcessInstaller();
        ServiceInstaller serviceInstaller = new ServiceInstaller();

        #if RUNUNDERSYSTEM
        serviceProcessInstaller.Account = ServiceAccount.LocalSystem;
        #else
        // should prompt for user on install
        processInstaller.Account = ServiceAccount.User;
        processInstaller.Username = null;
        processInstaller.Password = null;
        #endif

         serviceInstaller.DisplayName = "SomeName";
        serviceInstaller.StartType = ServiceStartMode.Manual;
        serviceInstaller.ServiceName = "SomeName";


        this.Installers.Add(serviceProcessInstaller);
        this.Installers.Add(serviceInstaller);

    }

}
Svet Angelov
  • 799
  • 8
  • 19
0

My favourite way to install a service is to use the SC command line utilities.

Official docs

Full syntax (to scare everybody !)

sc [<ServerName>] create [<ServiceName>] [type= {own | share | kernel | filesys | rec | interact type= {own | share}}] [start= {boot | system | auto | demand | disabled}] [error= {normal | severe | critical | ignore}] [binpath= <BinaryPathName>] [group= <LoadOrderGroup>] [tag= {yes | no}] [depend= <dependencies>] [obj= {<AccountName> | <ObjectName>}] [displayname= <DisplayName>] [password= <Password>]

In simple terms,

SC create YourServiceName start= auto binPath= "path/to/your/exe" DisplayName= "Your Display Name"

To remove a service, the command is

SC delete YourServiceName

The above commands need to run from a command prompt with Admin rights. Please note the space after the "=" sign is important.

Related SO post

Subbu
  • 2,130
  • 1
  • 19
  • 28