【问题标题】:error 1503 the service didn't respond to the start or control request in a timely fashion错误1503 服务没有及时响应启动或控制请求
【发布时间】:2018-02-02 05:05:27
【问题描述】:

我已经为我的项目安装了一个Windows服务,当我启动服务时会弹出这个错误。

错误 1503 服务没有及时响应启动或控制请求

但是,当我在 Visual Studio Code 中进行调试时,该项目运行良好,但是当我使用 Visual Studio 2017 按照此tutorial

我尝试了几个解决方案,但错误仍然相同。这是我尝试过的解决方案。

Use CCCleaner to scan and fix issues

Modify ServicesPipeTimeout to 180000

下面的 Logservice 可以将 String 写入文本文件,我在其中分析服务运行到哪个部分失败。该服务只能运行到 LogService("3");然后它在从端口接收字节时失败。

代码如下:

 public Service1()
        {
            InitializeComponent();
            LogService("server");

        try
        {
            LogService("1");
            IPEndPoint anyIP = new IPEndPoint(IPAddress.Any, 0);
            UdpClient udpListener = new UdpClient(514);
            byte[] bReceive; string sReceive; string sourceIP;
            Console.WriteLine("Waiting...");
            /* Main Loop */
            /* Listen for incoming data on udp port 514 (default for SysLog events) */
            while (true)
            {
                LogService("2");
                try
                {
                    LogService("3");
                    bReceive = udpListener.Receive(ref anyIP);
                    LogService("4");
                    /* Convert incoming data from bytes to ASCII */
                    sReceive = Encoding.ASCII.GetString(bReceive);
                    LogService(sReceive);
                    /* Get the IP of the device sending the syslog */
                    sourceIP = anyIP.Address.ToString();
                    LogService("5");
                    LogService(sourceIP);
                    new Thread(new logHandler(sourceIP, sReceive).handleLog).Start();
                    /* Start a new thread to handle received syslog event */

                    LogService(sReceive);
                }
                catch (Exception ex) { Console.WriteLine(ex.ToString()); }
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.ToString());
            throw ex;
        }
    }

不知道是因为代码还是其他原因导致的错误

更新 参考这个post,我尝试关闭防火墙接收所有连接,但错误依旧。

在我将udpListener.Client.Bind(anyIP); 添加到代码中后,有一次我的项目成功地监听了来自服务器的数据,但经过一些修改后,它又无法正常运行。我不确定 Bind() 是否让代码只运行一次。

【问题讨论】:

  • CCCleaner 链接几乎与垃圾邮件接壤
  • 当您在 VS 中使用模板创建新服务时,应自动提供与服务控制管理器 (SCM) 正确交互的代码,并为您提供 StartStop 方法用于创建作为服务实现的一部分所需的任何线程/侦听器/等。上面的代码似乎已经完全删除/忽略了该代码,因此,当然,您没有正确地与 SCM 交互。
  • @Saruman 我已经更新了帖子,看来这对我来说不是解决方案。
  • @Damien_The_Unbeliever 那么这是否意味着我必须将正在运行的模块放入 Start 方法中才能使我的项目正常运行?

标签: c# windows-services visual-studio-code visual-studio-2017


【解决方案1】:

这是我解决错误的方法,请参阅post。无论如何,我并不完全理解这背后的过程,如果有人有一些很好的例子或链接来解释这个解决方案,请不要犹豫,在下面发表评论。

protected override void OnStart(string[] args)
    {
        LogService("Service started");
        NewThread = new Thread(runSysLog);
        NewThread.Start();
    }

    protected override void OnStop()
    {
        LogService("Service stopped");
        StopRequest.Set();
        //NewThread.Join();
    }

public void runSysLog()
    {
        try
        {
            AutoResetEvent StopRequest = new AutoResetEvent(false);
            /* Main Loop */
            while (true)
            {
                if (StopRequest.WaitOne(5000)) return;
                try
                { 
                    //while (udpListener.Available > 0)
                    if (udpListener.Available > 0)
                    {
                        //Some code here  
                    }
                }
                catch (Exception ex)
                {
                    LogService("Whileloop exception: " +ex.ToString());
                    throw ex;
                }
            }
        }
        catch (Exception ex)
        {
            LogService("Run Sys Log Exception: " +ex.ToString());
            throw ex;
        }
    }

【讨论】:

    猜你喜欢
    • 2014-04-08
    • 1970-01-01
    • 2011-03-28
    • 2014-08-05
    • 2011-01-05
    • 2011-12-29
    • 1970-01-01
    • 2015-12-28
    相关资源
    最近更新 更多