首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Windows服务:无法启动服务。服务进程无法连接到服务控制器

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

Stack Overflow用户
提问于 2018-09-04 15:05:16
回答 1查看 2.9K关注 0票数 1

我寻找解决方案,但找不到。这是windows服务的代码。

代码语言:javascript
复制
 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);


        }
    }

下面是AsynchronousSocketListner的

代码语言:javascript
复制
 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有关,但没有什么可以确定的。

对于控制台项目,相同的代码处于工作状态。我不知道在这里还能放什么代码。但请让我知道,如果需要进一步的细节。

EN

回答 1

Stack Overflow用户

发布于 2018-09-04 15:14:15

这个简单的答案是你的AsynchronousSocketListener不是异步的,也不是线程化的,或者任何类似的东西。从本质上讲,您的服务启动超时,并且永远不会命中

代码语言:javascript
复制
EventLog.WriteEntry("Successful start.", EventLogEntryType.Information);

因为它本质上是永远阻塞的

这个错误说明了一切

等待来自TCPService服务的事务响应时,

超时(30000毫秒)。

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

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52160622

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档