本地计算机上的Windows服务启动,然后停止了错误 [英] Windows service on Local Computer started and then stopped error

查看:712
本文介绍了本地计算机上的Windows服务启动,然后停止了错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通常,我收到以下错误消息:
(本地计算机上的服务名称服务已启动,然后停止。某些服务未由其他服务或程序使用时,它们会自动停止)我的代码,例如不存在的驱动器路径等。Windows服务将无法启动。

Usually, I get this error: (The "service name" service on Local Computer started and then stopped. Some services stop automatically if they are not in use by other service or programs) when there's something wrong with my code, like non-existing drive paths, etc. The windows service will not start.

我有一个Windows服务,可以将文件夹/文件备份到某个位置,如果它达到了大小限制。所有细节均由Windows服务在启动时读取的XML配置提供。我有一个单独的Windows表单,其中包含一个按钮,该按钮完全可以执行Windows服务的onstart操作。在将代码放入Windows服务之前,我先使用Windows窗体对其进行调试。

I have a windows service that backs up folder/files, to a location if it reached the size limit. Details are all provide by an XML Configuration that the windows service reads on start. I have a separate windows forms that has a button that does exactly what my windows service's onstart is doing. I use my windows forms for debugging the code before I put it in my windows service.

启动Windows窗体时。它完成了它应该做的事情。当我将代码放入Windows服务的OnStart()方法时,出现错误。

When I start my windows forms. It does what it suppose to do. When I put my code in the windows service OnStart() method the error showed up.

这是我的代码:

protected override void OnStart(string[] args)
{

    private static string backupConfig = @"D:\LogBackupConfig\backupconfig.xml";
    private static string serviceStat = @"D:\LogBackupConfig\Status.txt";
    private static string fileFolderStat = @"D:\LogBackupConfig\FileFolderStat.txt";

    protected override void OnStart(string[] args)
    {
        if (File.Exists(backupConfig))
        {
            FileSystemWatcher watcher = new FileSystemWatcher();
            XmlTextReader reader = new XmlTextReader(backupConfig);

            XmlNodeType type;
            List<string> listFile = new List<string>();
            string fileWatch = "";

            //this loop is for reading XML elements and assigning to variables
            while (reader.Read())
            {
                type = reader.NodeType;
                if (type == XmlNodeType.Element)
                {
                    if (reader.Name == "File")
                    {
                        reader.Read();
                        fileWatch = reader.Value;
                    }
                    else if (reader.Name == "Folder")
                    {
                        reader.Read();
                        fileWatch = reader.Value;
                    }
                }
            }
            reader.Close();

            watcher.Path = fileWatch;
            watcher.Filter = "*.*";

            //this loop reads whether the service will watch a file/folder
            XmlTextReader reader1 = new XmlTextReader(backupConfig);
            while (reader1.Read())
            {
                type = reader1.NodeType;
                if (type == XmlNodeType.Element)
                {
                    if (reader1.Name == "File")
                    {
                        watcher.IncludeSubdirectories = false;
                        watcher.Changed += new FileSystemEventHandler(OnChangedFile);
                    }
                    else if (reader1.Name == "Folder")
                    {
                        watcher.IncludeSubdirectories = true;
                        watcher.Changed += new FileSystemEventHandler(OnChangedFolder);
                    }
                }
            }
            reader1.Close();

            watcher.EnableRaisingEvents = true;

        }
        else
        {
            StreamWriter sw = new StreamWriter(serviceStat, true);
            sw.WriteLine("File not found. Please start the Log Backup UI first.");
            sw.Close();
        }
    }

我不知道是什么原因导致Windows服务无法正常运行开始时,Windows窗体模拟器运行正常。

I don't know what keeps the windows service not starting, the windows form simulator worked fine. What seems to be the problem?

更新:
经过多次试验,我注意到Windows仅使用文件夹目录(不带文件)服务不起作用。当我用特定文件(包括其目录)替换fileWatch变量时,Windows服务启动。当我将其更改回文件夹位置时,它不起作用。我认为文件夹位置在Filewatcher中不起作用。

UPDATE: After many trials I've noticed that using only a folder directory (w/out file), the windows service doesn't work. When I replaced the fileWatch variable with a specific file (including its directory), the windows service started. When I changed it back to a folder location, it didn't work. What I think is that folder locations doesn't work in a filewatcher.

当我尝试创建一个监视文件夹位置的新Windows服务时,它起作用了。当我在原始Windows服务中尝试相同的位置时,它不起作用!我介意$#* ed!似乎每次创建新代码/函数时,我都必须创建一个新的Windows服务并构建安装程序。这种方式可以跟踪出现错误的位置。

When I tried creating a new windows service that watches a folder location, it worked.. However, when I tried the same location in my original windows service, it didn't work! I was mindf$#*ed! It seems that I have to create a new windows service and build the installer everytime I place a new code/function.. This way I can keep track where I get an error.

推荐答案

如果服务那样启动和停止,则意味着您的代码将引发未处理的异常。这很难调试,但是有一些选择。

If the service starts and stops like that, it means your code is throwing an unhandled exception. This is pretty difficult to debug, but there are a few options.


  1. 咨询Windows Event Viewer 。通常,您可以转到计算机/服务器管理器,然后单击事件查看器-> Windows日志-> 应用程序来完成此操作。您可以在此处查看引发异常的原因,这可能有所帮助,但您没有获得堆栈跟踪。

  2. 将程序逻辑提取到库类项目中。现在,创建程序的两个不同版本:控制台应用程序(用于调试)和Windows服务。 (这是一些初期工作,但是从长远来看可以节省很多焦虑。)

  3. 添加更多try / catch块并登录到应用程序以更好地了解内容

  1. Consult the Windows Event Viewer. Normally you can get to this by going to the computer/server manager, then clicking Event Viewer -> Windows Logs -> Application. You can see what threw the exception here, which may help, but you don't get the stack trace.
  2. Extract your program logic into a library class project. Now create two different versions of the program: a console app (for debugging), and the windows service. (This is a bit of initial effort, but saves a lot of angst in the long run.)
  3. Add more try/catch blocks and logging to the app to get a better picture of what's going on.

这篇关于本地计算机上的Windows服务启动,然后停止了错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆