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

90

我已经创建并安装了一项服务多次。最初它运行良好,但在服务代码进行更改后,当我在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);
    }
}

这是我的主方法代码

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

我也尝试了以下步骤:

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

我曾使用以下命令进行安装和卸载:

installutil AutoSMS.exe

installutil /u AutoSMS.exe

Windows事件日志显示了什么?或者它显示“错误1053”?这可能是一个权限问题。尝试将服务作为“本地系统”用户运行。 - Silvermind
1
将来使用System.Diagnostics.Debugger.Launch()在Main()方法中可能很有用,这样您就可以使用调试器。 - user420667
32个回答

0
我为了解决这个错误而苦思冥想。 如果你在代码中进行调试,可能会导致这个错误。
static void Main()
{
 #if DEBUG
            MailService service = new MailService();
             service.Ondebug();
 #else
             ServiceBase[] ServicesToRun;
             ServicesToRun = new ServiceBase[]
             {
                 new MailService()
             };
             ServiceBase.Run(ServicesToRun);
 #endif
         }
     }

在代码中清除了if,elseendif后,错误就没有再出现了...希望能有所帮助...

static void Main()
{

    ServiceBase[] ServicesToRun;
    ServicesToRun = new ServiceBase[]
    {
        new MailService()
    };
    ServiceBase.Run(ServicesToRun);

}

-1

此错误可能由各种原因引起。在运行服务时添加 try / catch 以确定原因。

        try
            {
                ServiceBase[] ServicesToRun;
                ServicesToRun = new ServiceBase[] 
                { 
                    new Service1() 
                };
                <span class="skimlinks-unlinked">ServiceBase.Run(ServicesToRun</span>);
            }
            catch (Exception ex)
            {
                EventLog.WriteEntry("Application", ex.ToString(), <span class="skimlinks-unlinked">EventLogEntryType.Error</span>);
            }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接