简体   繁体   English

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

[英]Service freezes when calling XDocument.Root.Add(XElement)

I am working on a service that runs periodic scans on a certain folder to gather information about some of the files that are in it and its subfolders. 我正在使用一项服务,该服务在特定文件夹上运行定期扫描,以收集有关该文件夹及其子文件夹中某些文件的信息。

I have tested the code in a console application and it runs through as expected, however when it runs from my service, the execution seems to get stuck at the part where it calls scanLog.Root.Add(temp); 我已经在控制台应用程序中测试了代码,并且该代码按预期运行,但是当从我的服务运行时,执行似乎卡在了调用scanLog.Root.Add(temp)的部分。

I can see the event entry that says "Starting to add node", however it never gets to "Finished adding node" any thoughts on why this doesn't work in my service, but works fine in a console application? 我可以看到事件条目显示“正在开始添加节点”,但是对于“为什么不能在我的服务中使用它,但是在控制台应用程序中却能正常工作”,它从来没有达到“完成添加节点”的想法。

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



    }

I found the issue that was causing the service to hang. 我发现了导致服务挂起的问题。

scanLog.Root.Add(temp);

scanLog was declared as a static XDocument at the beginning, but was redeclared within the function that was calling the posted function. 在开始时,scanLog被声明为静态XDocument,但在调用发布函数的函数中已将其重新声明。

它永远不会执行的原因之一可能是因为您试图编辑与所调用服务位于同一文件夹中的文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM