[An update of this post].
After a few problems with using the ManagedInstallerClass (passing arguments in), and noticing that the MSDN docs say it’s not meant to be used in any case, I moved to using this sort of code instead:
using System;
using System.Configuration.Install;
using System.ServiceProcess;
using System.Threading;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Collections.Generic;
namespace SimpleWindowsServiceManager
{
class Program
{
static void Main(string[] args)
{
string fullpath = args[0];
string[] arguments = args[1].Split(' ');
string servicename = Path.GetFileNameWithoutExtension(Path.GetFileName(args[0]));
foreach (string arg in arguments)
{
if (arg.Contains("name"))
{
string[] bits = arg.Split(new char[] { '=' });
servicename = bits[1];
}
}
#region Test to see if it's a service
try
{
AssemblyInstaller.CheckIfInstallable(fullpath);
}
catch (Exception ex)
{
Console.WriteLine("Assembly's not installable");
return;
}
#endregion
#region install
Assembly assembly = null;
try
{
assembly = Assembly.LoadFrom(fullpath); // LoadFrom probes path for dependencies -- LoadFile does not.
}
catch (Exception ex)
{
Console.WriteLine("Couldn't load assembly: " + ex.Message);
return;
}
AssemblyInstaller installer = new AssemblyInstaller(assembly, null);
Dictionary state = new Dictionary();
try
{
installer.Install(state);
installer.Rollback(state);
}
catch (Exception ex)
{
Console.WriteLine("Trouble pre-installing assembly");
return;
}
#endregion
installer = new AssemblyInstaller(assembly, arguments);
state = new Dictionary();
#region Install service
try
{
installer.Install(state);
}
catch (InvalidOperationException iex)
{
Console.WriteLine("Install not applicable for this assembly:\n" + iex.Message);
return;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
#endregion
Console.WriteLine("Getting service controller for " + servicename);
ServiceController controller = new ServiceController(servicename);
#region Start service
try
{
Console.WriteLine("Starting service");
controller.Start();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
#endregion
Thread.Sleep(10000);
#region Stop service
try
{
Console.WriteLine("Stopping service");
controller.Stop();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
#endregion
Thread.Sleep(10000);
#region Uninstall service
try
{
installer.Uninstall(state);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
#endregion
}
}
}


