[英]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.