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

Viewed 90157

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.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();
    }
}
14 Answers

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)

enter image description here

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

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

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

Related