0

I have installed a Windows Service for my project, this error pop out when I start the service.

error 1503 the service didn't respond to the start or control request in a timely fashion

However, this project works fine when I debug in Visual Studio Code but when I use Visual Studio 2017 to create and start the service by following this tutorial and

Few solution I tried but the error still the same. Here are the solution I have tried.

Use CCCleaner to scan and fix issues

Modify ServicesPipeTimeout to 180000

The Logservice below can write the String into a text file, where I analyze the service run up until which part then fails. The service only able to run until LogService("3"); then it fails at receive the bytes from port.

Here is the code:

 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;
        }
    }

I'm not sure if the error is occur because of the codes or other reasons

Update Refer to this post, I tried to turn off the firewall to receive all the connection, but the error still remain the same.

There was once my project successfully listen the data from the server after I add udpListener.Client.Bind(anyIP); into the code, but then after some modification it is not functioning again. I'm not sure is the Bind() make the code works even just for once.

JJ___
  • 187
  • 2
  • 4
  • 14
  • The CCCleaner link nearly borders on spam – TheGeneral Feb 02 '18 at 05:16
  • Possible duplicate of [UDPClient Receive method not working in a service](https://stackoverflow.com/questions/2298815/udpclient-receive-method-not-working-in-a-service) – TheGeneral Feb 02 '18 at 05:18
  • 1
    When you create a new service using a template in VS, there should be code automatically provided that correctly interacts with the Service Control Manager (SCM) and provides `Start` and `Stop` methods for you to use to create whatever threads/listeners/etc you need to as part of your service implementation. The above code seems to have completely removed/ignored that code and so, of course, you're not interacting with the SCM correctly. – Damien_The_Unbeliever Feb 02 '18 at 06:43
  • @Saruman I have updated the post, seems like it's not a solution for me. – JJ___ Feb 03 '18 at 06:02
  • @Damien_The_Unbeliever So is that mean I have to put the running modules into `Start` method in order to make my project run properly? – JJ___ Feb 03 '18 at 06:05
  • You have to kick off activity from the start method - but you cannot use it to run any long-running code itself. It's when your Start method *returns* that the service is considered to be started. Start from the template, consider what I've said, don't write code like in your post. The alternative is to use some helper library (such as Topshelf) which gives a different abstraction over creating services. – Damien_The_Unbeliever Feb 03 '18 at 17:44
  • @Damien_The_Unbeliever Thanks for your advise and I have put my code in the `start` method, after some modification it works partially fine. May I know what do you mean by long-running code? Is that mean the while loop is not suitable to be used in `start` method? – JJ___ Feb 04 '18 at 16:21

1 Answers1

0

This is the way I solve my error refer to this post. Anyway, I'm not completely understand the process behind this, if anyone have some good example or link in explaining this solution please don't hesitate to comment below.

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;
        }
    }
JJ___
  • 187
  • 2
  • 4
  • 14