1

I am developing a self-installable Windows Service. I have a ServiceManager class that handles the installation / uninstallation, and seems to work correctly.

However, when I try to run the start the service I get, immediately, a MessageBox with the following text:

Could not start service on local system. Error 1053: The service didn't respond to the start or control request in a timely fashion.

My Service code:

public partial class MyService : ServiceBase
{
    public MyService()
    {
        using (TextWriter tw = new StreamWriter(path: "C:\\Test.txt", append: true))
        {
            tw.WriteLine("Test1");
            tw.Close();
        }
        InitializeComponent();
    }

    protected override void OnStart(string[] args)
    {

        using (TextWriter tw = new StreamWriter(path: "C:\\Test.txt", append: true))
        {
            tw.WriteLine("Test2");
            tw.Close();
        }

        Thread t = new Thread(new ThreadStart(InitService));
        t.Start();
    }

    protected override void OnStop()
    {
    }

    static void InitService()
    {
        using (TextWriter tw = new StreamWriter(path: "C:\\Test.txt", append: true))
        {
            tw.WriteLine("Test4");
            tw.Close();
        }

        while (true)
        {
            Thread.Sleep(100);
        }
    }

The designer code:

namespace MyService
{
    partial class MyService
    {
        /// <summary> 
        /// Variable del diseñador requerida.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Limpiar los recursos que se estén utilizando.
        /// </summary>
        /// <param name="disposing">true si los recursos administrados se deben eliminar; false en caso contrario, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Código generado por el Diseñador de componentes

        /// <summary> 
        /// Método necesario para admitir el Diseñador. No se puede modificar 
        /// el contenido del método con el editor de código.
        /// </summary>
        private void InitializeComponent()
        {
            components = new System.ComponentModel.Container();
            this.ServiceName = "MyService";
        }

        #endregion
    }
}

My Main:

public static void Main(string[] args)
{
    try
    {
        bool debug = false;
        bool mustRun = false;
        string errMsg = "";
        mustRun = true;

        using (TextWriter tw = new StreamWriter(path: "C:\\Test.txt", append: true))
        {
            tw.WriteLine("Test3");
            tw.Close();
        }

        ServiceManager manager = new ServiceManager();

        mustRun = !manager.ParseArgs(args, ref debug, ref errMsg);

        MyService svc = new MyService();

       //  
       //             if (debug)
       //             {
       //                 svc.DebugStart();
       //             } 
       //             else 
        if (mustRun)
        {
            System.ServiceProcess.ServiceBase[] ServicesToRun = null;
            ServicesToRun = new System.ServiceProcess.ServiceBase[] { svc };
            System.ServiceProcess.ServiceBase.Run(ServicesToRun);
        }
        else
        {
        }
    }
    catch (Exception ex)
    {
        using (TextWriter tw = new StreamWriter(path: "C:\\Test.txt", append: true))
        {
            tw.WriteLine(ex.Message);
            tw.Close();
        }
    }
}

File C:\Test.txt isn't written to, except when installing or uninstalling the service (With Test3 string). I'm using Windows XP so there shouldn't be permission problems.

I have tried installing the service with the System and NetworkService account, and the same problem arises.

With the administrator account it gives me another error, related to the account (I guess it's because I don't have a password).

Any idea about what happens?

Update: Answering David's question, this is what the event log says (hand translation, the english message could be different):

The service cannot start. The service process cannot connect with the service controller.

Update 2: Looking more carefully to the Applicatin log, the previous error was due to a mismatch between the Service installation name and the ServiceName property in the designer. It was already fixed; now there aren't new errors in the event log but the problem persists.

raven
  • 2,574
  • 2
  • 27
  • 49
  • Did you check the event log? Usually when a service won't start, it will throw an error that it automatically logged in the "Application" Event Log. That should tell you what's going on. – David Apr 18 '11 at 15:16
  • @David: Thanks for the tip, question updated. – raven Apr 18 '11 at 15:25

3 Answers3

1

From the looks of it you need to start the thread that runs the service.

Thread t = new Thread(new ThreadStart(InitService));
t.Start();
ChaosPandion
  • 77,506
  • 18
  • 119
  • 157
1

You mention that the text "Test3" is being written to the file when installing or uninstalling the service. Rather than writing "Test3" to the file, write tw.WriteLine(ex.Message); to see what the specific exception is.

the_boiler
  • 26
  • 2
  • Sorry, this was an error writing the code here; I had another Test3 printing at the beginning of the method. I'll fix it. As I pointed out, the service installs and uninstalls correctly. Thank you anyway. – raven Apr 18 '11 at 15:39
0

I have finally found the problem.

It lies in the ServiceManager; as I was installing the service from the IDE, it was getting the executable name as MyService.vshost.exe instead of MyService.exe.

I found this reading through the following link,

http://www.c-sharpcorner.com/UploadFile/timosten/DynamicServiceInCSharp11262005062503AM/DynamicServiceInCSharp.aspx

looking the registry keys it was writing:

//Open the HKEY_LOCAL_MACHINE\SYSTEM key
system = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("System");
//Open CurrentControlSet
currentControlSet = system.OpenSubKey("CurrentControlSet");
//Go to the services key
services = currentControlSet.OpenSubKey("Services");

Thank you a lot to everyone who tried to help.

raven
  • 2,574
  • 2
  • 27
  • 49