Wosh
Wosh

Reputation: 1625

How to install VS2012 Windows Service without using installutil.exe

I am trying to install a windows service without using the installutil. An understandable and straightforward way to do this which I found is to use:

ManagedInstallerClass.InstallHelper

So I end up with the following Program.cs:

 static class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    static void Main(string[] args)
    {
        if (args.Length >0)
        {
            string parameter = string.Concat(args);
            switch (parameter)
            {
                case "--install":
                    ManagedInstallerClass.InstallHelper(new string[] { Assembly.GetExecutingAssembly().Location });
                    break;
                case "--uninstall":
                    ManagedInstallerClass.InstallHelper(new string[] { "/u", Assembly.GetExecutingAssembly().Location });
                    break;
            }
        }
        else
        {
            ServiceBase[] ServicesToRun;
            ServicesToRun = new ServiceBase[] 
            { 
                new PicknikService() 
            };
           ServiceBase.Run(ServicesToRun);
        }
    }
}

After I Build the service and execute MyService.exe --install I get the following:

Cannot start service from the command line or debugger. A winwows Service must first be installed(using installutil.exe) and then started with the ServerExplorer, Windows Services Afministrative tool or the NET START command.

Any thoughts?

Upvotes: 0

Views: 4219

Answers (2)

Matt Davis
Matt Davis

Reputation: 46034

The MSDN entry for ManagedInstallerClass.InstallHelper says the following:

This API supports the .NET Framework infrastructure and is not intended to be used directly from your code.

While I have no doubt the solution offered at the link you provided will do the trick, it makes heavy use of P/Invoke calls. There's nothing wrong with that, but I prefer a fully C#-based solution.

I have such a solution in a step-by-step tutorial here for creating a Windows service that will install and uninstall itself from the command line without requiring InstallUtil.exe. It was written for Visual Studio 2008, but it still works as I've since written a service that does the same thing in Visual Studio 2012.

Upvotes: 0

Wosh
Wosh

Reputation: 1625

The only possible way seems to be the following

Upvotes: 1

Related Questions