1

我开发了一个 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 时,一切都开始运行了。

我怎么解决这个问题?

4

0 回答 0