Windows服务器无法在.net Framework 4.5.1上运行

时间:2018-09-03 13:24:25

标签: c# windows-services

我试图在Windows Server 2012上安装Windows Service,但此错误始终返回我

  

错误1053:服务未响应启动或控制   及时请求

这就是我启动Windows服务的方式:

protected override void OnStart(string[] args)
{

    try
    {

        int serviceWorkingDurationSecond = int.Parse(ConfigurationManager.AppSettings["serviceWorkingDurationSeconds"].ToString());

        // For first time, set amount of seconds between current time and schedule time
        _timer = new System.Timers.Timer();

        _scheduleTime = DateTime.Today.AddMinutes(serviceWorkingDurationSecond); // Schedule to run once a day at 9:00 p.m.
        if (_scheduleTime.Subtract(DateTime.Now).TotalSeconds * 1000 <= 0)
            _scheduleTime = DateTime.Today.AddDays(1).AddMinutes(serviceWorkingDurationSecond); // Schedule to run once a day at 9:00 p.m.
        _timer.Enabled = true;
        _timer.Interval = _scheduleTime.Subtract(DateTime.Now).TotalSeconds * 1000;
        _timer.Elapsed += new System.Timers.ElapsedEventHandler(Timer_Elapsed);
    }
    catch (Exception ex)
    {
        GeneralMethods.createLogFile("OnStart() Function error*** " + ex.ToString());
    }
}

private static object _lock = new object();
    public static void createLogFile(string errorMsg)
    {
        try
        {
            lock (_lock)
            {
                string appDirectory = Path.GetDirectoryName(Application.ExecutablePath);
                if (!Directory.Exists(appDirectory + "\\Log"))
                {
                    DirectoryInfo di = Directory.CreateDirectory(appDirectory + "\\Log");//create folder in direction if not exists
                }
                File.AppendAllText(appDirectory + "\\Log\\Log.txt", errorMsg + Environment.NewLine);
            }
        }
        catch (Exception ex)
        {

        }

    }

我认为这是导致Windows服务在.net Framework 4.5.2上工作的原因

1 个答案:

答案 0 :(得分:0)

就我而言,我在Entity framework中遇到了问题

因此,我检查了服务器上安装的最新实体框架和Entity framework 4.5.2

未安装在Windows Server 2012上,因此我是从Microsoft网站https://www.microsoft.com/en-us/download/details.aspx?id=42637安装的,然后重新启动服务器,现在可以正常工作了