错误1053服务未及时响应启动或控制请求

Nee*_*rma 48 .net c# timer

我已经创建并安装了几次服务.最初它工作正常,但在服务代码中的一些更改后,当我在Services.msc中重新启动服务时,它开始给出错误:

错误1053:服务未及时响应启动或控制请求

码:

public partial class AutoSMS : ServiceBase
{
    public AutoSMS()
    {
        InitializeComponent();
        eventLog1.Clear();

        if (!System.Diagnostics.EventLog.SourceExists("MySource"))
        {
            System.Diagnostics.EventLog.CreateEventSource(
                "MySource", "MyNewLog");
        }
        eventLog1.Source = "MySource";
        eventLog1.Log = "MyNewLog";

        Timer checkForTime = new Timer(5000);
        checkForTime.Elapsed += new ElapsedEventHandler(checkForTime_Elapsed);
        checkForTime.Enabled = true;

    }

    protected override void OnStart(string[] args)
    {
        eventLog1.WriteEntry("In OnStart");
    }

    protected override void OnStop()
    {
        eventLog1.WriteEntry("In onStop.");
    }


    void checkForTime_Elapsed(object sender, ElapsedEventArgs e)
    {
        string Time = "15:05:00";
        DateTime dateTime = DateTime.ParseExact(Time, "HH:mm:ss",
                                        CultureInfo.InvariantCulture);

        if (DateTime.Now == dateTime) ;
            eventLog1.WriteEntry(Time);
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我的主要方法代码

static void Main()
{
    ServiceBase[] ServicesToRun;
    ServicesToRun = new ServiceBase[] 
    { 
        new AutoSMS() 
    };
    ServiceBase.Run(ServicesToRun);
}
Run Code Online (Sandbox Code Playgroud)

我还尝试了以下步骤:

  • 转到"开始">"运行">,然后键入regedit
  • 导航到:HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control
  • 选择控制文件夹后,右键单击右侧的窗格,然后 - 选择新的DWORD值
  • 将新DWORD命名为:ServicesPipeTimeout
  • 右键单击"ServicesPipeTimeout",然后单击"修改"
  • 单击"十进制",键入"180000",然后单击"确定"
  • 重新启动计算机

我曾经使用以下命令安装和卸载它:

installutil AutoSMS.exe

installutil /u AutoSMS.exe
Run Code Online (Sandbox Code Playgroud)

Pra*_*ngh 26

就我而言,我在调试模式下发布服务.

解决方案是:

  • 将解决方案更改为发布模式
  • 使用命令卸载旧服务 InstallUtil -u WindowsServiceName.exe
  • 再次安装服务 InstallUtil -i WindowsServiceName.exe

它之后运作得很好.

  • 男人我在这上面摸了一会儿.检查配置文件和事件日志结束,直到我读到这个答案并踢我自己忘记翻转构建版本.这对我有用! (3认同)

Jam*_*phy 25

正如其他人所指出的那样,这个错误可能有很多原因.但是希望这会帮助别人,我将分享我们案件中发生的事情.对我们来说,我们的服务已升级到.NET 4.5,但服务器没有安装.NET 4.5.

  • 众多是正确的.如果您的服务中存在错误,则可能会出现此错误.所以与您的代码而不是框架相关. (4认同)

小智 10

我遇到了同样的问题,并不确定如何解决它.是的,这是因为服务引发了异常,但您可以遵循一些通用准则来纠正此问题:

  • 检查您是否已编写正确的代码以启动该服务: ServiceBase[] ServicesToRun; ServicesToRun = new ServiceBase[] { new WinsowsServiceToRun() }; ServiceBase.Run(ServicesToRun);
  • 您需要确保在WinsowsServiceToRun类中运行某种无限循环

  • 最后,可能有一些代码没有记录任何内容并突然关闭程序(我就是这种情况),在这种情况下,你将不得不按照旧的调试学校来编写一行来源(文本/分贝/哪里).我面临的问题是,由于运行该服务的帐户不是"管理员",因此代码刚刚下降并且没有记录任何异常,以防它尝试写入"Windows事件日志",即使代码是用于记录异常.记录到偶数日志实际上不需要管理员权限,但需要定义源.如果事件的源尚未在系统中定义,并且服务尝试在没有管理员权限的情况下首次记录它,则它将失败.要解决此问题,请按照以下步骤

    1. 使用admin权限打开命令提示符
    2. 粘贴命令: eventcreate /ID 1 /L APPLICATION /T INFORMATION /SO <<Source>> /D "<<SourceUsingWhichToWrite>>"
    3. 按enter键
    4. 现在开始服务


JS5*_*JS5 10

花了一些时间在问题上,尝试了无效的解决方案后,我遇到了这个博客。建议将服务初始化代码包装在try / catch块中,像这样,并添加EventLog

using System;
using System.Diagnostics;
using System.ServiceProcess;

namespace WindowsService
{
    static class Program
    {
        static void Main()
        {
            try
            {
                ServiceBase[] ServicesToRun;
                ServicesToRun = new ServiceBase[] 
                { 
                    new Service1() 
                };
                ServiceBase.Run(ServicesToRun);
            }
            catch (Exception ex)
            {
                EventLog.WriteEntry("Application", ex.ToString(), EventLogEntryType.Error);
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

然后,卸载旧服务,并通过这些修改重新部署该服务。启动服务并签出事件查看器/应用程序日志。您将看到真正的问题是什么,这是超时的根本原因。

  • 好主意,但就我而言,它没有记录任何内容..仍然是这个烦人的消息,并且没有任何提示! (2认同)

dev*_*der 9

我刚刚在.Net 4.5中尝试了本地代码,服务正常启动和停止.我怀疑你的问题可能在于创建EventLog源码.

方法:

EventLog.SourceExists("MySource")
Run Code Online (Sandbox Code Playgroud)

要求运行代码的用户必须是管理员,根据此处的文档:

http://msdn.microsoft.com/en-us/library/x7y6sy21(v=vs.110).aspx

检查该服务是否以具有管理员权限的用户身份运行.


小智 8

如果您想将 .NET core 3.1 可执行文件注册为 Windows 服务,请确保在 3.1.7 或更高版本中添加 nuget 包 Microsoft.Extension.Hosting.WindowsServices 并初始化 hostBuilder,如以下示例所示:

using Microsoft.Extensions.Hosting;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] arguments)
        {
            IHostBuilder hostBuilder = Host.CreateDefaultBuilder(arguments);
            hostBuilder.UseWindowsService();
            hostBuilder.Build().Run();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

现在您可以安装可执行文件并将其作为 Windows 服务启动:

sc.exe create ConsoleApp1 binPath= "<BIN_PATH>\ConsoleApp1.exe"
Run Code Online (Sandbox Code Playgroud)


Alb*_*rez 7

这是因为 Microsoft Windows 服务控制,它有时会控制服务的状态。如果服务没有在 30 秒内发送响应,那么您将收到此错误。

可以修改注册表,让服务有更多时间响应

Go to Start > Run > and type regedit
Navigate to: HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control
With the control folder selected, right click in the pane on the right and select new DWORD Value
Name the new DWORD: ServicesPipeTimeout 
Right-click ServicesPipeTimeout, and then click Modify
Click Decimal, type '180000', and then click OK
Restart the computer
Run Code Online (Sandbox Code Playgroud)

或者确保在那一刻没有另一个进程与服务交谈,也许存在我不知道的冲突


ahs*_*ant 5

我遇到了完全相同的问题,我所做的就是在编译 dll 时将调试模式更改为发布模式。这解决了我的问题,如何/为什么?我不知道我已经问过一个问题


Jac*_*cob 5

在这个问题上花了太多时间之后。EventLog尽管使用得当,但我发现了所有混乱的原因。

无论谁解决此问题,我都建议您摆脱EventLog。使用更好的工具,例如“ log4net ”。


Unh*_*ean 5

在我今天早上遇到的情况中,罪魁祸首是格式错误的配置文件。配置文件有一个关闭注释标签,但没有开放注释标签。因此,请仔细检查您的配置文件是否有错误。