简体   繁体   中英

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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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