I have developed a Windows Service. I'm trying to install it.
static void Main(string[] args)
{
args = new[] { "-i" };
if (args.Length == 0)
{
ServiceBase.Run(new ServiceBase[] { new Service() });
}
else if (args.Length == 1)
{
var windowsServiceInstaller =
new WindowsServiceInstaller("AutocompleteTemplateService", typeof (Service));
try
{
switch (args[0])
{
case "-i":
Console.WriteLine("Install service...");
windowsServiceInstaller.InstallService();
Console.WriteLine("Start service");
windowsServiceInstaller.StartService();
Console.WriteLine("Сервис запущен...");
break;
case "-u":
Console.WriteLine("Stop service...");
windowsServiceInstaller.StopService();
Console.WriteLine("Delete service...");
windowsServiceInstaller.UninstallService();
Console.WriteLine("Сервис удалён...");
break;
default:
Console.WriteLine("Не известный параметр");
break;
}
}
catch (Exception exception)
{
Console.WriteLine(exception.Message);
}
}
}
The problem itself in the installation:
windowsServiceInstaller.InstallService();
The service itself is installed, but when I run service the message: Error 1053: The service did not respond to the start or control request in a timely fashion
To install using the following code:
public void InstallService()
{
if (IsInstalled()) return;
using (var installer = GetInstaller())
{
IDictionary state = new Hashtable();
try
{
installer.Install(null);
installer.Commit(null);
}
catch
{
installer.Rollback(state);
}
}
}
Or:
ManagedInstallerClass.InstallHelper(new[] { Assembly.GetExecutingAssembly().Location });
Or: http://www.verious.com/qa/how-to-install-a-windows-service-programmatically-in-c/
The result is the same.
But when I install a installUtill, it everything turns to run.
How can I solve this problem?