调用 XDocument.Root.Add(XElement) 时服务冻结

Service freezes when calling XDocument.Root.Add(XElement)

我正在开发一项服务,该服务对某个文件夹运行定期扫描,以收集有关该文件夹及其子文件夹中的某些文件的信息。

我已经在控制台应用程序中测试了代码,它按预期运行,但是当它从我的服务运行时,执行似乎卡在它调用 scanLog.Root.Add(temp) 的部分;

我可以看到显示 "Starting to add node" 的事件条目,但它从来没有 "Finished adding node" 想过为什么这在我的服务中不起作用,但在控制台应用程序中却能正常工作?

private void ScanForChildCopies(string dir)
    {
        foreach(var dirs in Directory.GetDirectories(dir))
        {
            ScanForChildCopies(dirs);
        }

        foreach(var files in Directory.GetFiles(dir, "*.ac"))
        {
            FileInfo fInfo = new FileInfo(files);
            serviceLogging.WriteEntry("Found File: " + files);
            if (fInfo.Exists)
            {
                if ((DateTime.Now - fInfo.LastWriteTime).Days > 0)
                {
                    serviceLogging.WriteEntry("Building node");
                    XElement temp = new XElement("childfile", new XElement("name", fInfo.Name), new XElement("lastmodified", fInfo.LastWriteTime.ToShortDateString()),
                        new XElement("age", (DateTime.Now - fInfo.LastWriteTime).Days.ToString()));
                    serviceLogging.WriteEntry("Starting to add node");
                    scanLog.Root.Add(temp);
                    serviceLogging.WriteEntry("finished Adding node to scanlog");                 
                }                    
            }
        }



    }

它从不执行的原因之一可能是因为您正在尝试编辑与您调用的服务位于同一文件夹中的文件。

我发现了导致服务挂起的问题。

scanLog.Root.Add(temp);

scanLog 在开始时被声明为静态 XDocument,但在调用已发布函数的函数中被重新声明。