开发者

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.

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:

error 1053 the service did not respond to the start or control request in a timely fashion

My OnStart method looks like this:

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...)

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.Enable开发者_StackOverflow社区d = 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:

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.

If your timer is not initialized in the OnStart call, this could be a problem. I would also check the type of timer, make sure its a System.Timers.Timer for Services. Here is an example of how to setup the timer in a Windows service.

//TODONT: Use a Windows Service just to run a scheduled process

I tried your code, and it seems ok. The only difference I had was to hard code the timer value (Service1.cs). Let me know if the below doesn't work.

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

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).

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.

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. Backing the service down to target .NET 4.0 fixed the error.


When I see an issue like this, I'd always go check out the Event Viewer first. 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)

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


The constructor was the issue for me. The constructor must have been throwing an excpetion about missing DLLs.

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).


Also had this error until I found out that there's an excess ">" character on my .config file.

So, try to double check your .config file first before punching your computer ;)


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.

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

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


I had the same problem when trying to run .Net Core 3.0.0 Windows Service. Solution stated here works for me. But here is the gist of it:

Add Microsoft.Extensions.Hosting.WindowsServices package

Add .UseWindowsService() at IHostBuilder

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();


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. In the server the .Net 4.0 framework was installed.

I change my target framework of service to .Net 4.0. Now it works fine.


The first thing that is executed from the assembly containing the service is the Main method . 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. 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.

Without suitable 'Main' method the executable finishes immediately and system reports that 30 seconds timeout was exceeded :)


Like me. In 4.6.1 do not work (I have the same message). Then I try in 4.5 and work fine.


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.

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'. Removing this fixed the problem.

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

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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜