【问题标题】:Unable to start Windows Service if reference is used如果使用引用,则无法启动 Windows 服务
【发布时间】:2018-11-22 13:42:34
【问题描述】:

我已经创建了一个 Windows 服务。当我尝试启动服务时,我收到错误

Windows 无法在本地计算机上启动自动处理服务。

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

这是我的代码

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);
            }
        }
    }
}
}

但是如果我删除这些行 使用 Drone.Engine;

服务正在成功启动。为什么我无法在服务中使用它?

dll 存在于同一目录中。我已经使用发布版本进行安装。还是这个问题。

【问题讨论】:

  • 您在事件日志中看到任何错误吗?
  • 异常信息:System.IO.DirectoryNotFoundException
  • 发现问题。我必须在创建文件的任何地方添加 AppDomain.CurrentDomain.BaseDirectory。
  • 发布您找到的答案并接受它。
  • 在使用 BaseDirectory 之前,我尝试在与 service.exe 相同的位置创建所有目录,但没有成功。为什么??

标签: c# windows-services


【解决方案1】:

以前我使用类似的文件路径

filePath=@"data\log";

这在我的应用程序中运行良好。但是当我将它引用到我的 Windows 服务时,我收到了 System.IO.DirectoryNotFoundException 错误。

我将文件路径更改为

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

它对我的服务和应用程序都有效。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-09
    • 1970-01-01
    • 2023-03-14
    相关资源
    最近更新 更多