4

I have a problem with a Windows Service application. The application has a timer that every x seconds performs a function.

By testing the application on the local developer PC, the service works correctly.

By testing the service on the Windows Server 2008 compiling also in Release mode, when I start the service I get the following error

Error 1053: The service did not responde to the start or control request in a timely fashion

If I go to check from the server event viewer I get this information

Error from error viewer

Below I leave my little snippet of code

private const int TICK_TIMER = 120000; //Start timer every 2 minutes
private Timer readTimer = null;

public BarcodeReader()
{
    InitializeComponent();
}

protected override void OnStart(string[] args)
{
    readTimer = new Timer();
    readTimer.Interval = TICK_TIMER;
    readTimer.Elapsed += readTimer_Tick;
    readTimer.Enabled = true;
    WriteLog("Servizio Barcode started");
}

private void readTimer_Tick(object sender, ElapsedEventArgs e)
{
    //Start function
    try
    {
        MyFunction();
    }
    catch (Exception ex)
    {
        WriteLog("ERROR: " + ex.Message);
    }
}

private void WriteLog(string mex)
{
    try
    {
        //sw = new StreamWriter(AppDomain.CurrentDomain.BaseDirectory + "\\LogFile.txt", true);
        using (var sw = new StreamWriter(Globals.LogPath + "LogBarcodeService.txt", true))
        {
            sw.WriteLine(DateTime.Now.ToString(CultureInfo.CurrentCulture) + ": " + mex);
            sw.Flush();
            sw.Close();
        }
    }
    catch (Exception)
    {

        throw;
    }
}

protected override void OnStop()
{
    readTimer.Enabled = false;
    WriteLog("Servizio Barcode terminated");
}

N.B. On the server NET Framework 4.5 is installed as on the Developer PC


UPDATE

This is the call to the InitializeComponent function

namespace BarcodeReaderService
{
    partial class BarcodeReader
    {
        /// <summary> 
        /// Variabile di progettazione necessaria.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Pulire le risorse in uso.
        /// </summary>
        /// <param name="disposing">ha valore true se le risorse gestite devono essere eliminate, false in caso contrario.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Codice generato da Progettazione componenti

        /// <summary> 
        /// Metodo necessario per il supporto della finestra di progettazione. Non modificare 
        /// il contenuto del metodo con l'editor di codice.
        /// </summary>
        private void InitializeComponent()
        {
            components = new System.ComponentModel.Container();
            this.ServiceName = "Service1";
        }

        #endregion
    }
}

UPDATE 2

I tried to bring all the code into an application console and run them smoothly on the server.

Lorenzo Belfanti
  • 1,205
  • 3
  • 25
  • 51
  • As we can see, the constructor of BarcodeReaderService throws an Exception of type "FileNotFound". That means that a necessary file cannot be found. It seems that you are missing to deploy a needed dependency. It seems that you are trying to run a Windows Forms App as a windows service. The error is coming from the InitializeComponent() designer call. I think some referenced Assembly (design time component) is missing in your deployment path. – Sven Jun 29 '17 at 10:06
  • @Sven The project is Windows Service, I do not understand why the developer pc works. Should the error also occur there? – Lorenzo Belfanti Jun 29 '17 at 10:15
  • You are missing a dependency. Show us the the code in InitializeComponent call. – Sven Jun 29 '17 at 11:13
  • @Sven I added it as required. – Lorenzo Belfanti Jun 29 '17 at 11:20
  • Ok it was just to make sure that you have no special code/component in there. You should make sure that you deploy all necessary assembly references to your server in order to make your service run. The exception states that some file is missing. So you must be sure to have all necessary files in your deployment on the server. – Sven Jun 29 '17 at 12:10
  • I guess your service should read barcodes at certain times and there are some references regarding a barcode scanner. These could be missing. – Sven Jun 29 '17 at 12:11
  • @Sven How do we figure out what's missing? – Lorenzo Belfanti Jun 29 '17 at 12:12
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/147929/discussion-between-sven-and-lorenzo-belfanti). – Sven Jun 29 '17 at 12:13
  • interesting: From your exception stack trace, it seems that your constructor is throwing the file not found exception. Yet, in your constructor there does not seem to be any reference to any file. I would suggest you build your code in "debug" mode which would give you the line number of where it is failing. This may provide a clue. Another thing to check would be: do you have any static initializers ? i.e., any static block of code (possibly in your `Globals` class ?). Does this try to read any file ? – Subbu Jun 29 '17 at 12:15
  • @Subbu I tried to comment all function of the Windows Service. I just left OnStart and OnStop that don't do nothing and still doesn't work – Lorenzo Belfanti Jun 29 '17 at 12:56

2 Answers2

2

The solution should be to include everything from your output directory (e.g. bin\Debug) to copy to a certain folder on your server. From there you run InstallUtil to register the service.

Another way would be to create an installer for the Windows Service.

Sven
  • 2,345
  • 2
  • 21
  • 43
1

One thing to check would be the .Net framework version between your development machine and the Server.

We faced a similar issue when our development machine had .Net 4.7 (the latest one as of now) and the Server had 4.5.

In our case, we also had the following logged in our event viewer

First exception

Framework Version: v4.0.30319

Description: The process was terminated due to an unhandled exception.

Exception Info: System.IO.FileLoadException

Second exception entry

Faulting module name: KERNELBASE.dll, version: 6.3.9600.18340, time stamp: 0x5736541b

Exception code: 0xe0434352

Fault offset: 0x00014878

You can use this guide to find the exact version: https://learn.microsoft.com/en-us/dotnet/framework/migration-guide/how-to-determine-which-versions-are-installed

Please also note that the later versions are in-place upgrades ! So once you have the newest version, there is no way to go back to the old version !

https://learn.microsoft.com/en-us/dotnet/framework/install/guide-for-developers

The fact that later .NET Framework 4.x versions are in-place updates to earlier versions means that you cannot install an earlier version listed in the table if a later version is already installed

Community
  • 1
  • 1
Subbu
  • 2,130
  • 1
  • 19
  • 28
  • I have checked the version of the server and it's .NET 4.5. I have tried creating a new project in a new solution so without any reference and in that case it works. The problem is that I have many features that are in the main application and I need to make these references to use them. – Lorenzo Belfanti Jul 03 '17 at 06:51
  • You scenario seems to be some missing referenced file / DLL. Since the service is not giving the full exception - can you see whether you can add a try / catch block to your `OnStart` block and see whether the exception gives any further details ? Please see this related item as well - https://stackoverflow.com/questions/7402557/windows-service-will-not-start-error-1053?rq=1 – Subbu Jul 03 '17 at 07:52
  • I tried to bring all the code into an application console and run them smoothly on the server. – Lorenzo Belfanti Jul 03 '17 at 15:39