简体   繁体   English

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

[英]Error 1053 the service did not respond to the start or control request

I've written a Windows Service in C# that basically checks my db every minute for orders, generates a PDF from these orders, and emails it.我在 C# 中编写了一个 Windows 服务,它基本上每分钟检查我的数据库以获取订单,从这些订单生成 PDF,然后通过电子邮件发送给它。

The logic works perfectly in my tests etc..该逻辑在我的测试等中完美运行。

When i create the service, and install it using the setup project, when I go to start the service in the services mmc, I get:当我创建服务并使用安装项目安装它时,当我 go 在服务 mmc 中启动服务时,我得到:

error 1053 the service did not respond to the start or control request in a timely fashion错误1053 服务没有及时响应启动或控制请求

My OnStart method looks like this:我的 OnStart 方法如下所示:

protected override void OnStart(string[] args)
{
    //writeToWindowsEventLog("Service started", EventLogEntryType.Information);
    timer.Enabled = true;
}

Basically, just enables the timer... so theres no process intensive call there.基本上,只需启用计时器......所以那里没有进程密集型调用。

Where am I going wrong?我哪里错了?

I've tried setting the startup account to local system, network service etc... nothing works!我已经尝试将启动帐户设置为本地系统、网络服务等......没有任何效果!

Edit:编辑:

Here is my code: (processPurchaseOrders is the method where the db is queried and pdf's are generated etc...)这是我的代码:(processPurchaseOrders 是查询数据库和生成 pdf 等的方法......)

public partial class PurchaseOrderDispatcher : ServiceBase
{
    //this is the main timer of the service
    private System.Timers.Timer timer;

    public PurchaseOrderDispatcher()
    {
        InitializeComponent();
    }

    // The main entry point for the process
    static void Main()
    {
      #if (!DEBUG)
        ServiceBase[] ServicesToRun;
        ServicesToRun = new ServiceBase[] { new PurchaseOrderDispatcher() };
        ServiceBase.Run(ServicesToRun);
      #else //debug code
        PurchaseOrderDispatcher service = new PurchaseOrderDispatcher();
        service.processPurchaseOrders();
      #endif
    }

    private void InitializeComponent()
    {
        this.CanPauseAndContinue = true;
        this.ServiceName = "Crocus_PurchaseOrderGenerator";
    }

    private void InitTimer()
    {
        timer = new System.Timers.Timer();

        //wire up the timer event
        timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);

        //set timer interval
        var timeInSeconds = Convert.ToInt32(ConfigurationManager.AppSettings["TimerIntervalInSeconds"]);
        timer.Interval = (timeInSeconds * 1000); // timer.Interval is in milliseconds, so times above by 1000

        timer.Enabled = true;
    }

    protected override void Dispose(bool disposing)
    {
        if (disposing && (components != null))
            components.Dispose();

        base.Dispose(disposing);
    }

    protected override void OnStart(string[] args)
    {
        //instantiate timer
        Thread t = new Thread(new ThreadStart(this.InitTimer));
        t.Start();
    }

    protected override void OnStop()
    {
        //turn off the timer.
        timer.Enabled = false;
    }

    protected override void OnPause()
    {
        timer.Enabled = false;

        base.OnPause();
    }

    protected override void OnContinue()
    {
        timer.Enabled = true;

        base.OnContinue();
    }

    protected void timer_Elapsed(object sender, ElapsedEventArgs e)
    {
        processPurchaseOrders();
    }
}

I just had the same problem. 我刚遇到同样的问题。

It turned out it was because I was running it as a console in debug mode - like the code you have above 事实证明这是因为我在调试模式下将其作为控制台运行 - 就像上面的代码一样

#if (!DEBUG)

#else //debug code

#endif

And I had compiled it in debug mode and installed the service 我已经在调试模式下编译并安装了服务

When I compiled it in release mode it worked as expected 当我在发布模式下编译它时,它按预期工作

Hope this helps 希望这可以帮助

From MSDN : 来自MSDN
"Do not use the constructor to perform processing that should be in OnStart. Use OnStart to handle all initialization of your service. The constructor is called when the application's executable runs, not when the service runs. The executable runs before OnStart. When you continue, for example, the constructor is not called again because the SCM already holds the object in memory. If OnStop releases resources allocated in the constructor rather than in OnStart, the needed resources would not be created again the second time the service is called." “不要使用构造函数来执行应该在OnStart中的处理。使用OnStart来处理服务的所有初始化。构造函数在应用程序的可执行文件运行时调用,而不是在服务运行时调用。可执行文件在OnStart之前运行。当你继续例如,由于SCM已经将对象保存在内存中,因此不会再次调用构造函数。如果OnStop释放在构造函数而不是OnStart中分配的资源,则第二次调用服务时将不会再次创建所需的资源。

If your timer is not initialized in the OnStart call, this could be a problem. 如果您的计时器未在OnStart调用中初始化,则可能是一个问题。 I would also check the type of timer, make sure its a System.Timers.Timer for Services. 我还会检查计时器的类型,确保它是一个System.Timers.Timer for Services。 Here is an example of how to setup the timer in a windows service. 以下是如何在Windows服务中设置计时器的示例。

//TODONT: Use a Windows Service just to run a scheduled process // TODONT:使用Windows服务只是为了运行计划的进程

I tried your code, and it seems ok. 我尝试了你的代码,似乎没问题。 The only difference I had was to hard code the timer value (Service1.cs). 我唯一的区别是硬编码定时器值(Service1.cs)。 Let me know if the below doesnt work. 如果以下内容不起作用,请告诉我。

Service1.cs Service1.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Text;
using System.Timers;
using System.Threading;

namespace WindowsServiceTest
{
    public partial class Service1 : ServiceBase
    {
        private System.Timers.Timer timer;

        public Service1()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            //instantiate timer
            Thread t = new Thread(new ThreadStart(this.InitTimer)); 
            t.Start();
        }

        protected override void OnStop()
        {
            timer.Enabled = false;
        }

         private void InitTimer()  
         {     
             timer = new System.Timers.Timer();  
             //wire up the timer event 
             timer.Elapsed += new ElapsedEventHandler(timer_Elapsed); 
             //set timer interval   
             //var timeInSeconds = Convert.ToInt32(ConfigurationManager.AppSettings["TimerIntervalInSeconds"]); 
             double timeInSeconds = 3.0;
             timer.Interval = (timeInSeconds * 1000); 
             // timer.Interval is in milliseconds, so times above by 1000 
             timer.Enabled = true;  
         }

        protected void timer_Elapsed(object sender, ElapsedEventArgs e) 
        {
            int timer_fired = 0;
        }
    }
}

Service1.Designer.cs Service1.Designer.cs

namespace WindowsServiceTest
{
    partial class Service1
    {
        /// <summary> 
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Component Designer generated code

        /// <summary> 
        /// Required method for Designer support - do not modify 
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            components = new System.ComponentModel.Container();
            this.ServiceName = "Service1";
            this.CanPauseAndContinue = true;
        }

        #endregion
    }
}

I just created a blank Windows Service project and add the below so I could run installutil.exe and attach to the above to see if the event was firing (and it did). 我刚刚创建了一个空白的Windows服务项目并添加以下内容,因此我可以运行installutil.exe并附加到上面以查看事件是否正在触发(并且确实如此)。

using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.ServiceProcess;

namespace WindowsServiceTest
{
    [RunInstaller(true)]
    public class MyServiceInstaller : System.Configuration.Install.Installer
    {
        public MyServiceInstaller()
        {
            ServiceProcessInstaller process = new ServiceProcessInstaller();

            process.Account = ServiceAccount.LocalSystem;

            ServiceInstaller serviceAdmin = new ServiceInstaller();

            serviceAdmin.StartType = ServiceStartMode.Manual;
            serviceAdmin.ServiceName = "Service1";
            serviceAdmin.DisplayName = "Service1 Display Name";
            Installers.Add(process);
            Installers.Add(serviceAdmin);
        }
    }
}

In case anyone else runs across this in the future, I received the same Error 1053 when trying to start my Windows service on Windows Server 2012. 如果将来有其他人遇到这种情况,我在Windows Server 2012上尝试启动Windows服务时收到了相同的错误1053。

The issue ended up being that the service was developed targeting the .NET framework 4.5.1, but the Windows Server 2012 instance did not have that version of the .NET framework installed. 问题最终是该服务是针对.NET framework 4.5.1开发的,但Windows Server 2012实例没有安装该版本的.NET框架。 Backing the service down to target .NET 4.0 fixed the error. 将服务备份到目标.NET 4.0修复了错误。

When I see an issue like this, I'd always go check out the Event Viewer first.当我看到这样的问题时,我总是先 go 检查事件查看器 It does provide a lot more essential details for initial investigation and debugging.它确实为初步调查和调试提供了更多重要的细节。

Often times, we forget about Event Viewer captures unhandled .net runtime exceptions, which can be found under Windows Logs > Application , then filter by Event ID 1026 (.NET Runtime)很多时候,我们忘记了事件查看器捕获未处理的 .net 运行时异常,可以在Windows Logs > Application下找到,然后按事件 ID 1026 (.NET 运行时)过滤

在此处输入图像描述

The constructor was the issue for me. 构造函数对我来说是个问题。 The constructor must have been throwing an excpetion about missing DLLs. 构造函数必须抛出关于丢失DLL的重复。

My issue: my inexperience with creating installers. 我的问题:我缺乏创建安装程序的经验。 I didn't have the dependent DLLs being copied into the install folder (I needed to select Release build configuration when creating Primary Project Output). 我没有将依赖的DLL复制到安装文件夹中(我需要在创建主项目输出时选择发布构建配置)。

This worked for me. 这对我有用。 Basically make sure the Log on user is set to the right one. 基本上确保登录用户设置为正确的用户。 However it depends how the account infrastructure is set. 但是,这取决于帐户基础结构的设置方式。 In my example it's using AD account user credentials. 在我的示例中,它使用的是AD帐户用户凭据。

In start up menu search box search for 'Services' -In Services find the required service -right click on and select the Log On tab -Select 'This account' and enter the required content/credentials -Ok it and start the service as usual 在启动菜单搜索框中搜索“服务” - 在“服务”中找到所需的服务 - 右键单击​​并选择“登录”选项卡 - 选择“此帐户”并输入所需的内容/凭据 - 然后按常规启动服务

在此输入图像描述

I had the same problem when trying to run.Net Core 3.0.0 Windows Service.我在尝试运行.Net Core 3.0.0 Windows 服务时遇到了同样的问题。 Solution stated here works for me. 此处所述的解决方案对我有用。 But here is the gist of it:但这里是它的要点:

Add Microsoft.Extensions.Hosting.WindowsServices package添加Microsoft.Extensions.Hosting.WindowsServices package

Add .UseWindowsService() at IHostBuilderIHostBuilder中添加.UseWindowsService()

Below is a sample:下面是一个示例:

using Microsoft.Extensions.Hosting;

public static IHostBuilder CreateHostBuilder(string[] args) =>
    Microsoft.Extensions.Hosting.Host.CreateDefaultBuilder(args)
        .ConfigureAppConfiguration((context, config) =>
        {
            IHostEnvironment env = context.HostingEnvironment;
            config.SetBasePath(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location));
            config.AddEnvironmentVariables();
            config.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);
            config.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true);
        })
        .ConfigureLogging((context, logging) =>
        {
            logging.AddConfiguration(context.Configuration.GetSection("Logging"));
            logging.AddConsole();
            logging.AddDebug();
        })
        .ConfigureServices((context, services) =>
        {                    
            services.AddHostedService<Worker>();
        })
    .UseWindowsService();

Also had this error until I found out that there's an excess ">" character on my .config file. 还有这个错误,直到我发现我的.config文件中有一个多余的“>”字符。

So, try to double check your .config file first before punching your computer ;) 因此,在打电脑之前,请先尝试仔细检查.config文件;)

I go to the server console (in the server room) and start the service from there. 我转到服务器控制台(在服务器机房)并从那里启动服务。 Remote in wont' work. 远程不会工作。

In my case; 就我而言; i was trying to install a .Net 3.5 service to Windows 2012 server. 我试图安装.Net 3.5服务到Windows 2012服务器。 In the server the .Net 4.0 framework was installed. 在服务器中安装了.Net 4.0框架。

I change my target framework of service to .Net 4.0. 我将目标服务框架更改为.Net 4.0。 Now it works fine. 现在它工作正常。

The first thing that is executed from the assembly containing the service is the Main method . 从包含服务的程序集执行的第一件事是Main方法。 And it must take special actions, or at least one such action: 它必须采取特殊行动,或至少采取一项此类行动:

public static int Main()
{
  Run(new System.ServiceProcess.ServiceBase[] { new YourServiceClass() });
}

That's what I discovered after trials and errors session when creating my first service. 这是我在创建我的第一个服务后在试验和错误会话后发现的。 I didn't use VS. 我没有使用VS. I did use VS guide ( Walkthrough: Creating a Windows Service Application in the Component Designer ), while I should rather use this one: Creating a C# Service Step-by-Step: Lesson I . 我确实使用VS指南( 演练:在组件设计器中创建Windows服务应用程序 ),而我应该使用这个: 创建C#服务循序渐进:第一课

Without suitable 'Main' method the executable finishes immediately and system reports that 30 seconds timeout was exceeded :) 没有合适的'Main'方法,可执行文件立即完成,系统报告超过30秒超时:)

Like me. 像我这样的。 In 4.6.1 do not work (I have the same message). 在4.6.1中不起作用(我有相同的消息)。 Then I try in 4.5 and work fine. 然后我尝试4.5并且工作正常。

If your service's name differs from the actual .exe file name, make sure you don't have an .exe with the same name as the service located in the same folder as this will cause it to fail. 如果您的服务名称与实际的.exe文件名不同,请确保您没有与位于同一文件夹中的服务同名的.exe,否则将导致其失败。

In my sitatuation I had a service called 'Index Reader' pointing to 'Index reader service.exe' and in the same folder an exe called 'Index reader.exe'. 在我的讨论中,我有一个名为'Index Reader'的服务,指向'Index reader service.exe',在同一文件夹中有一个名为'Index reader.exe'的exe文件。 Removing this fixed the problem. 删除此修复了问题。

在此输入图像描述

在此输入图像描述

I had the same issue But in my case when I rebuild the installer services on release. 我有同样的问题但在我的情况下,我在发布时重建安装程序服务。 Install the service again and run, The service started running without any issues 再次安装服务并运行,服务开始运行没有任何问题

暂无
暂无

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

相关问题 错误 1053 服务没有及时响应启动或控制请求 - 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服务无法启动&#39;错误1053:服务未及时响应启动或控制请求&#39; - Windows Service won't start 'Error 1053: The service did not respond to the start or control request in timely fashion' 启动服务:“错误1053:服务未及时响应启动或控制请求” - Starting a service: “Error 1053: The service did not respond to the start or control request in a timely fashion” 错误 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 C#错误1053,服务未及时响应启动或控制请求 - C# Error 1053 the service did not respond to the start or control request in a timely fashion 发生错误1053,服务未及时响应启动或控制请求 - Im getting Error 1053 the service did not respond to the start or control request in a timely fashion 错误 1053:服务没有使用 FileSystemWatcher 及时响应启动或控制请求 - Error 1053:The service did not respond to start or control request in timely fashion with FileSystemWatcher 错误1053服务未响应启动或控制请求 - Error 1053 service didnot respond to start or control request Dotnet 5.0 服务作为 EXE 运行,但在启动服务超时时出现 1053:服务没有及时响应启动或控制请求 - Dotnet 5.0 Service Runs as EXE but when starting service times out with 1053: Service did not respond in time to start or control request
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM