Search code examples
c#windows-services

Unable to start Windows Service if reference is used


I have create a windows service. when i try to start the service, i recieve the error

Windows could not start the Auto Process service on Local Computer.

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

This is my code

using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.ServiceProcess;
using System.Timers;
using Drone.Engine;                                                                             //

namespace Auto
{
public partial class Service1 : ServiceBase
{
    static string month = DateTime.Now.ToString("MMM");
    private Timer timer = new Timer();
    private Dictionary<string, bool> processStatus = new Dictionary<string, bool>();
    private Executor worker = new Executor();                                               //
    public Service1()
    {
        InitializeComponent();
    }

    protected override void OnStart(string[] args)

    {
        processStatus.Add("start", false);
        processStatus.Add("stop", false);
        System.Threading.Thread.Sleep(10000);
        WriteToFile("Service Started at " + DateTime.Now);
        timer.Elapsed += new ElapsedEventHandler(OnElapsedTime);
        timer.Interval = 2000;
        timer.Enabled = true;
    }

    private void OnElapsedTime(object sender, ElapsedEventArgs e)
    {
        foreach (var process in processStatus.Where(v => v.Value == false))
        {
            WriteToFile(process.Key + " Sync Started at " + DateTime.Now);
            ChangeProcessStatus(process.Key, true);
            worker.Execute(Executor.sender.Navision, process.Key);                          // 
            ChangeProcessStatus(process.Key, false);
            WriteToFile(process.Key + " Sync Finished at " + DateTime.Now);
        }

    }

    protected override void OnStop()
    {
        WriteToFile("Service stopped at " + DateTime.Now);
    }

    private void ChangeProcessStatus(string key, bool status)
    {
        processStatus[key] = status;
    }
    public void WriteToFile(string Message)
    {
        string path = AppDomain.CurrentDomain.BaseDirectory + "\\data\\logs";
        if (!Directory.Exists(path))
        {
            Directory.CreateDirectory(path);
        }
        string filepath = AppDomain.CurrentDomain.BaseDirectory + "\\data\\logs\\ServiceLog_" + DateTime.Now.Date.Day + " " + month + ".log";
        if (!File.Exists(filepath))
        {
            using (StreamWriter sw = File.CreateText(filepath))
            {
                sw.WriteLine(Message);
            }
        }
        else
        {
            using (StreamWriter sw = File.AppendText(filepath))
            {
                sw.WriteLine(Message);
            }
        }
    }
}
}

But if i remove the lines using Drone.Engine;

The service is starting successfully. Why am i unable to use it in the service?

The dll is present in the same directory. I have used the release build for installing. Still this issue.


Solution

  • Previously i was using filepaths like

    filePath=@"data\log";
    

    This was working fine in my application. But when i reference it to my windows service, i was getting System.IO.DirectoryNotFoundException error.

    I changed the filepath to

    filepath=AppDomain.CurrentDomain.BaseDirectory + "\\data\\logs";
    

    It worked for both my service and application.