我开发了一个Windows服务。我正在尝试安装它。
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);
}
}
}
安装中的问题本身:
windowsServiceInstaller.InstallService();
服务本身已安装,但当我运行服务时,消息:错误1053:服务未及时响应启动或控制请求
使用以下代码进行安装:
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);
}
}
}
或者:
ManagedInstallerClass.InstallHelper(new[] { Assembly.GetExecutingAssembly().Location });
或者:http://www.verious.com/qa/how-to-install-a-windows-service-programmatically-in-c/
结果是一样的。
但是当我安装installUtill时,一切都会转为运行。
我该如何解决这个问题?