user1416994
user1416994

Reputation: 13

C# Windows service not starting

I try to create a windows service which automatically startsup. I am able to install and deinstall the service. If I try to start it, I get following error message: "Der Dienst antwortete nicht rechtzeitig auf die Start- oder Steueranfrage". (I try to translate) "The service don't respont in time on start or control request".

Here is my poor code

    public class LisaServerService: System.ServiceProcess.ServiceBase
{
    private Program lisaServerServiceProgram;

    public static string LisaServiceName = "LISA-ServerService";

    [STAThread]
    public static void Main(string[] args)
    {
        LisaServerService lisaServerService = new LisaServerService();

        if (Environment.UserInteractive)
        {
            lisaServerService.OnStart(args);
            Console.WriteLine("I am a service. Normally you can't see the console... just ignore me");
            Console.ReadLine();
            lisaServerService.OnStop();
        }
        else
        {
            ServiceBase.Run(lisaServerService);
        }
    }

    public LisaServerService()
    {
        InitializeComponent();
    }

    private void InitializeComponent()
    {
        this.CanShutdown = true;
        this.ServiceName = "LISA - ServerService";
        this.CanPauseAndContinue = true;
        this.lisaServerServiceProgram = new Program();
    }

    protected override void OnStart(string[] args)
    {
        lisaServerServiceProgram.Start(null);
        base.OnStart(args);
    }

    protected override void OnStop()
    {
        lisaServerServiceProgram.Stop();
        base.OnStop();
    }

    protected override void OnShutdown()
    {
        OnStop();
        base.OnShutdown();
    }
}

Program.cs

    public class Program
{
    public Program()
    {
        Logger.LogLevel = LogLevel.Information;
        Logger.LogRange = LogRange.Write;
        Logger.Log("Logger initialized");
    }

    public void Start(string[] args)
    {
        DatabaseHandler.StartDatabase();
        NetworkHandler.StartNetwork();
        Logger.Log("Service started");
    }

if I run the service as a console program, it works fine. So the db connection + logger are working fine too. (Also within < 10ms)

Upvotes: 0

Views: 1107

Answers (1)

Doug Morrow
Doug Morrow

Reputation: 1366

If you're running the service in interactive mode it's waiting for the console here:

if (Environment.UserInteractive)
{
    lisaServerService.OnStart(args);
    Console.WriteLine("I am a service. Normally you can't see the console... just ignore me");
    Console.ReadLine();
    ...

That is likely preventing the service from responding properly to indicate it is started.

Upvotes: 1

Related Questions