簡體   English   中英

調用XDocument.Root.Add(XElement)時服務凍結

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

我正在使用一項服務,該服務在特定文件夾上運行定期掃描,以收集有關該文件夾及其子文件夾中某些文件的信息。

我已經在控制台應用程序中測試了代碼,並且該代碼按預期運行,但是當從我的服務運行時,執行似乎卡在了調用scanLog.Root.Add(temp)的部分。

我可以看到事件條目顯示“正在開始添加節點”,但是對於“為什么不能在我的服務中使用它,但是在控制台應用程序中卻能正常工作”,它從來沒有達到“完成添加節點”的想法。

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,但在調用發布函數的函數中已將其重新聲明。

它永遠不會執行的原因之一可能是因為您試圖編輯與所調用服務位於同一文件夾中的文件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM