0

I am using this solution from Matt Davis:How to make a .NET Windows Service start right after the installation? (second answer) to programatically install windows service. But I don't know how to use it. Where should his code which he has put inside Main go? Inside Service Main, or it is main of console application?

How to start/install the service and how to pass that -install parameters?

I tried this:

sc start myservice -install

but doesn't work :(

It tells me:

Specified service doesn't exist as installed service

I would really appreciate help because I am struggling with these services

Community
  • 1
  • 1
  • Should you not simply call `myservice -install`? This should install your service. Afterwards you could call `sc start myservice`. – Oliver Jun 12 '15 at 10:13
  • @Oliver: I don't know, you say just `myservicename.exe -install`? - when I do that nothing gets displayed on console and I don't see my service installed in Services. Here is my full code, could you please have a look at it and maybe spot some problem? http://hostcode.sourceforge.net/view/3063 –  Jun 12 '15 at 10:17
  • there were small changes I added to Matt's code like: add `typeof(Service1)` - as commented in his code, and added servicename –  Jun 12 '15 at 10:19
  • Maybe take a look at https://groups.google.com/forum/#!topic/microsoft.public.dotnet.languages.csharp/TUXp6lRxy6Q – Oliver Jun 12 '15 at 10:29
  • @Oliver: That doesn't seem to help me - what specifically do you suggest me to look at? I find it very weird that its author Matt Davis didn't put step by step guide how to do this –  Jun 12 '15 at 10:36
  • @Oliver: I think this is right `myservice -install` because I printed something to log file inside main and that above command made entry in the log.. but even though when I run it like that, it doesn't get installed –  Jun 12 '15 at 10:37

1 Answers1

0

I don't know how you created your windows service but I started with a console application when I do that. You will have Installer.cs and Program.cs classes. Installer class will make necessary operations before and after installation and Program class will perform the consistent operations.

Here an Installer.cs which you can use:

using System;
using System.Collections.Generic;
using System.ServiceProcess;
using System.Reflection;
using System.Configuration.Install;

namespace MyWindowsService
{
    [System.ComponentModel.RunInstallerAttribute(true)]
    public class MyServiceInstaller : Installer
    {
        ServiceInstaller _serviceInstaller = new ServiceInstaller();
        ServiceProcessInstaller _processInstaller = new ServiceProcessInstaller();
        string _serviceName = "MyWindowsService";
        string _displayName = "MyWindowsService";
        string _description = "My Windows Service Application";

        public MyServiceInstaller()
        {
            this.BeforeInstall += new InstallEventHandler(ProjectInstaller_BeforeInstall);

            _processInstaller.Account = ServiceAccount.LocalSystem;

            _serviceInstaller.StartType = ServiceStartMode.Automatic;
            _serviceInstaller.Description = _description;
            _serviceInstaller.ServiceName = _serviceName;
            _serviceInstaller.DisplayName = _displayName;

            Installers.Add(_serviceInstaller);
            Installers.Add(_processInstaller);
        }

        /// <summary>
        /// This function is called after installation completed
        /// </summary>
        /// <param name="savedState"></param>
        protected override void OnCommitted(System.Collections.IDictionary savedState)
        {
            ServiceController sc = new ServiceController(_serviceName);

            // Run Windows service if it is not running
            if (sc.Status != ServiceControllerStatus.Running)
            {
                sc.Start();
            }
            else
            {
                // Restart windows service if it is already running
                RestartService(10000);
            }
        }

        private void RestartService(int timeoutMiliseconds)
        {
            ServiceController service = new ServiceController(_serviceName);

            int millisec1 = Environment.TickCount;
            TimeSpan timeout = TimeSpan.FromMilliseconds(timeoutMiliseconds);

            service.Stop();
            service.WaitForStatus(ServiceControllerStatus.Stopped, timeout);

            int millisec2 = Environment.TickCount;
            timeout = TimeSpan.FromMilliseconds(timeoutMiliseconds - (millisec2 - millisec1));

            service.Start();
            service.WaitForStatus(ServiceControllerStatus.Running, timeout);
        }

        void ProjectInstaller_BeforeInstall(object sender, System.Configuration.Install.InstallEventArgs e)
        {
            List<ServiceController> services = new List<ServiceController>(ServiceController.GetServices());

            foreach (ServiceController s in services)
            {
                if (s.ServiceName == this._serviceInstaller.ServiceName)
                {
                    ServiceInstaller ServiceInstallerObj = new ServiceInstaller();
                    ServiceInstallerObj.Context = new System.Configuration.Install.InstallContext();
                    ServiceInstallerObj.Context = Context;
                    ServiceInstallerObj.ServiceName = _serviceName;
                    ServiceInstallerObj.Uninstall(null);
                }
            }
        }
    }
}

And this may be used as Program.cs:

namespace MyWindowsService
{
    class Program : ServiceBase
    {
        static void Main()
        {
            System.ServiceProcess.ServiceBase.Run(new Program());
        }

        // This function is called after windows service starts running
        protected override void OnStart(string[] args)
        {
            // Do something

            base.OnStart(args);
        }

        // This function is called when windows service stopping
        protected override void OnStop()
        {
            // Do something

            base.OnStop();
        }
    }
}

This example uninstalls the application if it was installed previously, installs it again and automatically starts your Windows service after installation.

Also here is a link which will help you.

Orkun Bekar
  • 1,447
  • 1
  • 15
  • 36