Windows服务:无法启动服务。服务进程无法连接到服务控制器

问题描述 投票:1回答:1

我搜索了解决方案,但无法得到它。这是Windows服务的代码。

 protected override void OnStart(string[] args)
    {
        Debugger.Launch();
        try {
           AsynchronousSocketListener.StartListening();



            // Log an event to indicate successful start.

            EventLog.WriteEntry("Successful start.", EventLogEntryType.Information);

        }
        catch(Exception ex)
        {
            // Log the exception.
            EventLog.WriteEntry(ex.Message, EventLogEntryType.Error);


        }
    }

这是类异步套接字侦听器

 static string constr = "Integrated Security=SSPI;Persist Security Info=False;Data Source=WIN-OTVR1M4I567;Initial Catalog=CresijCam";

    //string test = constr;

    // Thread signal.  
    public static  ManualResetEvent allDone = new ManualResetEvent(false);

    private  AsynchronousSocketListener()
    {

    }
    public static void StartListening()
    {
        // Establish the local endpoint for the socket.  
        // The DNS name of the computer  

        IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Any, 1200);

        // Create a TCP/IP socket.  
        Socket listener = new Socket(AddressFamily.InterNetwork,
            SocketType.Stream, ProtocolType.Tcp);

        // Bind the socket to the local endpoint and listen for incoming connections.  
        try
        {
            listener.Bind(localEndPoint);
            listener.Listen(200);

            while (true)
            {
                // Set the event to nonsignaled state.  
                 allDone.Reset();

                // Start an asynchronous socket to listen for connections.  

                listener.BeginAccept(
                    new AsyncCallback(AcceptCallback),
                    listener);

                // Wait until a connection is made before continuing.  
                 allDone.WaitOne();
            }

        }
        catch (Exception e)
        {
            string me = e.Message;

        }

    }

我每次都收到不同的错误消息:

等待来自TCPService服务的事务响应时达到超时(30000毫秒)。

服务无法启动。服务进程无法连接到服务控制器

我不知道从哪里来的错误即将到来。我知道服务尚未运行的一件事。它在这个方法中是startListening()。我使用Debugger.launch()进行了调试。但我没有达到特定的路线。我也认为这与某些地方的TCP有关,但肯定无关紧要。

控制台项目的相同代码处于工作状态。我不知道其他代码放在这里。但如果需要进一步的细节,请告诉我。

c# windows-services
1个回答
1
投票

这个简单的答案是你的AsynchronousSocketListener不是异步或线程或任何类型。基本上你的服务开始是超时,永远不会命中

EventLog.WriteEntry("Successful start.", EventLogEntryType.Information);

因为它基本上是永远阻止的


这个错误说明了一切

等待来自TCPService服务的事务响应时达到超时(30000毫秒)。


OnStart应该只开始工作。这通常意味着产生一个新线程来完成实际工作。简而言之,预计OnStart会迅速完成。

您将需要重构代码以在新线程或任务中运行AsynchronousSocketListener

© www.soinside.com 2019 - 2024. All rights reserved.