简体   繁体   English

VS C# tcp 服务器/侦听器收到错误 1053 服务在作为 Windows 服务运行时没有及时响应

[英]VS C# tcp server/listener gets error 1053 the service did not respond in a timly fashion when ran as a Windows Service

I have a simple TCP server written in C# with Visual Studio.我有一个简单的 TCP 服务器,使用 Visual Studio 用 C# 编写。 Everything works fine unless ran as a Windows Service in which I get the 1053 error.除非作为 Windows 服务运行,否则一切正常,在该服务中出现 1053 错误。 I narrowed it down to the TCP listener parts because when I comment them out the service starts just fine.我将其缩小到 TCP 侦听器部分,因为当我将它们注释掉时,服务启动得很好。 Here is the code I keep in Service1.cs:这是我保留在 Service1.cs 中的代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;
using System.Net.Sockets;
using System.IO;
using System.Text.RegularExpressions;

namespace Vegas_Remote_Render_Server
{
    public partial class Service1 : ServiceBase
    {
        public Service1()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            var defaultwaittime = "20";
            int defaultport = 1302;
            


            TcpListener listener = new TcpListener(System.Net.IPAddress.Any, defaultport);
            listener.Start();
            while (true)
            {

                Console.WriteLine("Waiting for a connection.");

                TcpClient client = listener.AcceptTcpClient();
                Console.WriteLine("Client accepted.");
                NetworkStream stream = client.GetStream();
                StreamReader sr = new StreamReader(client.GetStream());
                StreamWriter sw = new StreamWriter(client.GetStream());

                try
                {
                    byte[] buffer = new byte[1024];
                    stream.Read(buffer, 0, buffer.Length);
                    int recv = 0;
                    foreach (byte b in buffer)
                    {
                        if (b != 0)
                        {
                            recv++;
                        }
                    }

                    string request = Encoding.UTF8.GetString(buffer, 0, recv);
                    string formattedrequest = Regex.Replace(request, @"^\s*$\n|\r", string.Empty, RegexOptions.Multiline).TrimEnd();

                    Console.WriteLine(request);
                    Console.WriteLine("request received");
                    sw.WriteLine("request received");
                    string[] splitrequest = request.Split('|');
                    var projectpath = "";
                    var parameternumber = 0;

                    var waittime = defaultwaittime;


                    foreach (string value in splitrequest)
                    {
                        parameternumber = parameternumber + 1;
                        if (parameternumber == 1)
                        {
                            projectpath = value;
                        }
                        if (parameternumber == 2)
                        {
                            waittime = value;
                        }


                    }



                    if (formattedrequest != "killrender")
                    {
                        Console.WriteLine(projectpath);
                        Console.WriteLine(waittime);
                        System.Diagnostics.Process.Start(@"render.bat", $"\"{projectpath}\" \"{waittime}\"");
                        sw.Flush();
                    }
                    if (formattedrequest == "killrender")
                    {

                        Console.WriteLine("killing instances");
                        System.Diagnostics.Process.Start(@"kill.bat");
                    }

                }
                catch (Exception e)
                {
                    Console.WriteLine("Something went wrong.");
                    sw.WriteLine(e.ToString());

                }
            }
        }

        protected override void OnStop()
        {
        }
    }
}

To simplify the script, here is only the TCP listener parts:为了简化脚本,这里只列出 TCP 监听部分:

static void Main(string[] args)
    {
        TcpListener listener = new TcpListener(System.Net.IPAddress.Any, defaultport);
        listener.Start();
        while (true)
        {
    TcpClient client = listener.AcceptTcpClient();
    NetworkStream stream = client.GetStream();
            StreamReader sr = new StreamReader(client.GetStream());
            StreamWriter sw = new StreamWriter(client.GetStream());
    }
}

I've also tried many solutions in other posts about this issue with the service.我还在其他帖子中尝试了许多关于该服务问题的解决方案。 This includes exporting the.exe from visual studio as Release instead of Debug, commenting the console.WriteLine commands that I used while debugging, and removing the while(true).这包括将 .exe 从 Visual Studio 导出为 Release 而不是 Debug,注释我在调试时使用的 console.WriteLine 命令,以及删除 while(true)。 My Visual Studio Project also uses the project template "Windows Service C#"我的 Visual Studio 项目也使用项目模板“Windows Service C#”

I have heard that you can make registry edits to give the service more time, but I would rather not use that solution.我听说您可以进行注册表编辑以给服务更多时间,但我宁愿不使用该解决方案。 The service works for the amount of time before it is deemed "unresponsive".该服务在被视为“无响应”之前的一段时间内有效。 I am stuck trying to find what part of the TCP listener is doing this.我一直试图找到 TCP 监听器的哪一部分正在这样做。

Thanks in advance, I'm still kind of a Newbie with C#在此先感谢,我仍然是 C# 的新手

1053 error states: 1053 错误状态:

The service did not respond to the start or control request in a timely fashion.服务未及时响应启动或控制请求。

This error message is the cause of a timeout that occurs after a request was initiated to start a service but it did not respond in the time window.此错误消息是在发起请求以启动服务后发生超时但在 window 时间内没有响应的原因。

Response in your case is exiting OnStart() method.您的情况的响应是退出OnStart()方法。 You use while(true) which is in many cases quite error prone.您使用while(true) ,这在许多情况下很容易出错。 I suspect your service just enters the loop and ever exits because AcceptTcpClient is !blocking!我怀疑您的服务只是进入循环并退出,因为AcceptTcpClient 是!阻塞! operantion : 操作

AcceptTcpClient is a blocking method that returns a TcpClient that you can use to send and receive data. AcceptTcpClient 是一种阻塞方法,它返回可用于发送和接收数据的 TcpClient。 Use the Pending method to determine if connection requests are available in the incoming connection queue if you want to avoid blocking.如果要避免阻塞,请使用 Pending 方法确定传入连接队列中的连接请求是否可用。

You need to move your routine of handling clients out of the OnStart method to do not block it, just move it to the separate thread.您需要将处理客户端的例程移出OnStart方法以不阻塞它,只需将其移至单独的线程即可。

Don't forget to Stop() TcpListener on service stop.不要忘记在服务停止时Stop() TcpListener

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误 1053:安装并运行 WCF 服务时,服务未及时响应启动或控制请求 - Error 1053: The service did not respond to the start or control request in a timely fashion, when intalled and ran a WCF service 1053 windows服务没有及时响应 - 1053 windows service did not respond in timely fashion C#错误1053,服务未及时响应启动或控制请求 - C# Error 1053 the service did not respond to the start or control request in a timely fashion 错误1053:服务未及时响应启动或控制请求 - Error 1053: the service did not respond to the start or control request in a timely fashion Windows服务无法启动'错误1053:服务未及时响应启动或控制请求' - Windows Service won't start 'Error 1053: The service did not respond to the start or control request in timely fashion' 错误 1053 服务没有及时响应启动或控制请求 - Error 1053 the service did not respond to the start or control request in a timely fashion 错误1053:服务没有响应 C#.Net Core 3.1 - Error 1053: Service did not respond C# .Net Core 3.1 使用 TopShelf 创建 Windows 服务时出现“错误 1053 服务没有响应”错误 - "Error 1053 The Service did not respond" error when using TopShelf to create a Windows Service 启动服务:“错误1053:服务未及时响应启动或控制请求” - Starting a service: “Error 1053: The service did not respond to the start or control request in a timely fashion” 错误1053:服务没有及时响应 - Error 1053: Service did not respond in time
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM